How to export file into CSV format using xlsIO

I am using Asp.net core with syncfusionand using xlsIO for exporting. Here is code snippet:
var dtAddressList = context.SmartyStreetFormat.FromSql("GetAddressForSmartyStreet @p0", regionId).ToList();
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Create(1);
workbook.Version = ExcelVersion.Excel2013;
IWorksheet sheet = workbook.Worksheets[0];
sheet.Name = "SmartyStreetAddress";
sheet.ImportData(dtAddressList, 1, 1, true);
var path = @"D:\Region" + regionId + "OwnerAddress_" + DateTime.Now.ToString("dd-MMM-yy_HH-mm-ss_tt") + ".csv";
Stream stream = File.Create(path);
workbook.SaveAs(stream);
I am able to export file successfully but when open into excel. It give an error of file format and extension don't match. File could be correct.
Kindly look into code snippet. If there is any change in code.

10 Replies

AV Abirami Varadharajan Syncfusion Team May 11, 2018 11:52 AM UTC

Hi Kapil, 

Thank you for contacting Syncfusion support. 

As separator parameter for CSV is not provided while saving the document, the file is getting corrupted. It is recommended to use the below code to save CSV files. 

workbook.SaveAs(stream, ","); 
 
Please refer to below API reference to know more on saving the workbook as CSV. 
 

Regards, 
Abirami 



KG kapil Gupta May 11, 2018 11:54 AM UTC

Thanks for the reply. Issue resolved 


AV Abirami Varadharajan Syncfusion Team May 14, 2018 12:41 PM UTC

Hi Kapil, 

Thank you for updating us. 

We are glad that the issue is resolved at your end. Please let us know if you need further assistance. 

Regards, 
Abirami. 



KG kapil Gupta August 7, 2018 06:03 PM UTC

I want to create a tab delimited file using stream. I give Tab in  workbook.SaveAs(stream, "     "); and it is creating a simple text file.

Can we do in any other way?


PK Prakash Kumar D Syncfusion Team August 9, 2018 05:54 AM UTC

Hi Kapil, 
 
Thank you for contacting Syncfusion support. 
 
You can achieve your requirement by using the following code snippet. We have shared the KB link about tab delimited file for your reference. 
 
Code Snippet: 
 
Stream stream = File.Create("Output.tsv"); 
workbook.SaveAs(stream, "\t"); 
 
 
Regards, 
Prakash Kumar 



KG kapil Gupta August 9, 2018 09:11 AM UTC

Thanks for the reply,

When create file using .tsv extension. Excel not able to open in the correct format. I want a tab-delimited file with .txt extension.  As I want to process file dynamically with the third API which accepts .txt file with tab separator same as Microsoft excel do.



PK Prakash Kumar D Syncfusion Team August 9, 2018 09:51 AM UTC

Hi Kapil,  
 
Thank you for updating us. To achieve requirement, we suggest you save the file with .txt extension by using the following code snippet. 
  
Code Snippet:  
  
Stream stream = File.Create("Output.txt");  
workbook.SaveAs(stream, "\t");  
 
Please let us know if you need any further assistance. 
 
Regards, 
Prakash Kumar 



KG kapil Gupta August 9, 2018 12:20 PM UTC

Attach is the tab-delimited file exported using xlsio. When I open in excel, It comes in single columns and if I Open in notepad++. It comes with Tab-delimited file. why the file  not opening in excel in separate columns?


KG kapil Gupta August 9, 2018 12:22 PM UTC

please find the attachment.

Attachment: Region1OwnerAddress_09Aug18_170738_PM_1ded8ecf.zip


PK Prakash Kumar D Syncfusion Team August 10, 2018 12:40 PM UTC

Hi Kapil, 
 
 
Thank you for updating us. 
 
 
To resolve the reported issue, we request to pass the encoding type as default in parameter while saving the stream. Please find the code snippet below  
 
Code snippet: 
 
Stream stream = File.Create("Output.txt"); 
//Passing encoding type in parameter 
workbook.SaveAs(stream, "\t",Encoding.Default ); 
 
Regards, 
Prakash Kumar 


Loader.
Up arrow icon