The Syncfusion .NET Excel (XlsIO) library offers comprehensive support for converting Excel to CSV format. It allows the creation of CSV documents from Excel documents in different formats such as xlsx, xls, xlsm, and xltm. Also, it supports saving CSV documents with customized separators instead of the default CSV comma separator.
Here is an example of how to save an Excel document as a CSV using C#.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using (FileStream inputStream = new FileStream("sample.xlsx",FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(inputStream);
using (FileStream outputStream = new FileStream("Output.csv", FileMode.Create, FileAccess.ReadWrite))
{
//Save the workbook in CSV format with comma(,) as delimiter
workbook.SaveAs(outputStream, ",");
}
}
}
By changing the separator as tab (“\t”) when saving the workbook, TSV files can be created.
Here is an example of how to convert Excel to TSV in C#.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using (FileStream inputStream = new FileStream("sample.xlsx",FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(inputStream);
using (FileStream outputStream = new FileStream("Output.tsv", FileMode.Create, FileAccess.ReadWrite))
{
//Save the workbook in CSV format with tab(\t) as delimiter
workbook.SaveAs(outputStream, "\t");
}
}
}
By changing the separator to space(“ ”) when saving the workbook, text(.txt) files can be created.
Here is an example of how to convert an Excel file to text files in C#.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using (FileStream inputStream = new FileStream("sample.xlsx",FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(inputStream);
using (FileStream outputStream = new FileStream("Output.txt", FileMode.Create, FileAccess.ReadWrite))
{
//Save the workbook in .txt format
workbook.SaveAs(outputStream, " ");
}
}
}
Besides this Excel to CSV conversion using Syncfusion .NET Excel library, developers can also use:
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.