NET使用NPOI读取excel

 1,记录一下,通过sheel名字读取,同时处理数据为时间读取内容不正确的情况

 /// 
        /// 读取excel 表格
        /// 
        /// 
        /// Sheet名称
        /// 第一行是否列名
        /// 表格
        public static DataTable ReadExcel(string fileName, string sheetName, bool isFirstRowColumn)
        {
            FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            //根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
            IWorkbook workbook = null;
            if (fileName.ToLower().EndsWith("xls"))
            {
                workbook = new HSSFWorkbook(file);
            }
            else
            {
                workbook = new XSSFWorkbook(file);
            }

            ISheet sheet = workbook.GetSheet(sheetName);

            int startRow = -1;
            DataTable dataTable = new DataTable();
            if (sheet != null)
            {
                IRow firstRow = sheet.GetRow(0);

                //一行最后一个cell的编号 即总的列数
                int cellCount = firstRow.LastCellNum;

                if (isFirstRowColumn)
                {
                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                    {
                        ICell cell = firstRow.GetCell(i);
                        if (cell != null)
                        {
                            string cellValue = cell.StringCellValue;
                            if (cellValue != null)
                            {
                                DataColumn column = new DataColumn(cellValue);
                                dataTable.Columns.Add(column);
                            }
                        }
                    }
                    startRow = sheet.FirstRowNum + 1;
                }
                else
                {
                    startRow = sheet.FirstRowNum;
                }

                //最后一列的标号
                int rowCount = sheet.LastRowNum;
                for (int i = startRow; i <= rowCount; ++i)
                {
                    IRow row = sheet.GetRow(i);

                    //没有数据的行默认是null 
                    if (row == null) continue;

                    DataRow dataRow = dataTable.NewRow();
                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                    { 
                        ICell cell = row.GetCell(j);
                        if (cell != null)
                        {
                            if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell))
                                dataRow[j] = cell.DateCellValue.ToString("yyyy/MM/dd HH:mm:ss");
                            else
                            {
                                dataRow[j] = row.GetCell(j).ToString();
                            }
                        }

                    }
                    dataTable.Rows.Add(dataRow);
                }
            }

            return dataTable;

        }

//调用
//  string path = Path.Combine(Server.MapPath("~/data/Excel")) + "/" + fileName;
// DataTable dtPlan = ExcelHelper.ReadExcel(path, "计划信息", true);    计划信息为sheel名

 

你可能感兴趣的:(.net,C#)