Click or drag to resize

Importing Data from CSV into Excel with ExpertXls Library for .NET

The ExpertXls Excel Library for .NET offers support for loading CSV directly into an Excel worksheet and for saving an Excel worksheet to a CSV file. A CSV file can be loaded into a workbook using the static method ExcelWorkbook.LoadFromCSV() of the ExcelWorkbook class and a worksheet from a workbook can be exported to CSV using the ExcelWorkbook.SaveToCSV() static method.

Code Samples

Below there is a sample code for creating a workbook from a CSV file:

// The data from CSV file (numbers, dates, etc) was saved for the English US culture and 
// the CSV parser uses the current thread culture
// Temporary set the en-US culture for the current thread and restore the old culture after the CSV file was loaded
System.Globalization.CultureInfo oldCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

// get the Excel workbook format
ExcelWorkbookFormat workbookFormat = radioXlsFormat.Checked ? ExcelWorkbookFormat.Xls_2003 : ExcelWorkbookFormat.Xlsx_2007;

// create the CSV stream
string dataFilePath = System.IO.Path.Combine(Server.MapPath("~"), @"Data\awemployees.csv");
System.IO.FileStream csvDataStream = new System.IO.FileStream(dataFilePath, System.IO.FileMode.Open);

// load the data from the CSV stream to a new workbook in the specified format
ExcelWorkbook workbook = ExcelWorkbook.LoadFromCsv(csvDataStream, Encoding.GetEncoding("windows-1252"), ",", 5, 1, workbookFormat, null);

// close the CSV stream
csvDataStream.Close();

// restore the current culture
System.Threading.Thread.CurrentThread.CurrentCulture = oldCulture;