Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data;
Using System. Data. OleDb;
Using System. Data. SQL;
Using System. Data. SqlClient;
Using System. IO;
Namespace HPRSP. CommonDataObject
{
Public class CSVHelper
{
Private string path;
Private string fileName;
/// <Summary>
/// Z Constructor
/// </Summary>
Public CSVHelper (string filePath, string fileName)
{
This. path = filePath;
This. fileName = fileName;
}
/// <Summary>
/// Read data from Csv
/// </Summary>
/// <Returns> </returns>
Public datatable read ()
{
Return read (null );
}
/// <Summary>
/// CSV files are read through file streams. By default, the first column is the title column separated by commas (,).
/// </Summary>
/// <Param name = "Files"> </param>
/// <Returns> </returns>
Public datatable readcsvfiletotable ()
{
Return readcsvfiletotable (true ,',');
}
/// <Summary>
/// CSV files are read through file streams. By default, columns are separated by commas.
/// </Summary>
/// <Param name = "Files"> file name </param>
/// <Param name = "headyes"> whether the first row is a column title </param>
/// <Returns> </returns>
Public datatable readcsvfiletotable (bool headyes)
{
Return ReadCsvFileToTable (HeadYes ,',');
}
/// <Summary>
/// Read CSV files through file streams
/// </Summary>
/// <Param name = "files"> file name </param>
/// <Param name = "HeadYes"> whether the first row is a column title </param>
/// <Param name = "span"> delimiter </param>
/// <Returns> </returns>
Public DataTable ReadCsvFileToTable (bool HeadYes, char span)
{
// File path and file name
String files = path + fileName;
DataTable dt = new DataTable ();
StreamReader fileReader = new StreamReader (files, Encoding. Default );
Try
{
// Whether the row is the first line (if HeadYes is TRUE, the first row is the title line)
Int lsi = 0;
// Delimiter Between Columns
Char cv = span;
While (fileReader. EndOfStream = false)
{
String line = fileReader. ReadLine ();
String [] y = line. Split (cv );
// The first row of the title
If (HeadYes = true)
{
// The first line
If (lsi = 0)
{
For (int I = 0; I <y. Length; I ++)
{
Dt. Columns. Add (y [I]. Trim (). ToString ());
}
Lsi ++;
}
// Start from the second column as the data column
Else
{
DataRow dr = dt. NewRow ();
For (int I = 0; I <y. Length; I ++)
{
Dr [I] = y [I]. Trim ();
}
Dt. Rows. Add (dr );
}
}
// The first line is not the title line
Else
{
If (lsi = 0)
{
For (int I = 0; I <y. Length; I ++)
{
DT. Columns. Add ("col" + I. tostring ());
}
LSI ++;
}
Datarow DR = DT. newrow ();
For (INT I = 0; I <Y. length; I ++)
{
Dr [I] = Y [I]. Trim ();
}
DT. Rows. Add (DR );
}
}
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Filereader. Close ();
Filereader. Dispose ();
}
Return DT;
}
/// <Summary>
/// Read data from CSV
/// </Summary>
/// <Param name = "colnames"> column name list, which can be empty </param>
/// <Returns> </returns>
Private datatable read (string [] colnames)
{
String SQL = createsql (colnames );
Return executetable (SQL );
}
/// <Summary>
/// Read data from CSV by executing an SQL statement
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Returns> </returns>
Private datatable executetable (string SQL)
{
Datatable table = new datatable ();
Using (oledbconnection connection = new oledbconnection (getconnstring (true )))
{
Oledbcommand command = connection. createcommand ();
Command. commandtext = SQL;
Oledbdataadapter adapter = new oledbdataadapter (command );
Adapter. Fill (table );
}
Return table;
}
# Region private Method
Private string createsql (string [] colnames)
{
String Cols = "*";
If (null! = Colnames & colnames. length> 0)
{
Stringbuilder collist = new stringbuilder ();
For (INT I = 0; I <colnames. Length-1; I ++)
{
Collist. appendformat ("[{0}],", colnames [I]);
}
Collist. appendformat ("[{0}]", collist [collist. Length]);
Cols = collist. tostring ();
}
Return string. Format ("select {0} from {1}", cols, filename );
}
Private string getconnstring (bool isheaderrow)
{
Return string. format ("Provider = Microsoft. jet. OLEDB.4.0; Extended Properties = 'text; FMT = Delimited; HDR = {0} '; data source = {1} ", IsHeaderRow? "Yes": "No", path );
}
# Endregion
}
}
From http://www.cnblogs.com/starxp/articles/2387804.html