NPOI 读取xls,xlsx文件

using NPOI.HSSF.UserModel;

using NPOI.SS.UserModel;

using NPOI.XSSF.UserModel;

public static DataTable NpoiExcelToDataTable(string filePath, string sheetName, int startRow, bool isFirstRowColumn,bool cshz)
        {
            DataTable dataTable = null;
            FileStream fs = null;
            DataColumn column = null;
            DataRow dataRow = null;
            IWorkbook workbook = null;
            ISheet sheet = null;
            IRow row = null;
            ICell cell = null;
            int cellCount = 0;
            //  int startRow = 0;
            try
            {
                using (fs = File.OpenRead(filePath))
                {
                    // 2007版本
                    if (filePath.IndexOf(".xlsx") > 0)
                        workbook = new XSSFWorkbook(fs);
                    // 2003版本
                    else if (filePath.IndexOf(".xls") > 0)
                        workbook = new HSSFWorkbook(fs);

                    if (workbook != null)
                    {
                        if (sheetName != null)
                        {
                            sheet = workbook.GetSheet(sheetName);
                            if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                            {
                                if (cshz)
                                {
                                    return null;
                                }                   sheet = workbook.GetSheetAt(0);
                            }
                        }
                        else
                        {
                            sheet = workbook.GetSheetAt(0);
                        }
                        dataTable = new DataTable();
                        if (sheet != null)
                        {
                            int rowCount = sheet.LastRowNum;//总行数
                            if (rowCount > 0)
                            {
                                IRow firstRow = sheet.GetRow(startRow);//第一行
                                int rowindex = 0;                    //搜索空行,并跳过
                                while (firstRow == null && rowindex < rowCount)
                                {
                                    rowindex++;
                                    firstRow = sheet.GetRow(rowindex);
                                }                    //如果全为空行则返回null值
                                if (rowindex == rowCount) return null;

                                cellCount = firstRow.LastCellNum;//列数
                                startRow = firstRow.RowNum;
                                //  构建datatable的列
                                if (isFirstRowColumn)
                                {
                                    //如果第一行是列名,则从第二行开始读取
                                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    {
                                        cell = firstRow.GetCell(i);
                                        if (cell != null)
                                        {
                                            if (cell.StringCellValue != null)
                                            {
                                                column = new DataColumn(cell.StringCellValue);
                                                dataTable.Columns.Add(column);
                                            }
                                        }
                                    }
                                    startRow++;
                                }
                                else
                                {
                                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    {
                                        column = new DataColumn("column" + (i + 1));
                                        dataTable.Columns.Add(column);
                                    }
                                }

                                //   填充行
                                for (int i = startRow; i <= rowCount; ++i)
                                {
                                    row = sheet.GetRow(i);
                                    if (row == null) continue;
                                    cellCount = row.LastCellNum;                      //全文行之间的列数不一样的话,继续添加列
                                    if (cellCount > dataTable.Columns.Count)
                                    {
                                        for (int c = dataTable.Columns.Count; c < cellCount; c++)
                                        {
                                            column = new DataColumn("column" + (c + 1));
                                            dataTable.Columns.Add(column);
                                        }
                                    }
                                    dataRow = dataTable.NewRow();
                                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                                    {
                                        cell = row.GetCell(j);
                                        if (cell == null)
                                        {
                                            dataRow[j] = "";
                                        }
                                        else
                                        {
                                            //  CellType(Unknown = -1, Numeric = 0, String = 1, Formula = 2, Blank = 3, Boolean = 4, Error = 5,)
                                            switch (cell.CellType)
                                            {
                                                case CellType.Blank:
                                                    dataRow[j] = "";
                                                    break;
                                                case CellType.Numeric:
                                                    short format = cell.CellStyle.DataFormat;
                                                    //  对时间格式(2015.12.5、2015 / 12 / 5、2015 - 12 - 5等)的处理
                                                    if (format == 14 || format == 31 || format == 57 || format == 58)
                                                        dataRow[j] = cell.DateCellValue;
                                                    else
                                                        dataRow[j] = cell.NumericCellValue;
                                                    break;
                                                case CellType.String:
                                                    dataRow[j] = cell.StringCellValue;
                                                    break;
                                            }
                                        }
                                    }
                                    dataTable.Rows.Add(dataRow);
                                }
                            }
                        }
                    }
                }
                return dataTable;
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                return null;
            }
        }

原文链接: https://www.cnblogs.com/clarklxr/p/13225580.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/199868

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午8:15
下一篇 2023年2月12日 下午8:15

相关推荐