Seamlessly Import and Export CSV Data in Excel Using C#
Tags: fileformats, csharp, excel, csv
Published: 2023-05-08 15:56:04

---
title: Seamlessly Import and Export CSV Data in Excel Using C#
published: true
date: 2023-05-08 10:40:13 UTC
tags: fileformats, csharp, excel, csv
canonical_url: https://www.syncfusion.com/blogs/post/import-export-csv-data-in-excel-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h58dcq9mo57xog6fubro.png
---
A [comma-separated value (CSV)](https://en.wikipedia.org/wiki/Comma-separated_values "Wikipedia Link: Comma-Separated Values") 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.

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](https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet#open-a-csv-file "import and export CSV data in Excel documentation") files using the [Syncfusion .NET Excel (XlsIO) library](https://www.syncfusion.com/document-processing/excel-framework/net "Syncfusion .NET Excel (XlsIO) library") in C#.

**Note:** If you are new to our Excel Library, following the [getting started guide](https://help.syncfusion.com/file-formats/xlsio/getting-started-create-excel-file-csharp-vbnet "Getting Started - Create Excel File in C# and VB.NET") 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.

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Input-CSV-Document-1.png" alt="Input CSV Document" style="width:100%">

  <figcaption>Input CSV Document</figcaption>

</figure>

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

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Applying-Filters-to-the-Imported-CSV-Data-in-an-Excel-Document-1.png" alt="Applying Filters to the Imported CSV Data in an Excel Document" style="width:100%">

  <figcaption>Applying Filters to the Imported CSV Data in an Excel Document</figcaption>

</figure>

## 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.

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Input-Excel-Document-1.png" alt="Input Excel Document" style="width:100%">

  <figcaption>Input Excel Document</figcaption>

</figure>

<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Exporting-Excel-Data-to-CSV-Document-1.png" alt="Exporting Excel Data to CSV Document" style="width:100%">

  <figcaption>Exporting Excel Data to CSV Document</figcaption>

</figure>

## 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.

```csharp
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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Input-CSV-Document-with-Comma-Separator-1.png" alt="Input CSV Document with Comma Separator" style="width:100%">

  <figcaption>Input CSV Document with Comma Separator</figcaption>

</figure>

<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/05/Output-CSV-Document-with-Semicolon-Separator-1.png" alt="Output CSV Document with Semicolon Separator" style="width:100%">

  <figcaption>Output CSV Document with Semicolon Separator</figcaption>

</figure>

## GitHub reference
Check out our [importing and exporting CSV data in Excel using C# demo on GitHub](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Create%20and%20Edit%20CSV "Importing and exporting CSV data in Excel using C# GitHub demo").

## 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](https://www.syncfusion.com/excel-framework/net "Syncfusion Excel Library"). Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-data-to-worksheets "Working with Excel data documentation") where you’ll find information on importing other data formats like [DataTable](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datatable "Importing data from DataTable"), [collection objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-collection-objects "Importing data from Collection Objects"), [DataView](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-dataview "Importing Data from DataView"), [DataColumn](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datacolumn "Importing data from DataColumn"), and [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-html-table-to-excel-worksheet "Importing HTML Table to Excel worksheet"), all with accompanying code samples.

Using the Excel Library, you can export Excel data to [PDF](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion "Excel to PDF conversion"), [image](https://help.syncfusion.com/file-formats/xlsio/worksheet-to-image-conversion "Worksheet to image conversion"), [DataTable](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-data-table "Exporting from Worksheet to DataTable"), [CSV](https://www.syncfusion.com/kb/9092/convert-excel-file-to-csv-in-c-vb-net "Convert Excel file to CSV"), [TSV](https://www.syncfusion.com/kb/8556/does-xlsio-support-tsv-files "Excel to TSV conversion"), [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet#save-worksheet-as-html "Save Worksheet as HTML"), [collection objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-collection-objects "Exporting from Worksheet to Collection Objects"), [ODS](https://help.syncfusion.com/file-formats/xlsio/excel-to-ods-conversion "Excel to ODS convertion"), [JSON](https://help.syncfusion.com/file-formats/xlsio/excel-to-json-conversion "Excel to JSON convertion"), and other file formats.

Are you already a Syncfusion user? You can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/ "Essential Studio license and downloads page"). If you’re not a Syncfusion user, you can download a [free 30-day trial](https://www.syncfusion.com/downloads/fileformats/confirm "Free evaluation of the Essential Studio Products").

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](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/excel "Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [6 Easy Ways to Export Data to Excel in C#](https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx "Blog: 6 Easy Ways to Export Data to Excel in C#")
- [How to Export Data from SQL Server to Excel Table in C#](https://www.syncfusion.com/blogs/post/export-data-from-sql-server-to-excel-in-c-sharp.aspx "Blog: How to Export Data from SQL Server to Excel Table in C#")
- [Export Data from Collection to Excel and Group It in C# ](https://www.syncfusion.com/blogs/post/export-data-from-collection-to-excel-and-group-csharp.aspx "Blog: Export Data from Collection to Excel and Group It in C#")
- [Export Data to a Predefined Excel Template in C#](https://www.syncfusion.com/blogs/post/export-data-to-a-predefined-excel-template-in-c.aspx "Blog: Export Data to a Predefined Excel Template in C#")
- [Easy Steps to Export HTML Tables to an Excel Worksheet in C#](https://www.syncfusion.com/blogs/post/easy-steps-to-export-html-tables-to-an-excel-worksheet-in-c.aspx "Blog: Easy Steps to Export HTML Tables to an Excel Worksheet in C#")