Csharp--Read Csv file to DataTable

在网上找的资料都不怎么好使,许多代码一看就知道根本没有考虑全面。

最后找到一个好用的,在codeplex上,这位老兄写成了一个framework,太重了。

http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

确实挺好用的。

我没耐下性子看他的实现,自己尝试写了如下的代码来完成了阅读csv.

参照:http://msdn.microsoft.com/en-us/library/ae5bf541%28v=vs.90%29.aspx

只写了两个方法,组织得不是特别优雅。

使用的时候只需要调用ReadCsv2DT即可,传入参数是文件路径和第一行是否是Header的布尔值。

第二个方法是替换CSVWriter封装的多余的双引号。

public static DataTable ReadCsv2DT(string filename,bool isFirstLineHeader)

{

DataTable dt = new DataTable();

int quotecount = 0;

int lastbyte = 0;

int b = 0;

DataRow dr = null;

bool isfirstline = true;

int colindex = 0;

List<string> firstlinefields = new List<string>();

StringBuilder sb = new StringBuilder();

using (FileStream fs = File.OpenRead(filename))

{

while ((b = fs.ReadByte()) != -1)

{

if (!isfirstline && dr == null)

dr = dt.NewRow();

if (b == 10 && lastbyte == 13 && quotecount % 2 == 0) //one row finished

{

if (!isfirstline)

{

dr[colindex] = sb.ToString();

dt.Rows.Add(dr);

}

else

{

if (isFirstLineHeader)

dt.Columns.Add(sb.ToString());

else firstlinefields.Add(sb.ToString());

//build the table strucure

if (isfirstline && !isFirstLineHeader)

{

for (int i = 1; i <= firstlinefields.Count; i++)

{

dt.Columns.Add("col" + i);

}

dr = dt.NewRow();

for (int j = 0; j < firstlinefields.Count; j++)

{

dr[j] = firstlinefields[j];

}

dt.Rows.Add(dr);

}

isfirstline = false;

}

sb.Clear();

quotecount = 0;

b = 0;

dr = null;

colindex = 0;

lastbyte = 0;

}

else if (b == 44 && quotecount % 2 == 0) //one filed found 44 stand for comma

{

if (isfirstline)

{ if (isFirstLineHeader)

dt.Columns.Add(removeTextQualifier(sb.ToString()));

else

firstlinefields.Add(removeTextQualifier(sb.ToString()));

}

else dr[colindex] =removeTextQualifier(sb.ToString());

sb.Clear();

colindex++;

}

else

{

if (b == 34) quotecount++; //"

lastbyte = b;

sb.Append(UnicodeEncoding.ASCII.GetString(new byte[] { byte.Parse(b.ToString()) }));

}

}

};

return dt;

}

public static string removeTextQualifier(string text)

{

string pattern = "^\"(?<word>[\\s\\S]*)\"$";

Regex rgx = new Regex(pattern,RegexOptions.Multiline);

Match m = rgx.Match(text);

if (m.Success)

//return m.Result("($1)").Replace("\"\"", "\"");

return m.Groups["word"].Value.Replace("\"\"", "\"");

else

return text.Replace("\"\"", "\"");

}

效果图:测试了一个文件,效果还可以,和Excel打开显示的无差异。

如果你需要测试的话,请确保自己写的文件是有效的csv文件,否则请使用excel另存为,自己写的文件改后缀不是真正的csv.

时间: 2024-10-15 18:15:00

Csharp--Read Csv file to DataTable的相关文章

C# - CSV file reader

// -------------------------------------------------------------------------------------------------------------------- // <summary> // Defines the CSVFileReader type. // </summary> // ----------------------------------------------------------

a helper class for generating xls or csv file

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Office.Interop.Excel; namespace Reuters.ProcessQuality.ContentAuto.Lib{ public class XlsOrCsvUtil { /// <summary> /// Generate Xls Or Csv File /// <

解决Jmeter Non GUI运行时报“...ensure the jmeter .save.saveservice.* properties are the same as when the CSV file was created or the file may be read incorrectly”错误的问题

错误信息: File 'xxx.jtl' does not contain the field names header, ensure the jmeter.save.saveservice.* properties are the same as when the CSV file was created orthe file may be read incorrectly 解决: jmeter.properties配置文件 jmeter.save.saveservice.output_fo

[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

Hello everyone, this is the third post of the series. .   Background =============== In my solution, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Withou

How to handle csv file using python

As i need to read something from a csv file using python.  I got something and put it here. Module: csv import csv FILE_FULL_PATH = 'D:\\Work\\script\\My Script\\Book1.csv' def f(): with open(FILE_FULL_PATH,'rb') as csvfile: for row in csv.reader(csv

Powercli随笔 - PowerCLI script to sequentially Storage vMotion VMs from a CSV File

PowerCLI script to sequentially Storage vMotion VMs from a CSV File This is a PowerCLI script that I use to Storage vMotion (s/vmotion) VMs from an input file (CSV File). This helps me evacuate VMs from a datastore that will be decommissioned and thi

csharp: datagridview Convert csv file

/// <summary> /// 保存文件 /// 涂聚文 /// 2014-08-29 /// Geovin Du /// </summary> /// <param name="dGV"></param> /// <param name="filename"></param> public static void DataGridViewToCsV(DataGridView dGV, st

SQL Script for select data from ebs and make a csv file to FTP

DECLARE CURSOR cur_lcy_test IS SELECT rcta.customer_trx_id, rcta.trx_number, rcta.trx_date FROM ra_customer_trx_all rcta WHERE rcta.customer_trx_id = 11993; -- rec_lcy_test cur_lcy_test%ROWTYPE; -- w_csv_line_num NUMBER := 0; TYPE tbl_varchar2 IS TAB

SQL Script for read information from a csv file in FTP Server

DECLARE w_file_path VARCHAR2(4000) := 'XXIF_INPUT'; --all_directories.directory_name w_file_name VARCHAR2(4000) := 'lcytest001.csv'; --The file name w_file_exists BOOLEAN; w_file_length NUMBER(10) DEFAULT 0; w_file_type utl_file.file_type; w_line VAR