填充CSV文件中的数据集

我想阅读CSV文件的内容并创建数据集。 我这样想:

var lines = File.ReadAllLines("test.csv").Select(a => a.Split(';')); DataSet ds = new DataSet(); ds.load(lines); 

但显然这是不正确的。

您需要针对CSV文件运行SELECT语句以填充数据集:

编辑:这里有一些示例代码来自http://carllbrown.blogspot.co.uk/2007/09/populate-dataset-from-csv-delimited_18.html

 string FileName = ... OleDbConnection conn = new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(FileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter ("SELECT * FROM " + Path.GetFileName(FileName), conn); DataSet ds = new DataSet("Temp"); adapter.Fill(ds); conn.Close(); 

您需要添加引用Microsoft.VisualBasic.dll以使用TextFieldParser类。

  private static DataTable GetDataTabletFromCSVFile(string csv_file_path) { DataTable csvData = new DataTable(); try { using(TextFieldParser csvReader = new TextFieldParser(csv_file_path)) { csvReader.SetDelimiters(new string[] { "," }); csvReader.HasFieldsEnclosedInQuotes = true; string[] colFields = csvReader.ReadFields(); foreach (string column in colFields) { DataColumn datecolumn = new DataColumn(column); datecolumn.AllowDBNull = true; csvData.Columns.Add(datecolumn); } while (!csvReader.EndOfData) { string[] fieldData = csvReader.ReadFields(); //Making empty value as null for (int i = 0; i < fieldData.Length; i++) { if (fieldData[i] == "") { fieldData[i] = null; } } csvData.Rows.Add(fieldData); } } } catch (Exception ex) { } return csvData; } } 

有关详细信息,请参阅此文章: http : //www.morgantechspace.com/2013/08/how-to-read-data-from-csv-file-in-c.html

您可以使用像快速CSV阅读器这样的库

 using System.IO; using LumenWorks.Framework.IO.Csv; void ReadCsv() { // open the file "data.csv" which is a CSV file with headers using (CsvReader csv = new CsvReader( new StreamReader("data.csv"), true)) { myDataRepeater.DataSource = csv; myDataRepeater.DataBind(); } } 

逗号(,)本规范中解决的问题

即使您在单元格之间添加逗号(,)也可以工作

读取CSV文件代码:

 public MainWindow() { InitializeComponent(); DataTable dtDataSource = new DataTable(); string[] fileContent = File.ReadAllLines(@"..\\Book1.csv"); if (fileContent.Count() > 0) { //Create data table columns dynamically string[] columns = fileContent[0].Split(','); for (int i = 0; i < columns.Count(); i++) { dtDataSource.Columns.Add(columns[i]); } //Add row data dynamically for (int i = 1; i < fileContent.Count(); i++) { string[] rowData = fileContent[i].Split(','); string[] realRowData = new string[columns.Count()]; StringBuilder collaboration = new StringBuilder(); int v = 0; //this region solves the problem of a cell containing ",". #region CommaSepProblem for (int j = 0, K = 0; j < rowData.Count(); j++, K++) { if ((rowData[j].Count(x => x == '"') % 2 == 0))//checks if the string contains even number of DoubleQuotes { realRowData[K] = quotesLogic((rowData[j])); } else if ((rowData[j].Count(x => x == '"') % 2 != 0))//If Number of DoubleQuotes are ODD { int c = rowData[j].Count(x => x == '"'); v = j; while (c % 2 != 0)//Go through all the next array cell till it makes EVEN Number of DoubleQuotes. { collaboration.Append(rowData[j] + ","); j++; c += rowData[j].Count(x => x == '"'); } collaboration.Append(rowData[j]); realRowData[K] = quotesLogic(collaboration.ToString()); } else { continue; } } #endregion dtDataSource.Rows.Add(realRowData); } if (dtDataSource != null) { //dataGridView1 = new DataGridView(); dataGrid1.ItemsSource = dtDataSource.DefaultView; } } } 

方法需要添加:

  string quotesLogic(string collaboration) { StringBuilder after = new StringBuilder(collaboration); if (after.ToString().StartsWith("\"") && after.ToString().EndsWith("\""))//removes 1st and last quotes as those are system generated { after.Remove(0, 1); after.Remove(after.Length - 1, 1); int count = after.Length - 1; //FACT: if you try to add DoubleQuote in a cell in excel. It'll save that quote as 2 times DoubleQuote(Like "") which means first DoubleQuote is to give instruction to CPU that the next DoubleQuote is not system generated. while (count > 0)//This loop find twice insertion of 2 DoubleQuotes and neutralise them to One DoubleQuote. { if (after[count] == '"' && after[count - 1] == '"') { after.Remove(count, 1); } count--; } } return after.ToString(); } 

如果您只是想快速创建一个填充了CSV文件中的样本数据(或直接从Excel粘贴)来播放或原型的DataTable,那么您可以使用我的Shan Carter先生的数据转换器 – 我最近添加了这个function将逗号和制表符分隔的数据输出到C#DataTable。

http://thdoan.github.io/mr-data-converter/

我在下面写了五个方法,将Csv文件转换为DataTable。

它们的设计考虑了可选的引号(例如“符号”),并且在不使用其他库的情况下尽可能多样化:

  public static DataTable GetDataTabletFromCSVFile(string filePath, bool isHeadings) { DataTable MethodResult = null; try { using (TextFieldParser TextFieldParser = new TextFieldParser(filePath)) { if (isHeadings) { MethodResult = GetDataTableFromTextFieldParser(TextFieldParser); } else { MethodResult = GetDataTableFromTextFieldParserNoHeadings(TextFieldParser); } } } catch (Exception ex) { ex.HandleException(); } return MethodResult; } public static DataTable GetDataTableFromCsvString(string csvBody, bool isHeadings) { DataTable MethodResult = null; try { MemoryStream MemoryStream = new MemoryStream(); StreamWriter StreamWriter = new StreamWriter(MemoryStream); StreamWriter.Write(csvBody); StreamWriter.Flush(); MemoryStream.Position = 0; using (TextFieldParser TextFieldParser = new TextFieldParser(MemoryStream)) { if (isHeadings) { MethodResult = GetDataTableFromTextFieldParser(TextFieldParser); } else { MethodResult = GetDataTableFromTextFieldParserNoHeadings(TextFieldParser); } } } catch (Exception ex) { ex.HandleException(); } return MethodResult; } public static DataTable GetDataTableFromRemoteCsv(string url, bool isHeadings) { DataTable MethodResult = null; try { HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader StreamReader = new StreamReader(httpWebResponse.GetResponseStream()); using (TextFieldParser TextFieldParser = new TextFieldParser(StreamReader)) { if (isHeadings) { MethodResult = GetDataTableFromTextFieldParser(TextFieldParser); } else { MethodResult = GetDataTableFromTextFieldParserNoHeadings(TextFieldParser); } } } catch (Exception ex) { ex.HandleException(); } return MethodResult; } private static DataTable GetDataTableFromTextFieldParser(TextFieldParser textFieldParser) { DataTable MethodResult = null; try { textFieldParser.SetDelimiters(new string[] { "," }); textFieldParser.HasFieldsEnclosedInQuotes = true; string[] ColumnFields = textFieldParser.ReadFields(); DataTable dt = new DataTable(); foreach (string ColumnField in ColumnFields) { DataColumn DataColumn = new DataColumn(ColumnField); DataColumn.AllowDBNull = true; dt.Columns.Add(DataColumn); } while (!textFieldParser.EndOfData) { string[] Fields = textFieldParser.ReadFields(); for (int i = 0; i < Fields.Length; i++) { if (Fields[i] == "") { Fields[i] = null; } } dt.Rows.Add(Fields); } MethodResult = dt; } catch (Exception ex) { ex.HandleException(); } return MethodResult; } private static DataTable GetDataTableFromTextFieldParserNoHeadings(TextFieldParser textFieldParser) { DataTable MethodResult = null; try { textFieldParser.SetDelimiters(new string[] { "," }); textFieldParser.HasFieldsEnclosedInQuotes = true; bool FirstPass = true; DataTable dt = new DataTable(); while (!textFieldParser.EndOfData) { string[] Fields = textFieldParser.ReadFields(); if(FirstPass) { for (int i = 0; i < Fields.Length; i++) { DataColumn DataColumn = new DataColumn("Column " + i); DataColumn.AllowDBNull = true; dt.Columns.Add(DataColumn); } FirstPass = false; } for (int i = 0; i < Fields.Length; i++) { if (Fields[i] == "") { Fields[i] = null; } } dt.Rows.Add(Fields); } MethodResult = dt; } catch (Exception ex) { ex.HandleException(); } return MethodResult; } 

如果像我一样,你从报告服务中保存,那么你应该像这样使用它:

  Warning[] warnings; string[] streamids; string mimeType; string encoding; string filenameExtension; byte[] bytes = rvMain.ServerReport.Render("csv", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings); string CsvBody = System.Text.Encoding.UTF8.GetString(bytes); DataTable dt = GetDataTableFromCsvString(CsvBody,true); 

否则,您所需要做的就是:

  bool IsHeadings = true; //Does the data include a heading row? DataTable dt = GetDataTableFromCsvString(CsvBody, IsHeadings); 

或者直接从csv文件中使用

  bool IsHeadings = true; //Does the data include a heading row? DataTable dt = GetDataTabletFromCsvFile(FilePath, IsHeadings) 

或者使用远程存储的csv文件

  bool IsHeadings = true; //Does the data include a heading row? DataTable dt = GetDataTabletFromRemoteCsv(Url, IsHeadings) 

数据集是DataTable的集合,因此创建一个如下所示:

  DataSet ds = new DataSet(); ds.Tables.Add(dt);