예제 #1
0
        /// <summary>
        /// 将excel中的数据导入到DataTable中
        /// </summary>
        /// <param name="sheetName">excel工作薄sheet的名称</param>
        /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
        /// <returns>返回的DataTable</returns>
        public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn)
        {
            ISheet    sheet    = null;
            DataTable data     = new DataTable();
            int       startRow = 0;

            NpoiDataType[] ColumnDataType = null;
            try
            {
                using (FileStream stream = new FileStream(@fileName, FileMode.Open, FileAccess.Read))
                {
                    workbook = WorkbookFactory.Create(stream);//使用接口,自动识别excel2003/2007格式
                    if (sheetName != null)
                    {
                        sheet = workbook.GetSheet(sheetName);
                    }
                    else
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                    if (sheet != null)
                    {
                        IRow firstRow  = sheet.GetRow(0);
                        int  cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数

                        if (isFirstRowColumn)
                        {
                            for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                            {
                                DataColumn column = new DataColumn(firstRow.GetCell(i).StringCellValue);
                                data.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);
                            if (row == null)
                            {
                                continue;              //没有数据的行默认是null       
                            }
                            if (ColumnDataType == null)
                            {
                                ColumnDataType = new NpoiDataType[cellCount];
                                for (int j = 0; j < cellCount; j++)
                                {
                                    ICell hs = row.GetCell(j);
                                    if (hs != null)
                                    {
                                        ColumnDataType[j] = GetCellDataType(hs);
                                    }
                                }
                            }

                            DataRow dataRow = data.NewRow();
                            for (int j = row.FirstCellNum; j < cellCount; ++j)
                            {
                                ICell hs = row.GetCell(j);
                                if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                                {
                                    if (hs.CellType == CellType.Numeric && HSSFDateUtil.IsCellDateFormatted(hs))
                                    {
                                        dataRow[j] = HSSFDateUtil.GetJavaDate(hs.NumericCellValue).ToString("HH:mm:ss");
                                    }
                                    else
                                    {
                                        dataRow[j] = hs;
                                    }
                                    //dataRow[j] = row.GetCell(j).ToString();
                                }
                                //dataRow[j] = GetCellData(ColumnDataType[j], row, j);
                            }
                            data.Rows.Add(dataRow);
                        }
                    }
                }
                return(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return(null);
            }
        }