• 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; } }
  • 相关阅读:
    #研发解决方案#研发协作平台CloudEngine
    人生做出的选择越多,友谊的小船翻得越快?
    告诉别人你是谁:边界的那些事儿
    如何从零开始搭建一个技术平台?
    Windows10 自动配置切换IP地址
    Human disease database
    R语言install.packages("jpeg")报错
    RStudio中,出现中文乱码问题的解决方案(修改文件编码)
    How do I run Adobe Flash on Chrome?
    RGB颜色查询对照表
  • 原文地址:https://www.cnblogs.com/clarklxr/p/13225580.html
Copyright © 2020-2023  润新知