Seamlessly Import and Export CSV Data in Excel Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Seamlessly Import and Export CSV Data in Excel Using C#

Seamlessly Import and Export CSV Data in Excel Using C#

A comma-separated value (CSV) document is one of the most used formats for storing extensive data in a specific structure. It uses a comma (,) as the delimiter to separate each field in a row, and every row indicates a new record. Because CSV documents are text documents, many devices can read them without the need for extra software because they already have built-in applications that open and read text documents. Other document formats may require additional applications to open them.

Enjoy a smooth experience with Syncfusion’s Excel Library! Get started with a few lines of code and without Microsoft or interop dependencies.

CSV is sometimes called a character separator value document because the delimiter can be any character. However, the comma is most commonly used.

This article will show you how to import and export CSV data in your Excel files using the Syncfusion .NET Excel (XlsIO) library in C#.

Note: If you are new to our Excel Library, following the getting started guide is highly recommended. 

Import CSV data to Excel

As mentioned previously, CSV documents can be opened easily on any device, but they don’t provide data management features. In such cases, you can import the CSV data into Excel files, which support opening CSV files with other options like sorting, filtering, and exporting to other formats.

Syncfusion Excel Library also supports opening CSV and all types of delimited documents.

Refer to the following code example to import a CSV document and apply filters to the data using the Excel Library.

using Syncfusion.XlsIO;
using System;
using System.IO;

namespace CsvToExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
           using(ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                
                FileStream inputStream = new FileStream(@"../../../Data/PurchasedItems.csv", FileMode.Open,FileAccess.ReadWrite);

                //Opening CSV document with comma separator
                IWorkbook workbook = application.Workbooks.Open(inputStream, ",");

                IWorksheet worksheet = workbook.Worksheets[0];

                //Applying filters to the data
                IAutoFilters filters = worksheet.AutoFilters;
                filters.FilterRange = worksheet[1, 1, worksheet.UsedRange.LastRow, worksheet.UsedRange.LastColumn];

                IAutoFilter filter = filters[1];

                filter.AddTextFilter("Wednesday");

                //Saving the CSV data as Excel
                FileStream outputStream = new FileStream(@"PurchasedItems.xlsx", FileMode.Create, FileAccess.ReadWrite);

                workbook.SaveAs(outputStream);
            }
        }
    }
}

The following image shows the input CSV document.

Input CSV Document
Input CSV Document

Refer to the following image. It shows the output Excel document after applying filters to the imported CSV data using the Syncfusion Excel Library.

Applying Filters to the Imported CSV Data in an Excel Document
Applying Filters to the Imported CSV Data in an Excel Document

Syncfusion’s C# Excel Library is meticulously documented with a multitude of code examples. Working with Excel files has never been simpler than this.

Export Excel to CSV

Similarly, you can export the previous Excel document again as a CSV document and share it with users using the Excel Library.

Refer to the following code example.

using Syncfusion.XlsIO;
using System;
using System.IO;

namespace ExcelToCSV
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;

                FileStream inputStream = new FileStream(@"../../../Data/PurchasedItems.xlsx", FileMode.Open, FileAccess.ReadWrite);

                //Opening CSV document with comma separator
                IWorkbook workbook = application.Workbooks.Open(inputStream);

                //Saving the Excel data as CSV
                FileStream outputStream = new FileStream(@"PurchasedItems.csv", FileMode.Create, FileAccess.ReadWrite);


                workbook.SaveAs(outputStream,",");
            }
        }
    }
}

Refer to the following images.

Input Excel Document
Input Excel Document
Exporting Excel Data to CSV Document
Exporting Excel Data to CSV Document

Immerse yourself in practical examples spotlighting the extraordinary features of Syncfusion’s C# Excel Library!

Export CSV data with custom separator

In CSV documents, the commonly used separator is a comma (,), but this character can also be customized based on the user’s requirements.

Syncfusion Excel Library supports modifying the separator while saving a CSV document. However, the user must be aware of the separator character used in the CSV document before processing the document. Otherwise, the data cannot be read properly.

Refer to the following code example to export CSV data with a custom separator, a semicolon (;) in this case, using the Excel Library.

using Syncfusion.XlsIO;
using System;
using System.IO;

namespace CustomCsvSeparator
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;

                FileStream inputStream = new FileStream(@"../../../Data/PurchasedItems.csv", FileMode.Open, FileAccess.ReadWrite);

                //Opening CSV document with comma separator
                IWorkbook workbook = application.Workbooks.Open(inputStream, ",");

                
                //Saving the CSV data with separator as ";"
                FileStream outputStream = new FileStream(@"PurchasedItems.csv", FileMode.Create, FileAccess.ReadWrite);

                workbook.SaveAs(outputStream, ";");
            }
        }
    }
}

Refer to the following images.

Input CSV Document with Comma Separator
Input CSV Document with Comma Separator
Output CSV Document with Semicolon Separator
Output CSV Document with Semicolon Separator

GitHub reference

Check out our importing and exporting CSV data in Excel using C# demo on GitHub.

From simple data tables to complex financial models, Syncfusion empowers you to unleash your creativity and design stunning Excel spreadsheets.

Wrapping up

Thanks for reading! In this blog, we’ve seen how to import and export CSV data in C# using the Syncfusion Excel Library. Take a moment to peruse the documentation where you’ll find information on importing other data formats like DataTablecollection objects, DataView, DataColumn, and HTML, all with accompanying code samples.

Using the Excel Library, you can export Excel data to PDFimageDataTableCSVTSVHTMLcollection objectsODSJSON, and other file formats.

Are you already a Syncfusion user? You can download the product setup here. If you’re not a Syncfusion user, you can download a free 30-day trial.

Please let us know in the comments section below if you have any questions about these features. You can also contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed