{"id":23060875,"url":"https://github.com/syncfusionexamples/export-data-to-excel-in-c-sharp","last_synced_at":"2025-10-25T13:36:21.729Z","repository":{"id":98095134,"uuid":"185202570","full_name":"SyncfusionExamples/export-data-to-excel-in-c-sharp","owner":"SyncfusionExamples","description":"This repository contains Syncfusion Excel library examples that explain how to export data to Excel in C#, from the data table, collection of objects, database, Microsoft Grid controls, array, and CSV. This works without Microsoft Excel Interop.","archived":false,"fork":false,"pushed_at":"2024-12-02T11:47:06.000Z","size":731,"stargazers_count":37,"open_issues_count":2,"forks_count":18,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-12-02T12:31:53.758Z","etag":null,"topics":["array-to-excel","collections-to-excel","csv-to-excel","database-to-excel","datargridview-to-excel","datatable-to-excel","excel","excel-library","export-excel","xlsio"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SyncfusionExamples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-06T13:29:35.000Z","updated_at":"2024-11-18T02:54:26.000Z","dependencies_parsed_at":"2024-12-02T12:37:27.022Z","dependency_job_id":null,"html_url":"https://github.com/SyncfusionExamples/export-data-to-excel-in-c-sharp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyncfusionExamples%2Fexport-data-to-excel-in-c-sharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyncfusionExamples%2Fexport-data-to-excel-in-c-sharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyncfusionExamples%2Fexport-data-to-excel-in-c-sharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyncfusionExamples%2Fexport-data-to-excel-in-c-sharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SyncfusionExamples","download_url":"https://codeload.github.com/SyncfusionExamples/export-data-to-excel-in-c-sharp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229901552,"owners_count":18141741,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array-to-excel","collections-to-excel","csv-to-excel","database-to-excel","datargridview-to-excel","datatable-to-excel","excel","excel-library","export-excel","xlsio"],"created_at":"2024-12-16T03:15:34.510Z","updated_at":"2025-10-25T13:36:16.683Z","avatar_url":"https://github.com/SyncfusionExamples.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Export Data to Excel in C#\n\u003ca href=\"https://www.syncfusion.com/excel-framework/net\"\u003e\u003cstrong\u003eSyncfusion\u0026reg; Excel (XlsIO) library\u003c/strong\u003e\u003c/a\u003e is a .NET Excel library that allows the user to export data to Excel in C# and VB.NET from various data sources like data tables, arrays, collections of objects, databases, CSV/TSV, and Microsoft Grid controls in a very simple and easy way. Exporting data to Excel helps in visualizing the data in a more understandable fashion. This feature helps to generate financial reports, banking statements, and invoices, while also allowing for filtering large data, validating data, formatting data, and more.\n\nYou can refer the \u003ca href=\"https://help.syncfusion.com/file-formats/xlsio/working-with-data?_ga=2.120276040.1381167263.1557135100-214292665.1551328372#importing-data-to-worksheets\"\u003edocumention\u003c/a\u003e to know more in detail.\n\nEssential\u0026reg; XlsIO provides the following ways to export data to Excel:\n\n1. DataTable to Excel\n2. Collection of objects to Excel\n3. Database to Excel\n4. Microsoft Grid controls to Excel\n5. Array to Excel\n6. CSV to Excel\n\nThis repository contains the examples of these methods and explains how to implement them.\n\n\u003ch2 id=\"DataTable-to-Excel\"\u003e1. Export from DataTable to Excel\u003c/h2\u003e\n\nData from \u003ca href=\"https://en.wikipedia.org/wiki/ADO.NET\"\u003eADO.NET\u003c/a\u003e objects such as \u003ca href=\"https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/datatables\"\u003edatatable\u003c/a\u003e, \u003ca href=\"https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn\"\u003edatacolumn\u003c/a\u003e, and \u003ca href=\"https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/dataviews\"\u003edataview\u003c/a\u003e can be exported to Excel worksheets. The exporting can be done as column headers, by recognizing column types or cell value types, as hyperlinks, and as large dataset, all in a few seconds.\n\n\u003cstrong\u003eExporting DataTable to Excel\u003c/strong\u003e worksheets can be achieved through the \u003ca href=\"https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datatable\"\u003eImportDataTable\u003c/a\u003e method. The following code sample shows how to export a datatable of employee details to an Excel worksheet.\n\n```csharp\nusing (ExcelEngine excelEngine = new ExcelEngine())\n{\n    IApplication application = excelEngine.Excel;\n    application.DefaultVersion = ExcelVersion.Excel2016;\n \n    //Preserve data types as per the value\n    application.PreserveCSVDataTypes = true;\n \n    //Read the CSV file\n    Stream csvStream = File.OpenRead(Path.GetFullPath(@\"../../../TemplateSales.csv\")); ;\n \n    //Reads CSV stream as a workbook\n    IWorkbook workbook = application.Workbooks.Open(csvStream);\n    IWorksheet sheet = workbook.Worksheets[0];\n \n    //Formatting the CSV data as a Table \n    IListObject table = sheet.ListObjects.Create(\"SalesTable\", sheet.UsedRange);\n    table.BuiltInTableStyle =  TableBuiltInStyles.TableStyleMedium6;\n    IRange location = table.Location;\n    location.AutofitColumns();\n \n    //Apply the proper latitude \u0026 longitude numerformat in the table\n    TryAndUpdateGeoLocation(table,\"Latitude\");\n    TryAndUpdateGeoLocation(table,\"Longitude\");\n \n    //Apply currency numberformat in the table column 'Price'\n    IRange columnRange = GetListObjectColumnRange(table,\"Price\");\n    if(columnRange != null)\n        columnRange.CellStyle.NumberFormat = \"$#,##0.00\";\n \n    //Apply Date time numberformat in the table column 'Transaction_date'\n    columnRange = GetListObjectColumnRange(table,\"Transaction_date\");\n    if(columnRange != null)\n        columnRange.CellStyle.NumberFormat = \"m/d/yy h:mm AM/PM;@\";\n \n    //Sort the data based on 'Products'\n    IDataSort sorter = table.AutoFilters.DataSorter;\n    ISortField sortField = sorter. SortFields. Add(0, SortOn. Values, OrderBy. Ascending);\n    sorter. Sort();\n \n    //Save the file in the given path\n    Stream excelStream;\n    excelStream = File.Create(Path.GetFullPath(@\"../../../Output.xlsx\"));\n    workbook.SaveAs(excelStream);\n    excelStream.Dispose();\n}\n```\n\n\u003cimg class=\"aligncenter wp-image-38671 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/DataTable-to-Excel_900x177.png\" sizes=\"(max-width: 900px) 100vw, 900px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/DataTable-to-Excel_900x177.png 900w, https://blog.syncfusion.com/wp-content/uploads/2019/04/DataTable-to-Excel_900x177-300x59.png 300w, https://blog.syncfusion.com/wp-content/uploads/2019/04/DataTable-to-Excel_900x177-768x151.png 768w\" alt=\"Export DataTable to Excel in C#\" width=\"900\" height=\"177\" /\u003e\n\n\u003cem\u003eOutput of DataTable to Excel\u003c/em\u003e\n\n\nWhen exporting large data to Excel, and if there is no need to apply number formats and styles, you can make use of the \u003ca href=\"https://help.syncfusion.com/cr/cref_files/windowsforms/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.IWorksheet~ImportDataTable(DataTable,Int32,Int32,Boolean).html\"\u003eImportDataTable\u003c/a\u003e overload with the TRUE value for \u003cem\u003eimportOnSave\u003c/em\u003e parameter. Here, the export happens while saving the Excel file.\n\nUse this option to export large data with high performance.\n\n```csharp\nvalue = instance.ImportDataTable(dataTable, firstRow, firstColumn, importOnSave)\n```\n\nIf you have a named range and like to \u003ca href=\"https://help.syncfusion.com/cr/cref_files/windowsforms/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.IWorksheet~ImportDataTable(DataTable,IName,Boolean,Int32,Int32).html\"\u003eexport data to a named range\u003c/a\u003e from a specific row and column of the named range, you can make use of the below API, where rowOffset and columnOffset are the parameters to import from a particular cell in a named range.\n\n```csharp\nvalue = instance.ImportDataTable(dataTable, namedRange, showColumnName, rowOffset, colOffset);\n```\n\n\u003ch2 id=\"Collections-to-Excel\"\u003e2. Export from collection of objects to Excel\u003c/h2\u003e\n\nExporting data from a collection of objects to an Excel worksheet is a common scenario. However, this option will be helpful if you need to export data from a model to an Excel worksheet.\n\nThe Syncfusion\u0026reg; Excel (XlsIO) library provides support to export data from a collection of objects to an Excel worksheet.\n\nExporting data from a collection of objects to an Excel worksheet can be achieved through the ImportData method. The following code example shows how to export data from a collection to an Excel worksheet.\n\n```csharp\nusing (ExcelEngine excelEngine = new ExcelEngine())\n{\n    IApplication application = excelEngine.Excel;\n    application.DefaultVersion = ExcelVersion.Excel2016;\n\n    //Read the data from XML file\n    StreamReader reader = new StreamReader(Path.GetFullPath(@\"../../Data/Customers.xml\"));\n\n    //Assign the data to the customerObjects collection\n    IEnumerable customerObjects = GetData (reader.ReadToEnd());  \n\n    //Create a new workbook\n    IWorkbook workbook = application.Workbooks.Create(1);\n    IWorksheet sheet = workbook.Worksheets[0];\n\n    //Import data from customerObjects collection\n    sheet.ImportData(customerObjects, 5, 1, false);\n\n    #region Define Styles\n    IStyle pageHeader = workbook.Styles.Add(\"PageHeaderStyle\");\n    IStyle tableHeader = workbook.Styles.Add(\"TableHeaderStyle\");\n\n    pageHeader.Font.RGBColor = Color.FromArgb(0, 83, 141, 213);\n    pageHeader.Font.FontName = \"Calibri\";\n    pageHeader.Font.Size = 18;\n    pageHeader.Font.Bold = true;\n    pageHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;\n    pageHeader.VerticalAlignment = ExcelVAlign.VAlignCenter;\n\n    tableHeader.Font.Color = ExcelKnownColors.White;\n    tableHeader.Font.Bold = true;\n    tableHeader.Font.Size = 11;\n    tableHeader.Font.FontName = \"Calibri\";\n    tableHeader.HorizontalAlignment = ExcelHAlign.HAlignCenter;\n    tableHeader.VerticalAlignment = ExcelVAlign.VAlignCenter;\n    tableHeader.Color = Color.FromArgb(0, 118, 147, 60);\n    tableHeader.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;\n    tableHeader.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;\n    tableHeader.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;\n    tableHeader.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;\n    #endregion\n\n    #region Apply Styles\n    //Apply style to the header\n    sheet[\"A1\"].Text = \"Yearly Sales Report\";\n    sheet[\"A1\"].CellStyle = pageHeader;\n\n    sheet[\"A2\"].Text = \"Namewise Sales Comparison Report\";\n    sheet[\"A2\"].CellStyle = pageHeader;\n    sheet[\"A2\"].CellStyle.Font.Bold = false;\n    sheet[\"A2\"].CellStyle.Font.Size = 16;\n\n    sheet[\"A1:D1\"].Merge();\n    sheet[\"A2:D2\"].Merge();\n    sheet[\"A3:A4\"].Merge();\n    sheet[\"D3:D4\"].Merge();\n    sheet[\"B3:C3\"].Merge();\n\n    sheet[\"B3\"].Text = \"Sales\";\n    sheet[\"A3\"].Text = \"Sales Person\";\n    sheet[\"B4\"].Text = \"January - June\";\n    sheet[\"C4\"].Text = \"July - December\";\n    sheet[\"D3\"].Text = \"Change(%)\";\n    sheet[\"A3:D4\"].CellStyle = tableHeader;\n    sheet.UsedRange.AutofitColumns();\n    sheet.Columns[0].ColumnWidth = 24;\n    sheet.Columns[1].ColumnWidth = 21;\n    sheet.Columns[2].ColumnWidth = 21;\n    sheet.Columns[3].ColumnWidth = 16;\n    #endregion\n\n    sheet.UsedRange.AutofitColumns();\n\n    //Save the file in the given path\n    Stream excelStream = File.Create(Path.GetFullPath(@\"Output.xlsx\"));\n    workbook.SaveAs(excelStream);\n    excelStream.Dispose();\n}\n```\n\n\u003cimg class=\"aligncenter wp-image-38681 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Collection-to-Excel.png\" sizes=\"(max-width: 494px) 100vw, 494px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Collection-to-Excel.png 494w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Collection-to-Excel-300x291.png 300w\" alt=\"Export collection of objects to Excel in c#\" width=\"494\" height=\"479\" /\u003e\n\n\u003cem\u003eOutput of collection of objects to Excel\u003c/em\u003e\n\n\u003ch2 id=\"Database-to-Excel\"\u003e3. Export from Database to Excel\u003c/h2\u003e\n\nExcel supports creating Excel tables from different databases. If you have a scenario in which you need to create one or more Excel tables from a database using Excel, you need to establish every single connection to create those tables. This can be time consuming, so if you find an alternate way to generate Excel tables from database very quickly and easily, wouldn’t that be your first choice?\n\nThe Syncfusion\u0026reg; Excel (XlsIO) library helps you to export data to Excel worksheets from databases like MS SQL, MS Access, Oracle, and more. By establishing a connection between the databases and Excel application, you can export data from a \u003cstrong\u003edatabase to an Excel table\u003c/strong\u003e.\n\nYou can use the \u003cem\u003e\u003ca href=\"https://help.syncfusion.com/cr/file-formats/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.IListObject~Refresh.html\"\u003eRefresh()\u003c/a\u003e\u003c/em\u003e\u003cem\u003e \u003c/em\u003eoption to update the modified data in the Excel table that is mapped to the database.\n\nAbove all, you can refer to the documentation to \u003cu\u003e\u003ca href=\"https://help.syncfusion.com/file-formats/xlsio/working-with-tables?cs-save-lang=1\u0026amp;cs-lang=csharp#create-a-table-from-external-connection\"\u003ecreate a table from an external connection\u003c/a\u003e\u003c/u\u003e to learn more about how to export databases to Excel tables. The following code sample shows how to export data from a database to an Excel table.\n\n```csharp\nusing (ExcelEngine excelEngine = new ExcelEngine())\n{\n    IApplication application = excelEngine.Excel;\n    application.DefaultVersion = ExcelVersion.Excel2016;\n\n    //Create a new workbook\n    IWorkbook workbook = application.Workbooks.Create(1);\n    IWorksheet sheet = workbook.Worksheets[0];\n                    \n    if(sheet.ListObjects.Count == 0)\n    {\n        //Estabilishing the connection in the worksheet\n        string dBPath = Path.GetFullPath(@\"../../Data/EmployeeData.mdb\");\n        string ConnectionString = \"OLEDB;Provider=Microsoft.JET.OLEDB.4.0;Password=\\\"\\\";User ID=Admin;Data Source=\"+ dBPath;\n        string query = \"SELECT EmployeeID,FirstName,LastName,Title,HireDate,Extension,ReportsTo FROM [Employees]\";\n        IConnection Connection = workbook.Connections.Add(\"Connection1\", \"Sample connection with MsAccess\", ConnectionString, query, ExcelCommandType.Sql);\n        sheet.ListObjects.AddEx(ExcelListObjectSourceType.SrcQuery, Connection, sheet.Range[\"A1\"]);\n    }\n\n    //Refresh Excel table to get updated values from database\n    sheet.ListObjects[0].Refresh();\n\n    sheet.UsedRange.AutofitColumns();\n                    \n    //Save the file in the given path\n    Stream excelStream = File.Create(Path.GetFullPath(@\"Output.xlsx\"));\n    workbook.SaveAs(excelStream);\n    excelStream.Dispose();\n}\n```\n\n\u003cimg class=\"aligncenter wp-image-38692 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Database-to-Excel-1.png\" sizes=\"(max-width: 736px) 100vw, 736px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Database-to-Excel-1.png 736w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Database-to-Excel-1-300x244.png 300w\" alt=\"Export Database to Excel in c#\" width=\"736\" height=\"599\" /\u003e\n\n\u003cem\u003eOutput of Database to Excel table\u003c/em\u003e\n\n\u003ch2 id=\"DataGrid-GridView-DataGridView-to-Excel\"\u003e4. Export data from DataGrid, GridView, DatGridView to Excel\u003c/h2\u003e\n\nExporting data from \u003ca href=\"https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/datagrid-control-overview-windows-forms\"\u003eMicrosoft grid\u003c/a\u003e controls to Excel worksheets helps to visualize data in different ways. You may work for hours to iterate data and its styles from grid cells to export them into Excel worksheets. It should be good news for those who export data from Microsoft grid controls to Excel worksheets, because exporting with Syncfusion\u0026reg; Excel library is much faster.\n\nSyncfusion\u0026reg; Excel (XlsIO) library supports to \u003ca href=\"https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-data-from-microsoft-grid-controls-to-worksheet\"\u003eexporting data from Microsoft Grid controls\u003c/a\u003e, such as DataGrid, GridView, and DataGridView to Excel worksheets in a single API call. Also, you can export data with header and styles.\n\nThe following code example shows how to export data from DataGridView to an Excel worksheet.\n\n```csharp\n#region Loading the data to DataGridView\nDataSet customersDataSet = new DataSet();\n\n//Read the XML file with data\nstring inputXmlPath = Path.GetFullPath(@\"../../Data/Employees.xml\");\ncustomersDataSet.ReadXml(inputXmlPath);\nDataTable dataTable = new DataTable();\n            \n//Copy the structure and data of the table\ndataTable = customersDataSet.Tables[1].Copy();\n            \n//Removing unwanted columns\ndataTable.Columns.RemoveAt(0);\ndataTable.Columns.RemoveAt(10);\nthis.dataGridView1.DataSource = dataTable;\n\ndataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;\ndataGridView1.RowsDefaultCellStyle.BackColor = Color.LightBlue;\ndataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font(\"Tahoma\", 9F, ((System.Drawing.FontStyle)(System.Drawing.FontStyle.Bold)));\ndataGridView1.ForeColor = Color.Black;\ndataGridView1.BorderStyle = BorderStyle.None;\n#endregion\n\nusing (ExcelEngine excelEngine = new ExcelEngine())\n{\n    IApplication application = excelEngine.Excel;\n\n    //Create a workbook with single worksheet\n    IWorkbook workbook = application.Workbooks.Create(1);\n\n    IWorksheet worksheet = workbook.Worksheets[0];\n\n    //Import from DataGridView to worksheet\n    worksheet.ImportDataGridView(dataGridView1, 1, 1, isImportHeader: true, isImportStyle: true);\n\n    worksheet.UsedRange.AutofitColumns();\n    workbook.SaveAs(\"Output.xlsx\");\n}\n```\n\n\u003cimg class=\"aligncenter wp-image-38685 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Grid-to-Excel.png\" sizes=\"(max-width: 970px) 100vw, 970px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Grid-to-Excel.png 970w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Grid-to-Excel-300x182.png 300w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Grid-to-Excel-768x466.png 768w\" alt=\"Export Microsoft DataGridView to Excel in c#\" width=\"970\" height=\"588\" /\u003e\n\n\u003cem\u003eMicrosoft DataGridView to Excel\u003c/em\u003e\n\n\u003ch2 id=\"Array-to-Excel\"\u003e5. Export from array to Excel\u003c/h2\u003e\n\nSometimes, there may be a need where an array of data may need to be inserted or modified into existing data in Excel worksheet. In this case, the number of rows and columns are known in advance. Arrays are useful when you have a fixed size.\n\nThe Syncfusion\u0026reg; Excel (XlsIO) library provides support to export an array of data into an Excel worksheet, both horizontally and vertically. In addition, two-dimensional arrays can also be exported.\n\nLet us consider a scenario, “Expenses per Person.” The expenses of a person for the whole year is tabulated in the Excel worksheet. In this scenario, you need to add expenses for a new person, \u003cem\u003ePaul Pogba,\u003c/em\u003e in a new row and modify the expenses of all tracked people for the month \u003cem\u003eDec\u003c/em\u003e.\n\n\u003cimg class=\"aligncenter wp-image-38677 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_1.png\" sizes=\"(max-width: 982px) 100vw, 982px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_1.png 982w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_1-300x90.png 300w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_1-768x231.png 768w\" alt=\"Excel data before exporting from array to Excel in c#\" width=\"982\" height=\"296\" /\u003e\n\n\u003cem\u003eExcel data before exporting from array\u003c/em\u003e\n\nExporting an array of data to Excel worksheet can be achieved through the \u003ca href=\"https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-array\"\u003eImportArray\u003c/a\u003e method. The following code sample shows how to \u003cstrong\u003eexport an array of data to an Excel\u003c/strong\u003e worksheet, both horizontally and vertically.\n\n```csharp\nusing (ExcelEngine excelEngine = new ExcelEngine())\n{\n    IApplication application = excelEngine.Excel;\n    application.DefaultVersion = ExcelVersion.Excel2016;\n\n    //Reads input Excel stream as a workbook\n    IWorkbook workbook = application.Workbooks.Open(File.OpenRead(Path.GetFullPath(@\"../../../Expenses.xlsx\")));\n    IWorksheet sheet = workbook.Worksheets[0];\n\n    //Preparing first array with different data types\n    object[] expenseArray = new object[14]\n    {\"Paul Pogba\", 469.00d, 263.00d, 131.00d, 139.00d, 474.00d, 253.00d, 467.00d, 142.00d, 417.00d, 324.00d, 328.00d, 497.00d, \"=SUM(B11:M11)\"};\n\n    //Inserting a new row by formatting as a previous row.\n    sheet.InsertRow(11, 1, ExcelInsertOptions.FormatAsBefore);\n\n    //Import Peter's expenses and fill it horizontally\n    sheet.ImportArray(expenseArray, 11, 1, false);\n\n    //Preparing second array with double data type\n    double[] expensesOnDec = new double[6]\n    {179.00d, 298.00d, 484.00d, 145.00d, 20.00d, 497.00d};\n\n    //Modify the December month's expenses and import it vertically\n    sheet.ImportArray(expensesOnDec, 6, 13, true);\n\n    //Save the file in the given path\n    Stream excelStream = File.Create(Path.GetFullPath(@\"Output.xlsx\"));\n    workbook.SaveAs(excelStream);\n    excelStream.Dispose();\n}\n```\n\n\u003cimg class=\"aligncenter wp-image-38678 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_2.png\" sizes=\"(max-width: 976px) 100vw, 976px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_2.png 976w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_2-300x98.png 300w, https://blog.syncfusion.com/wp-content/uploads/2019/04/Array-to-Excel_2-768x250.png 768w\" alt=\"Export array of data to Excel in c#\" width=\"976\" height=\"318\" /\u003e\n\n\u003cem\u003eOutput of array of data to Excel\u003c/em\u003e\n\n\u003ch2 id=\"CSV-to-Excel\"\u003e6. Export from CSV to Excel\u003c/h2\u003e\n\n\u003ca href=\"https://en.wikipedia.org/wiki/Comma-separated_values\"\u003eComma-separated value\u003c/a\u003e (CSV) files are helpful in generating tabular data or lightweight reports with few columns and a high number of rows. Excel opens such files to make the data easier to read.\n\nThe Syncfusion\u0026reg; Excel (XlsIO) library supports opening and saving CSV files in seconds. The below code example shows how to open a CSV file, also save it as XLSX file. Above all, the data is shown in a table with number formats applied.\n\n```csharp\nusing (ExcelEngine excelEngine = new ExcelEngine())\n{\n    IApplication application = excelEngine.Excel;\n    application.DefaultVersion = ExcelVersion.Excel2016;\n\n    //Preserve data types as per the value\n    application.PreserveCSVDataTypes = true;\n\n    //Read the CSV file\n    Stream csvStream = File.OpenRead(Path.GetFullPath(@\"../../../TemplateSales.csv\")); ;\n\n    //Reads CSV stream as a workbook\n    IWorkbook workbook = application.Workbooks.Open(csvStream);\n    IWorksheet sheet = workbook.Worksheets[0];\n\n    //Formatting the CSV data as a Table\n    IListObject table = sheet.ListObjects.Create(\"SalesTable\", sheet.UsedRange);\n    table.BuiltInTableStyle =  TableBuiltInStyles.TableStyleMedium6;\n    IRange location = table.Location;\n    location.AutofitColumns();\n\n    //Apply the proper latitude \u0026 longitude numerformat in the table\n    TryAndUpdateGeoLocation(table,\"Latitude\");\n    TryAndUpdateGeoLocation(table,\"Longitude\");\n\n    //Apply currency numberformat in the table column 'Price'\n    IRange columnRange = GetListObjectColumnRange(table,\"Price\");\n    if(columnRange != null)\n        columnRange.CellStyle.NumberFormat = \"$#,##0.00\";\n\n    //Apply Date time numberformat in the table column 'Transaction_date'\n    columnRange = GetListObjectColumnRange(table,\"Transaction_date\");\n    if(columnRange != null)\n        columnRange.CellStyle.NumberFormat = \"m/d/yy h:mm AM/PM;@\";\n\n    //Sort the data based on 'Products'\n    IDataSort sorter = table.AutoFilters.DataSorter;\n    ISortField sortField = sorter. SortFields. Add(0, SortOn. Values, OrderBy. Ascending);\n    sorter. Sort();\n\n    //Save the file in the given path\n    Stream excelStream;\n    excelStream = File.Create(Path.GetFullPath(@\"../../../Output.xlsx\"));\n    workbook.SaveAs(excelStream);\n    excelStream.Dispose();\n}\n```\n\n\u003cimg class=\"aligncenter wp-image-38683 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Input.png\" sizes=\"(max-width: 949px) 100vw, 949px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Input.png 949w, https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Input-300x182.png 300w, https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Input-768x467.png 768w\" alt=\"Input CSV File\" width=\"949\" height=\"577\" /\u003e\n\n\u003cem\u003eInput CSV file\u003c/em\u003e\n\n\u003cimg class=\"aligncenter wp-image-38684 size-full\" src=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Output.png\" sizes=\"(max-width: 1002px) 100vw, 1002px\" srcset=\"https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Output.png 1002w, https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Output-300x156.png 300w, https://blog.syncfusion.com/wp-content/uploads/2019/04/CSV-to-Excel_Output-768x399.png 768w\" alt=\"Export CSV to Excel in c#\" width=\"1002\" height=\"521\" /\u003e\n\n\u003cem\u003eOutput of CSV converted to Excel\u003c/em\u003e\n\nApart from this, \u003ca href=\"https://www.syncfusion.com/excel-framework/net\"\u003eSyncfusion\u0026reg; Excel (XlsIO) library\u003c/a\u003e provides various other features such as, charts, pivot tables, tables, cell formatting, conditional formatting, data validation, encryption and decryption, auto-shapes, Excel to PDF, Excel to Image, Excel to HTML and more. You can refer to our \u003ca href=\"https://ej2.syncfusion.com/aspnetcore/XlsIO/Create#/material\"\u003eonline demo samples\u003c/a\u003e to know more about all the features. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyncfusionexamples%2Fexport-data-to-excel-in-c-sharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyncfusionexamples%2Fexport-data-to-excel-in-c-sharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyncfusionexamples%2Fexport-data-to-excel-in-c-sharp/lists"}