I have to extract specifict information from a too big CSV file with C#, the file is 233 MB in size, it has 1000008 rows and 28 columns and insert tha information in a datatable. actually i tried with OleDB and Aspose.cells, but both ways are too slow
ASPOSE CODE:
public static DataTable CsvReader(string CsvFile)
{
DataTable DtTemp = new DataTable();
try
{
System.Windows.Forms.Application.DoEvents();
Aspose.Cells.LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(CsvFile, loadOptions);
string SheetName = workbook.Worksheets[0].Name;
DtTemp = workbook.Worksheets[SheetName].Cells.ExportDataTable(0, 0, workbook.Worksheets[SheetName].Cells.MaxDataRow + 1, workbook.Worksheets[SheetName].Cells.MaxDataColumn + 1);
//filter DtTemp datatable based on 4th column
}
catch (Exception) { DtTemp.Reset(); }
return DtTemp;
}
OleDB
public static DataTable CsvReader(string CsvFile)
{
DataTable DtTemp = new DataTable();
try
{
System.Windows.Forms.Application.DoEvents();
OleDbConnection conn = null;
string strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(CsvFile) + "; Extended Properties='text; HDR=NO; IMEX=3; FMT=Delimited(,)';";
string sql_select;
conn = new OleDbConnection(strConnString.Trim());
sql_select = "select * from [" + Path.GetFileName(CsvFile) + "] WHERE F4 Like '%27628%' OR F4 Like '%35627%'";
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql_select, conn);
OleDbDataAdapter obj_oledb_da = new OleDbDataAdapter(cmd);
DataTable dtSchema = new DataTable();
obj_oledb_da.FillSchema(dtSchema, SchemaType.Source);
if (dtSchema != null)
{
writeSchema(dtSchema, CsvFile);
obj_oledb_da.Fill(DtTemp);
}
}
catch (Exception) { DtTemp.Reset(); }
return DtTemp;
}