Export data as CSV or Excel file in C#

Export data as CSV or Excel file in C# is one of the widely used functionality in most of the applications. So here we’ll see how to export the data to CSV as well as in Excel file.  Exporting as csv file is a very easy task because cvs files doesn’t have any formatting and the data are separated by a comma. Even we can create a csv file using any text editor like notepad.

data

Exporting as csv file

Here not need any library or big logic. Just prepare the string and parse the string to the File object as UTF8 encoded text.

Note: If the data has some comma in the text, there will be an issues with the csv file formatting since csv file uses comma to separate the columns. So the easy way to avoid this issues is, put the text inside a double quotation.

 

Export as Excel file

Exporting as excel file can’t be done directly since excel file is too complected compere to text file and it has its own formatting. There are some libraries to do the work, and EPPlus is one of the stable library the do the job well.  EPPlus  supports Cell Ranges, Cell styling (Border, Color, Fill, Font, Number, Alignments), Charts, Pictures, Shapes, Comments, Tables, Protection, Encryption, Pivot tables, Data validation, Conditional formatting and etc. but here we are looking to add simple text with some special characters.

First download the dll files from the EPPlus and add to you project reference list.

 

Hope, you guys got some info about Export data as CSV or Excel file in C#.  If you have any question,, lest me know via comment. 🙂

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: