[C#]C# uses Spire.XLS to read and write Excel files

First of all, you need to add Spire.XLS         from the NuGet package . You can choose FreeSpire, which is the same as the official version except that the number of read lines is limited.

         Then add a reference to the code:

using Spire.Xls;

1. Read EXCEL

        First, you need to use the following code to read the content of the table, including modifying the table, you also need to read it first. When reading, you need to specify the file name and table name .

Workbook workbook = new Workbook();    //创建对象
workbook.LoadFromFile(FilePath);       //从文件路径加载内容
Worksheet sheet1 = workbook.Worksheets[0];    //获取第一张表

        At this point, you can directly use the output function to output the data as a DataTable for subsequent operations. However, there is a limitation of using this method that the table header cannot have duplicate items (the first line defaults to the item of DataTable, and the item name of DataTable does not allow duplication)

DataTable dt = sheet1.ExportDataTable();

        If similar incompatibility occurs, you can unpack the read list by yourself, and then analyze it according to the data. Here is a DataTable generation method I wrote myself

                DataTable dt = new DataTable();
                int index = 0;
                foreach (var row in sheet1.Rows)
                {
                    if (index == 0)
                        foreach (var column in row)
                        {
                            string Name = column.Value.ToString();
                            dt.Columns.Add(Name);   //添加表头
                        }
                    else
                    {
                        DataRow dr = dt.NewRow();
                        int i = 0;
                        foreach (var column in row)
                        {
                            dr[i]= column.Value.ToString(); //添加内容
                            i++;
                        }
                        dt.Rows.Add(dr);
                    }
                    index++;
                }

        Of course, Spire can also read a lot of data, such as hyperlinks in the table, etc., if necessary, you can study it yourself

2. Write to Excel

         Here is an example of a traversal write method, you can modify the method according to your own needs

                //加载对象
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(FilePath);
                //选择第一个sheet
                Worksheet sheet1 = workbook.Worksheets[0];
                int Index = 0;
                foreach (var item in News)
                {
                    sheet1.Range[Index, 1].Text = (Index + 1).ToString();  //输出序号
                    Index++;
                }
                workbook.SaveToFile(FilePath, ExcelVersion.Version2010);

        It should be noted that the cell number in Range does not start from 0 regardless of row or column . If you fill in 0, an array out-of-bounds warning will appear

        If you are creating a new table, you don't need to use LoadFromFile, just do new directly:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

//操作代码

workbook.SaveToFile(FilePath,ExcelVersion.Version2010);

Guess you like

Origin blog.csdn.net/weixin_37878740/article/details/128854489