HOWTO · Csharp

How to Export Data to an Excel File Using C#

Compare ways to prepare C# object collections, arrays, DataTables, and JSON records for an Excel workbook.

On this page

Choose the export path that matches your input: map an object collection to a worksheet, place a two-dimensional array into cells, write a DataTable with its column names, or deserialize JSON records before mapping their fields. Each approach below describes the intended workbook layout. The library-specific fragments are unverified illustrations: no target framework, package versions, or execution evidence is supplied. Check the current vendor documentation for compatible packages, API signatures, and license terms before adapting them.

Export Data to Excel Using C#

For student-like records, start with a collection whose objects expose the values you want as columns. For example, each Student can have a name property and a numeric marks property. A workbook row should represent one student, with a header row identifying those two fields. This shape is convenient when the application already works with typed records and you want the worksheet to reflect their properties.

The original illustration names ExcelMapper and the ExcelMapper NuGet package. Treat that as a library choice to investigate, not verified setup guidance. Check the package’s vendor documentation for its current installation method and the supported way to map a collection, select a worksheet such as ShaniData, and save an .xlsx file. Supply a writable output path for the workbook. The intended result is a sheet containing the student names and marks; no output workbook or screenshot is verified here.

Export Data From Array to Excel Using C#

A two-dimensional array works well when the values already form a rectangular table. Put field names in its first row, such as Name, City, Gpa, and Language, then put one record in each subsequent row. Every row should have the same number of values so each item stays under its intended heading.

The supplied illustration uses Spire.Xls, creates a worksheet named ShaniArray, and calls InsertArray with row and column arguments of 2 and 1. That describes an intended starting position of the second worksheet row and first column; verify the API’s indexing convention in the current documentation before relying on it. With a header row in the array, starting on row two leaves the first worksheet row available for another title or blank space. The example’s original package heading said ExcelMapper, which did not match its Spire.Xls references; use documentation for the package actually selected. Its intended result is the header-and-record grid saved as an Excel workbook.

Export Data From DataTable to Excel Using C#

Use a DataTable when column names and rows are already represented in .NET’s tabular data model. Define columns such as Roll Number, Student Name, and Section, then add a DataRow for each student. The worksheet should show the column names as its first row and the table’s values beneath them. Confirm that each row supplies a value for each declared column.

The article’s illustration references Spire.Xls and intends to insert the table with its column names included. Its package instructions incorrectly named ExcelMapper; consult the chosen library’s documentation for the matching package and its current table-insertion API. The insertion arguments and save call are not verified here, so check their meaning, worksheet coordinates, and output path for your installed version. This approach is useful when a query or existing application logic already produces a DataTable and you want its named columns preserved in the workbook.

Export Data From JSON to Excel Using C#

The sample data is an inline JSON string, not a JSON file. It contains numbered student records with Name, Roll, GPA, and Fyp fields. Deserialize that string into a collection or dictionary of student objects, then map those fields to worksheet columns such as Student Name, Roll No, GPA, and FYP. A JSON file would require a separate file-reading step before deserialization; that step is not part of this illustration.

The source names Newtonsoft.Json for deserialization and GemBox.Spreadsheet for workbook creation. Keep the responsibilities distinct: initialize the workbook using the selected spreadsheet library, create the ShaniJSON worksheet, write the headings, then add each record below them and save the file. In the original mapping, the JSON key Name is associated with a C# property named Fname; ensure the serializer configuration matches the actual property names you use. The source alternated between Workbook and ExcelFile, so verify the workbook type and save method against the current GemBox documentation instead of combining incompatible fragments. Check its license terms and the JSON package’s compatibility for your project. No compilation, runtime result, or generated workbook is claimed.