Is CSV supported in read and write

Hi,

I'm new and I'm trying to read/write CSV file, but when I write it and open it, I got that the file is corrupted,
How I can read/write CSV files?

Thanks


1 Reply

RS Ramya Sivakumar Syncfusion Team March 31, 2022 09:59 AM UTC

Hi Bashar,


Greetings from Syncfusion.


Yes, XlsIO provides support for read and write CSV files. Please check the sample and let us know if this is helpful.


Code Snippet:

using (ExcelEngine excelEngine = new ExcelEngine())

{

     IApplication application = excelEngine.Excel;

     application.DefaultVersion = ExcelVersion.Excel2013;

     //Create a workbook

     IWorkbook workbook = application.Workbooks.Create(1);

     IWorksheet worksheet = workbook.Worksheets[0];                 

 

     worksheet.Range["A1"].Text = "Customer ID";

     worksheet.Range["A2"].Text = "ALFKI";

     worksheet.Range["A3"].Text = "ANATR";

     worksheet.Range["A4"].Text = "ANTON";

     worksheet.Range["A5"].Text = "AROUT";

     worksheet.Range["A6"].Text = "BERGS";

     worksheet.Range["B1"].Text = "CompanyName";

     worksheet.Range["B2"].Text = "Alfreds Futterkiste";

     worksheet.Range["B3"].Text = "Ana Trujillo Emparedados y helados";

     worksheet.Range["B4"].Text = "Around the Horn";

     worksheet.Range["B5"].Text = "Blauer See Delikatessen";

     worksheet.Range["B6"].Text = "Blondesddsl père et fils";

 

     //Saving the Excel to the MemoryStream

     MemoryStream stream = new MemoryStream();

     workbook.SaveAs(stream,",");

 

     //Set the position as '0'.

     stream.Position = 0;

     //Download the Excel file in the browser

     FileStreamResult fileStreamResult = new FileStreamResult(stream, "text/csv");

     fileStreamResult.FileDownloadName = "Output.csv";

     return fileStreamResult;

}


Sample link - https://www.syncfusion.com/downloads/support/directtrac/general/ze/F-1740~1-975557169


GitHub Sample link - https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Worksheet%20Features/Read%20and%20Save%20CSV/NET%20Standard/Read%20and%20Save%20CSV/Read%20and%20Save%20CSV


Kindly look into the following links to know more about the CSV files.

https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet#open-a-csv-file
https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet#save-worksheet-as-csv
https://www.syncfusion.com/kb/1875/does-xlsio-support-csv-file-format



Regards,

Ramya.


Loader.
Up arrow icon