Excel to CSV Conversion
27 Jul 20267 minutes to read
XlsIO can convert an Excel workbook to a CSV file by saving the workbook with the SaveAs overload that accepts a delimiter string. The default delimiter is a comma (,).
NOTE
IMPORTANT: Before running the samples on this page, install the required NuGet package for your target platform and register your Syncfusion license key. For more information, see the Licensing overview.
The supported delimiters are:
-
Comma (
,) — the default. -
Tab (
\t) — produces a TSV file. -
Semicolon (
;) -
Colon (
:) -
Space (
) -
Equals (
=) — prefixes cells that begin with=,+,-, or@with a single quote so they are not interpreted as formulas when opened in a spreadsheet program. This is the recommended setting when exporting to a CSV that may be re-imported.
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;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Saving the workbook
workbook.SaveAs(Path.GetFullPath("Output/Sample.csv"), ",");
}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
workbook.SaveAs("Output.csv", ",")
End UsingA 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 up to 1,048,576 rows and 16,256 columns when loading or saving a CSV document. Exceeding these limits throws ArgumentOutOfRangeException. Increase them by setting MaximumRowsForCsv and MaximumColumnsForCsv on IApplication. The properties affect loading as well as saving, so they apply whether the input is an .xlsx file or another .csv.
The following code example illustrates how to override the load/ save limits and convert a workbook to CSV.
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 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 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 UsingExcel to TSV Conversion
TSV (Tab-Separated Values) files can be created by saving a workbook with the tab character (\t in C#, vbTab in VB.NET) as the delimiter.
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;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Save the workbook in CSV format with tab(\t) as delimiter
workbook.SaveAs(Path.GetFullPath("Output/Output.tsv"), "\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 UsingA complete working example to convert an Excel file to TSV in C# is present on this GitHub page.