Home >Backend Development >C++ >How can I efficiently read Excel sheets into a DataTable in C# or VB.NET?
You've stumbled upon a common challenge - importing Excel data into a DataTable with speed and efficiency. Your current VB solution using the Microsoft Excel Driver is a viable approach, but let's explore some alternative methods that may offer enhancements.
If you prefer C#, here's a snippet adapted from Ciarán Walsh's answer:
string sSheetName = null; string sConnection = null; DataTable dtTablesList = default(DataTable); OleDbCommand oleExcelCommand = default(OleDbCommand); OleDbDataReader oleExcelReader = default(OleDbDataReader); OleDbConnection oleExcelConnection = default(OleDbConnection); sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Test.xls;Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\""; oleExcelConnection = new OleDbConnection(sConnection); oleExcelConnection.Open(); dtTablesList = oleExcelConnection.GetSchema("Tables"); if (dtTablesList.Rows.Count > 0) { sSheetName = dtTablesList.Rows[0]["TABLE_NAME"].ToString(); } dtTablesList.Clear(); dtTablesList.Dispose(); if (!string.IsNullOrEmpty(sSheetName)) { oleExcelCommand = oleExcelConnection.CreateCommand(); oleExcelCommand.CommandText = "Select * From [" + sSheetName + "]"; oleExcelCommand.CommandType = CommandType.Text; oleExcelReader = oleExcelCommand.ExecuteReader(); nOutputRow = 0; while (oleExcelReader.Read()) { } oleExcelReader.Close(); } oleExcelConnection.Close();
Consider using the NPOI library, which offers both a managed Excel library for .NET and support for the latest Excel file formats. NPOI may provide improved speed compared to using the ODBC driver.
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; public static DataTable ReadExcelSheet(string filePath, string sheetName) { var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); var workbook = new HSSFWorkbook(stream); var sheet = workbook.GetSheet(sheetName); var table = new DataTable(); var firstRow = sheet.GetRow(0); for (int i = 0; i < firstRow.Cells.Count; i++) { table.Columns.Add(firstRow.Cells[i].ToString()); } for (int i = 1; i <= sheet.LastRowNum; i++) { var row = sheet.GetRow(i); var dataRow = table.NewRow(); for (int j = 0; j < row.Cells.Count; j++) { dataRow[j] = row.Cells[j].ToString(); } table.Rows.Add(dataRow); } return table; }
Regarding your question about object disposal, you don't need to explicitly call Dispose() or set variables to Nothing in your VB code because the objects will be automatically disposed when the using block is exited. However, it's generally good practice to explicitly dispose of objects to ensure proper resource management.
If speed is a critical factor, consider converting the Excel file to a CSV file (.csv) before importing it into the DataTable. CSV files are plain text files with a simple structure, which can significantly improve reading speed. You can use the TextFieldParser class to read CSV files efficiently:
public static DataTable GetDataTableFromCSVFile(string csv_file_path) { var csvData = new DataTable(); using (var csvReader = new TextFieldParser(csv_file_path)) { csvReader.SetDelimiters(new string[] { "," }); csvReader.HasFieldsEnclosedInQuotes = true; var colFields = csvReader.ReadFields(); foreach (var column in colFields) { var dataColumn = new DataColumn(column); dataColumn.AllowDBNull = true; csvData.Columns.Add(dataColumn); } while (!csvReader.EndOfData) { var fieldData = csvReader.ReadFields(); //Making empty value as null for (int i = 0; i < fieldData.Length; i++) { if (fieldData[i] == string.Empty) { fieldData[i] = null; } } csvData.Rows.Add(fieldData); } } return csvData; }
The above is the detailed content of How can I efficiently read Excel sheets into a DataTable in C# or VB.NET?. For more information, please follow other related articles on the PHP Chinese website!