【C#】NPOI简单的Excel转DataTable

        /// 
        /// 将带标题Excel数据写入Datatable
        /// 
        /// excel工作薄sheet的名称,为null时,获取第一个sheet页
        /// 标题在Excel的行数
        /// Excel文档
        /// 
        public static DataTable ExcelToDataTable(string sheetName, int TitleNum, string fileName)
        {
        	FileStream fs = null;
            try
            {
                ISheet sheet = null;
                IWorkbook workbook = null;
                
                DataTable data = null;
                int startRow = TitleNum - 1;//标题行
                fs = new FileStream(fileName, FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                { workbook = new XSSFWorkbook(fs); }
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                { workbook = new HSSFWorkbook(fs); }

                if (sheetName != null)
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    sheet = workbook.GetSheetAt(0);
                }
                if (sheet != null)
                {
                    data = new DataTable();
                    IRow firstRow = sheet.GetRow(startRow);
                    int cellCount = firstRow.LastCellNum; //标题行最后一个cell的编号 即总的列数
                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                    {
                        ICell cell = firstRow.GetCell(i);

                        cell.SetCellType(CellType.String);
                        if (cell != null)
                        {
                            string cellValue = cell.StringCellValue;
                            cellValue = cellValue.Replace("\n", "");
                            if (cellValue != null)
                            {
                                DataColumn column = new DataColumn(cellValue.Trim());
                                data.Columns.Add(column);
                            }
                        }
                    }
                    startRow++;
                    int rowCount = sheet.LastRowNum;//最后一列的标号
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null)
                        {
                            continue; //没有数据的行默认是null       
                        }
                        else
                        {
                        	if (row.FirstCellNum < 0) { continue; }
                            int isnull = 0;
                            DataRow dataRow = data.NewRow();
                            for (int j = 0; j < data.Columns.Count; ++j)
                            {
                                int rowindex = j + row.FirstCellNum;
                                if (row.GetCell(rowindex) != null)
                                {
                                    string dataValue = "";
                                    //同理,没有数据的单元格都默认是null
                                    ICell c = row.GetCell(rowindex);
                                    CellType type = c.CellType;
                                    if (type == CellType.Numeric)
                                    {
                                        short format = c.CellStyle.DataFormat;
                                        if (HSSFDateUtil.IsCellDateFormatted(c))
                                        //if (format == 0xe || format == 0x16)
                                        {
                                            DateTime date = c.DateCellValue;
                                            dataValue = date.ToString();
                                        }
                                        else
                                        {
                                            dataValue = c.NumericCellValue.ToString();
                                        }
                                    }
                                    else if (type == CellType.Formula)
                                    {
                                        c.SetCellType(CellType.String);
                                        dataValue = c.StringCellValue;
                                    }
                                    else
                                    {
                                        dataValue = c.ToString();
                                    }
                                    dataValue = dataValue.Replace("\n", "");
                                    dataRow[j] = dataValue.Trim();
                                    if (string.IsNullOrWhiteSpace(dataValue.Trim()))
                                    {
                                        isnull++;
                                    }
                                }
                                else
                                {
                                    isnull++;
                                }
                            }
                            if (isnull != cellCount)
                            {
                                data.Rows.Add(dataRow);
                            }
                        }
                    }
                }
                return data;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally {

                fs.Close();
                fs.Dispose();
            }
        }

你可能感兴趣的:(c#)