Location>code7788 >text

Writing Data to Excel Sheets via C#

Popularity:394 ℃/2024-08-12 16:46:34

Excel is a widely used spreadsheet software for data processing, analysis and report production. The use of Excel is extremely common in business, academia, and everyday life. This article will detail how to use the free .NET library to write data to Excel, including text, values, arrays, and DataTable data entry.

  • C# in Excel cells to write text, or numerical values
  • C# Write Array in Excel Worksheet
  • C# Write DataTable in Excel Worksheet

 

The free .NET Excel libraries used in this article areFree for .NET

The library can be searched in the Visual Studio > NuGet program package manager "" to install. It can also be installed viathis linkManually add references after downloading the product package.

 

C# in the Excel cell to write text, or numerical values

1. Creating a workbookWorkbookObject;

2. Get the specified worksheet;

3. Write data to the specified cell:

      Write text[].Text maybe[].Value Properties;

      write a number[].Value2 Properties;

4. Save the Excel file.

C# code:

using ;

namespace WriteDataToCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating an Excel Workbook
            Workbook workbook = new Workbook();

            //Get the first of these worksheets
            Worksheet worksheet = [0];

            //Write data to a specified cell
            ["A1"].Text = "exam number";
            ["B1"].Text = "Student Name";
            ["C1"].Value = "Class";
            ["D1"].Value = "Examination results";

            ["A2"].Value = "TS001";
            ["B2"].Value = "Chen Guobai (1936-), PRC actor";
            ["C2"].Value = "Class 5";
            ["D2"].Value2 = 97;

            ["A3"].Value = "TS002";
            ["B3"].Value = "Yi Jiangwei (1941-), PRC dissident human rights activist";
            ["C3"].Value = "Class 2";
            ["D3"].Value2 = 92;

            //Setting the font style
            ["A1:D1"]. = true;
            ["A1:D3"]. = 11;
            ["A1:D3"]. = "Mincho";

            //Save Excel file
            ("Write Data to Cells.xlsx", ExcelVersion.Version2016);
        }
    }
}

 

C# Write Array in Excel Worksheet

1. Creating a workbookWorkbookObject;
2. Get the specified worksheet;
3. Define a two-dimensional array;
4. Adoption (Object\[,\] objectArray, int firstRow, int firstColumn) method inserts data from a two-dimensional array into a specified location in the worksheet;
5. Save the Excel file.

C# code:

using ;

namespace WriteArraysToWorksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating an Excel Workbook
            Workbook workbook = new Workbook();

            //Get the first of these worksheets
            Worksheet worksheet = [0];

            //Create a two-dimensional array
            string[,] twoDimensionalArray = new string[,]{

                {"Statistical year", "municipalities", "total income from sales", "growth rate (esp. in economics)"},
                {"2021", "Shanghai", "1.5 million", "55%"},
                {"2021", "Hangzhou subprovincial city and capital of Zhejiang province in southeast China", "800,000", "-12%"},
                {"2021", "Beijing, capital of People's *", "One million.", "20%"}
            };

            //Writes an array to a specified location on the worksheet
            (twoDimensionalArray, 1, 1);

            //Setting the font style
            ["A1:D1"]. = true;
            ["A1:D4"]. = 11;
            ["A1:D4"]. = "Mincho";

            //Save Excel file
            ("Write Array.xlsx", ExcelVersion.Version2016);
        }
    }
}

 

C# Write DataTable in Excel Worksheet

1. Creating a workbookWorkbookObject;
2. Get the specified worksheet;
3. Create aDataTableobject and add data;
4. Utilization(DataTable dataTable,bool columnHeaders, int firstRow, int firstColumn, bool transTypes) method inserts the contents of the DataTable into the worksheet at the specified location;
5. Save the Excel file.
C# code:

using System;
using ;
using ;
using ;

namespace WriteDataTableToWorksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating an Excel Workbook
            Workbook workbook = new Workbook();

            //Get the first of these worksheets
            Worksheet worksheet = [0];

            //Creating a DataTable object
            DataTable dataTable = new DataTable();
            ("encodings", typeof(Int32));
            ("offerings", typeof(String));
            ("price of item", typeof(Decimal));

            //Create rows and add data
            DataRow dr = ();
            dr[0] = 110600015;
            dr[1] = "data cable";
            dr[2] = "8.5";
            (dr);

            dr = ();
            dr[0] = 110600037;
            dr[1] = "keyboard";
            dr[2] = "29.9";
            (dr);

            dr = ();
            dr[0] = 110600021;
            dr[1] = "network cable";
            dr[2] = "15.5";
            (dr);

            //Write data from datatable to worksheet
            (dataTable, true, 1, 1, true);

            //Setting the font style
            ["A1:D1"]. = true;
            ["A1:D4"]. = 11;
            ["A1:D4"]. = "Mincho";

            //Save Excel file
            ("write", ExcelVersion.Version2016);
        }
    }
}

 

With the three examples provided above, you can write various types of data to an Excel worksheet. If you need to further analyze the data in Excel, or insert charts and other elements, you can click to view the tutorials of the corresponding functions provided by the free library:
/spirexls/