赞
踩
方式二 Excel引擎读取 Microsoft.Office.Interop.Excel
使用方式:引用 Microsoft.Office.Interop.Excel 调用Excel读取
优点:不受Excel版本限制,可指定sheet读取
缺点:读取速度慢,几千数据10分钟
代码如下:
public static List<DataTable> excelToDataTable(string filePath,bool hasTitle = false) { List<DataTable> dtList = new List<DataTable>(); int iRowCount; int iColCount; Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Sheets sheets; object oMissiong = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Workbook workbook = null; DataTable dt = new DataTable(); try { if (app == null) return null; workbook = app.Workbooks.Open(filePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong); sheets = workbook.Worksheets; //将数据读入到DataTable中 Microsoft.Office.Interop.Excel.Worksheet worksheet = sheets.get_Item(1); if (worksheet == null) return null; iRowCount = worksheet.UsedRange.Rows.Count; iColCount = worksheet.UsedRange.Columns.Count; //生成列头 for (int i = 0; i < iColCount; i++) { var name = "column" + i; if (hasTitle) { var txt = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]).Text.ToString(); if (!string.IsNullOrWhiteSpace(txt)) name = txt; } while (dt.Columns.Contains(name)) name = name + "_1";//重复行名称会报错。 dt.Columns.Add(new DataColumn(name, typeof(string))); } //生成行数据 Microsoft.Office.Interop.Excel.Range range; int rowIdx = hasTitle ? 2 : 1; for (int iRow = rowIdx; iRow <= iRowCount; iRow++) { DataRow dr = dt.NewRow(); for (int iCol = 1; iCol <= iColCount; iCol++) { range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol]; dr[iCol - 1] = (range.Value2 == null) ? "" : range.Text.ToString(); } dt.Rows.Add(dr); } dtList.Add(dt); for (int i = 2; i <= sheets.Count; i++) { worksheet=sheets.get_Item(i);//读取第一张表 iRowCount = worksheet.UsedRange.Rows.Count; iColCount = worksheet.UsedRange.Columns.Count; //生成列头 for (int j = 0; j < iColCount; j++) { var name = "column" + i; if (hasTitle) { var txt = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, j + 1]).Text.ToString(); if (!string.IsNullOrWhiteSpace(txt)) name = txt; } while (dt.Columns.Contains(name)) name = name + "_1";//重复行名称会报错。 dt.Columns.Add(new DataColumn(name, typeof(string))); } //生成行数据 Microsoft.Office.Interop.Excel.Range range1; int rowIdx1 = hasTitle ? 2 : 1; for (int iRow = rowIdx1; iRow <= iRowCount; iRow++) { DataRow dr = dt.NewRow(); for (int iCol = 1; iCol <= iColCount; iCol++) { range1 = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol]; dr[iCol - 1] = (range1.Value2 == null) ? "" : range1.Text.ToString(); } dt.Rows.Add(dr); } dtList.Add(dt); } return dtList; } catch { return null; } finally { workbook.Close(false, oMissiong, oMissiong); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); workbook = null; app.Workbooks.Close(); app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。