CSV文件读取
来源:互联网 发布:沈阳纳森网络 编辑:程序博客网 时间:2024/06/06 01:49
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.IO;namespace csv文件读取{ class Program { static void Main(string[] args) { DataTable Data_in = new DataTable(); Data_in=CSVFileHelper.OpenCSV("C:\\Users\\johon.ye\\Desktop\\4.csv"); string[,] Inresult = new string[3,3]; for (int i=0;i<3;i++) { for (int j = 0; j < 3;j++ ) { string abc = Data_in.Rows[0][0].ToString(); Inresult[i,j] = Data_in.Rows[0][0].ToString(); //Inresult[0, 0] = Data_in.Rows[0][0].ToString(); } } } public class CSVFileHelper { /// <summary> /// 将DataTable中数据写入到CSV文件中 /// </summary> /// <param name="dt">提供保存数据的DataTable</param> /// <param name="fileName">CSV的文件路径</param> public static void SaveCSV(DataTable dt, string fullPath) { FileInfo fi = new FileInfo(fullPath); if (!fi.Directory.Exists) { fi.Directory.Create(); } FileStream fs = new FileStream(fullPath, System.IO.FileMode.Create, System.IO.FileAccess.Write); //StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); string data = ""; //写出列名称 for (int i = 0; i < dt.Columns.Count; i++) { data += dt.Columns[i].ColumnName.ToString(); if (i < dt.Columns.Count - 1) { data += ","; } } sw.WriteLine(data); //写出各行数据 for (int i = 0; i < dt.Rows.Count; i++) { data = ""; for (int j = 0; j < dt.Columns.Count; j++) { string str = dt.Rows[i][j].ToString(); str = str.Replace("\"", "\"\"");//替换英文冒号 英文冒号需要换成两个冒号 if (str.Contains(',') || str.Contains('"') || str.Contains('\r') || str.Contains('\n')) //含逗号 冒号 换行符的需要放到引号中 { str = string.Format("\"{0}\"", str); } data += str; if (j < dt.Columns.Count - 1) { data += ","; } } sw.WriteLine(data); } sw.Close(); fs.Close(); // DialogResult result = MessageBox.Show("CSV文件保存成功!"); //if (result == DialogResult.OK) //{ // System.Diagnostics.Process.Start("explorer.exe", Common.PATH_LANG); //} } /// <summary> /// 将CSV文件的数据读取到DataTable中 /// </summary> /// <param name="fileName">CSV文件路径</param> /// <returns>返回读取了CSV数据的DataTable</returns> public static DataTable OpenCSV(string filePath) { //Encoding encoding = GetType(filePath); //Encoding.ASCII;// System.Text.Encoding encoding = System.Text.Encoding.ASCII; //Encoding.ASCII;// DataTable dt = new DataTable(); FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); // StreamReader srAsciiFromFileFalse =new StreamReader("C:\\Temp\\Test.txt",System.Text.Encoding.ASCII, false); //StreamReader sr = new StreamReader(fs, Encoding.UTF8); StreamReader sr = new StreamReader(fs, encoding); //string fileContent = sr.ReadToEnd(); //encoding = sr.CurrentEncoding; //记录每次读取的一行记录 string strLine = ""; //记录每行记录中的各字段内容 string[] aryLine = null; string[] tableHead = null; //标示列数 int columnCount = 0; //标示是否是读取的第一行 bool IsFirst = true; //逐行读取CSV中的数据 while ((strLine = sr.ReadLine()) != null) { //strLine = Common.ConvertStringUTF8(strLine, encoding); //strLine = Common.ConvertStringUTF8(strLine); if (IsFirst == true) { tableHead = strLine.Split(','); IsFirst = false; columnCount = tableHead.Length; //创建列 for (int i = 0; i < columnCount; i++) { DataColumn dc = new DataColumn(tableHead[i]); dt.Columns.Add(dc); } } else { aryLine = strLine.Split(','); DataRow dr = dt.NewRow(); for (int j = 0; j < columnCount; j++) { dr[j] = aryLine[j]; } dt.Rows.Add(dr); } } if (aryLine != null && aryLine.Length > 0) { dt.DefaultView.Sort = tableHead[0] + " " + "asc"; } sr.Close(); fs.Close(); return dt; } } }}
阅读全文
0 0
- 如何读取csv文件
- CSV文件直接读取
- C#读取CSV文件
- java读取CSV文件
- ajax 读取 .csv 文件
- Java读取CSV文件
- Java 读取 CSV 文件
- 读取.csv文件数据
- csv文件读取
- 读取/输出 CSV文件
- java读取csv文件
- c#读取csv文件
- php读取csv文件
- Python-读取csv文件
- DataTable 读取CSV文件
- c++ 读取csv文件
- CSV文件读取
- python读取CSV文件
- java项目配置dll,so动态库文件方式
- 【c#】控制台程序的显示和隐藏
- 正确认识Docker、Kubernetes 、Apache Mesos
- UE4_蓝图调试器
- 【分布式共识一】分布系统及共识介绍
- CSV文件读取
- CSP开发基础--CryptoAPI函数简介
- 【HDU
- Spring Boot项目结构推荐
- CSS3自定义滚动条样式 -webkit-scrollbar
- ATK-NEO-6M GPS模块实验(STM32F10X,中移物联网麒麟座开发板)串口4,DMA读取程序源码
- 单链表面试(二)复杂链表的拷贝
- hihocoder第九十三周 数论二·Eular质数筛法
- 一个自动生成测试用例的项目(github一个大神,曾经阿里P6的职位都没去的大神)