예제 #1
0
        public static DataTable ParseExcel(ExcelOptions options)
        {
            DataTable data = new DataTable();

            try
            {
                //根据文件流创建excel数据结构

                IWorkbook workbook;
                if (options.FileName.EndsWith(".xls"))
                {
                    workbook = new HSSFWorkbook(options.Stream);
                }
                else
                {
                    workbook = WorkbookFactory.Create(options.Stream);
                }
                //excel工作表
                ISheet sheet = null;
                //标题行
                int titleRow = options.TitleRow;
                //数据起始行
                int startRow = options.StartRow;
                //如果有指定工作表名称
                if (!string.IsNullOrEmpty(options.SheetName))
                {
                    sheet = workbook.GetSheet(options.SheetName);
                    //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    if (sheet is null)
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    //如果没有指定的sheetName,则尝试获取第一个sheet
                    sheet = workbook.GetSheetAt(0);
                }

                if (sheet != null)
                {
                    IRow firstRow = sheet.GetRow(titleRow);
                    //一行最后一个cell的编号 即总的列数
                    int cellCount = firstRow.LastCellNum;
                    //TitleRow > -1 说明有标题行
                    if (options.TitleRow > -1)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                //excel的列名
                                string cellValue = cell.StringCellValue;
                                if (!string.IsNullOrWhiteSpace(cellValue))
                                {
                                    //使用实体对应的名称
                                    string     tableName = options.Mappers.Where(e => e.Index == i).Select(e => e.TableName).FirstOrDefault();
                                    DataColumn column    = new DataColumn(tableName);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                    }
                    //最后一列的标号
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        //如果当前行为null,或者第一个单元格的值为空,默认认为是结束
                        if (row is null ||
                            row.Cells.Count == 0
                            )
                        {
                            break;
                        }

                        DataRow dataRow = data.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            ICell cells = row.GetCell(j);
                            //没有数据的单元格都默认是null
                            if (cells != null)
                            {
                                switch (cells.CellType)
                                {
                                case CellType.Numeric:
                                    if (DateUtil.IsCellDateFormatted(cells))
                                    {
                                        dataRow[j] = cells.DateCellValue.CastToDateTime();
                                    }
                                    else
                                    {
                                        dataRow[j] = cells.NumericCellValue;
                                    }
                                    break;

                                case CellType.Unknown:
                                case CellType.String:
                                case CellType.Formula:
                                case CellType.Blank:
                                case CellType.Error:
                                    dataRow[j] = cells.StringCellValue.CastToString();
                                    break;

                                case CellType.Boolean:
                                    dataRow[j] = cells.BooleanCellValue;
                                    break;

                                default:
                                    dataRow[j] = cells.CastToString();
                                    break;
                                }
                            }
                        }
                        data.Rows.Add(dataRow);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Console.WriteLine("Excel解析成功");
            return(data);
        }