Excel to CSV Conversion
14 Oct 20247 minutes to read
XlsIO supports converting Excel file to CSV file by saving the workbook using the SaveAs method. When saving as CSV, users can specify various delimiters to structure the data appropriately. By default, Syncfusion XlsIO uses a comma (,) as the delimiter.
Delimiters Used in CSV files
- Comma (,)
- Tab (\t)
- Semicolon (;)
- Colon (:)
- Space ( )
- Equals Sign (=)
The following code example illustrates how to convert an Excel file to CSV.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
//Saving the workbook as streams
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.csv"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream, ",");
//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
//Saving the workbook
workbook.SaveAs("Output.csv", ",");
}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
' Saving the workbook
worksheet.SaveAs("Output.csv", ",")
End Using
A complete working example to convert an Excel file to CSV in C# is present on this GitHub page.
Maximum Rows and Columns for CSV
By default, XlsIO allows only 1048576 rows and 16256 columns while loading or saving a CSV document. This limit can be increased by modifying the MaximumRowsForCsv and MaximumColumnsForCsv properties.
The following code example illustrates how to set the maximum rows and columns for saving as CSV files.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
application.MaximumRowsForCsv = 3000000;
application.MaximumColumnsForCsv = 20000;
FileStream inputStream = new FileStream("Sample.csv", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range[2000000, 1].Text = "Syncfusion";
sheet.Range[20, 18000].Text = "Syncfusion";
//Saving the workbook as stream
FileStream outputStream = new FileStream("Output.csv", FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream,",");
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
application.MaximumRowsForCsv = 3000000;
application.MaximumColumnsForCsv = 20000;
IWorkbook workbook = application.Workbooks.Open("Sample.csv");
IWorksheet sheet = workbook.Worksheets[0];
sheet.Range[2000000, 1].Text = "Syncfusion";
sheet.Range[20, 18000].Text = "Syncfusion";
//Saving the workbook
workbook.SaveAs("Output.csv", ",");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
application.MaximumRowsForCsv = 3000000
application.MaximumColumnsForCsv = 20000
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.csv")
Dim sheet As IWorksheet = workbook.Worksheets(0)
sheet.Range(2000000, 1).Text = "Syncfusion"
sheet.Range(20, 18000).Text = "Syncfusion"
//Saving the workbook
workbook.SaveAs("Output.csv", ",")
End Using
Excel to TSV Conversion
TSV (Tab-Separated Values) files can be created by saving a workbook with the tab separator (\t).
The following code example illustrates how to convert an Excel file to TSV.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
//Save the workbook in CSV format with tab(\t) as delimiter
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.tsv"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream, "\t");
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
//Saving the workbook in CSV format with tab(\t) as delimiter
workbook.SaveAs("Output.tsv", "\t");
}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
' Saving the workbook in CSV format with tab(\t) as delimiter
workbook.SaveAs("Output.tsv", vbTab)
End Using
A complete working example to convert an Excel file to TSV in C# is present on this GitHub page.