Several common ways and implementation steps of C# to read Excel

Table of contents

1. Use the Microsoft Office Interop Excel library

2. Use the OLEDB database connection method

3. Using the EPPlus library


In C#, we can use the following methods to read data from an Excel file into a DataTable:

1. Use the Microsoft Office Interop Excel library

This method requires Microsoft Office to be installed and has lower performance. The specific implementation steps are as follows:

using System.Data;
using Excel = Microsoft.Office.Interop.Excel;

// 创建 Excel 对象
Excel.Application app = new Excel.Application();
Excel.Workbook workbook = app.Workbooks.Open(@"D:\data.xlsx");
Excel.Worksheet worksheet = workbook.Sheets[1];

// 读取数据到 DataTable 中
DataTable dt = new DataTable();
for (int i = 1; i <= worksheet.UsedRange.Columns.Count; i++) {
    dt.Columns.Add(worksheet.Cells[1, i].Value2.ToString());
}
for (int j = 2; j <= worksheet.UsedRange.Rows.Count; j++) {
    DataRow dr = dt.NewRow();
    for (int i = 1; i <= worksheet.UsedRange.Columns.Count; i++) {
        dr[i - 1] = worksheet.Cells[j, i].Value2;
    }
    dt.Rows.Add(dr);
}

// 关闭 Excel 对象
workbook.Close(false, Type.Missing, Type.Missing);
app.Quit();

2. Use the OLEDB database connection method

This method needs to install Microsoft Access Database Engine, but its performance is higher than the first method. The specific implementation steps are as follows:

using System.Data.OleDb;

// 定义 Excel 连接字符串
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\data.xlsx;Extended Properties=\"Excel 12.0\";";

// 创建 OleDbConnection 对象
OleDbConnection conn = new OleDbConnection(connectionString);

// 打开连接
conn.Open();

// 查询数据
string sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);

// 关闭连接
conn.Close();

3. Using the EPPlus library

This method needs to introduce EPPlus dependent library. The specific implementation steps are as follows:

using System.IO;
using OfficeOpenXml;

// 创建 ExcelPackage 对象
using (ExcelPackage package = new ExcelPackage(new FileInfo(@"D:\data.xlsx"))) {
    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

    // 读取数据到 DataTable 中
    DataTable dt = new DataTable(worksheet.Name);
    foreach (var firstRowCell in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column]) {
        dt.Columns.Add(firstRowCell.Text);
    }
    for (int rowNumber = 2; rowNumber <= worksheet.Dimension.End.Row; rowNumber++) {
        var row = worksheet.Cells[rowNumber, 1, rowNumber, worksheet.Dimension.End.Column];
        DataRow dr = dt.Rows.Add();
        foreach (var cell in row) {
            dr[cell.Start.Column - 1] = cell.Text;
        }
    }
}

 Among the above three methods, the first and second methods need to install additional dependent libraries and have low performance; while the third method needs to introduce EPPlus dependent libraries, but it has high performance and is easy to use. Which method to choose can be considered according to your actual situation.

Guess you like

Origin blog.csdn.net/beenles/article/details/130626180