[源码分享]NPOI读取Excel到 dataTable

            const string fileName = "d:\aa.xlsx";  //路径
            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate); //读取文件流
            HSSFWorkbook workbook = new HSSFWorkbook(fs);  //根据EXCEL文件流初始化工作簿
            var sheet1= workbook.GetSheetAt(0); //获取第一个sheet
            DataTable table = new DataTable();//
            var row1 = sheet1.GetRow(0);//获取第一行即标头
            int cellCount = row1.LastCellNum; //第一行的列数
            //把第一行的数据添加到datatable的列名
            for (int i = row1.FirstCellNum; i < cellCount; i++)
            {
                DataColumn column = new DataColumn(row1.GetCell(i).StringCellValue);
                table.Columns.Add(column);
            }

            int rowCount = sheet1.LastRowNum; //总行数

            //把每行数据添加到datatable中
            for (int i = (sheet1.FirstRowNum + 1); i < sheet1.LastRowNum; i++)
            {
                HSSFRow row = sheet1.GetRow(i);
                DataRow dataRow = table.NewRow();

                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                table.Rows.Add(dataRow);
            }
//到这里 table 已经可以用来做数据源使用了
            workbook = null; //清空工作簿--释放资源
            sheet1 = null;  //清空sheet


可以使用NPOI库来读取Excel文件并将其转换为DataTable对象。以下是示例代码: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; using System.IO; public static DataTable ReadExcelToDataTable(string filePath, bool isFirstRowHeader = true) { DataTable dataTable = new DataTable(); using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = null; if (Path.GetExtension(filePath) == ".xls") { workbook = new HSSFWorkbook(fileStream); } else if (Path.GetExtension(filePath) == ".xlsx") { workbook = new XSSFWorkbook(fileStream); } if (workbook != null) { ISheet sheet = workbook.GetSheetAt(0); IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { ICell cell = headerRow.GetCell(i); if (cell != null) { string columnName = isFirstRowHeader ? cell.StringCellValue : "Column" + i; DataColumn column = new DataColumn(columnName); dataTable.Columns.Add(column); } } int rowCount = sheet.LastRowNum; for (int i = (isFirstRowHeader ? 1 : 0); i <= rowCount; i++) { IRow row = sheet.GetRow(i); if (row != null) { DataRow dataRow = dataTable.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); } } dataTable.Rows.Add(dataRow); } } } } return dataTable; } ``` 使用方法: ```csharp DataTable dataTable = ReadExcelToDataTable("path/to/excel/file.xlsx"); ``` 其,`isFirstRowHeader`参数表示Excel表格的第一行是否为表头。如果为`true`,则会将第一行作为表头。如果为`false`,则会自动为每一列生成一个列名。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值