using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Data; using System.Text; using NPOI; using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.POIFS; using NPOI.Util; using NPOI.XSSF.UserModel; namespace DSWeb.EntityDA { public class ExcelDA { #region 导出标准excel /// /// 导出标准excel /// /// 数据集 /// 表头 /// public static MemoryStream RenderToExcel(DataTable table, string strTitle) { MemoryStream ms = new MemoryStream(); using (table) { IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); IRow headerRow = sheet.CreateRow(0); //ICell icell1top0 = headerRow.CreateCell(0); //icell1top0.CellStyle = Getcellstyle(workbook, stylexls.头); // //定义几种字体,也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 IFont font12 = workbook.CreateFont(); font12.FontHeightInPoints = 12; font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 //font12.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色 IFont font10 = workbook.CreateFont(); font10.FontHeightInPoints = 10; font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 //font10.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色 //font.Underline = 1;下划线 // for (int j = 0; j < table.Columns.Count; j++) { headerRow.Height = 25 * 20; //- 创建当前 row 指定列索引的 cell ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行 //- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体! cell.SetFont(font12); //- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”? //- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。 //背景图形 cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index; //- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND // cellStyle.FillPattern = FillPatternType.NO_FILL; cell.FillPattern = FillPatternType.SOLID_FOREGROUND; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.CENTER; //- 细边缘 cell.BorderBottom = BorderStyle.THIN; cell.BorderLeft = BorderStyle.THIN; cell.BorderRight = BorderStyle.THIN; cell.BorderTop = BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } // if (strTitle.Trim() == "") { for (int i = 0; i < table.Columns.Count; i++) { headerRow.CreateCell(i).SetCellValue(table.Columns[i].Caption.ToString().Trim()); } } else { string[] txtval = strTitle.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < txtval.Length; i++) { headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim()); } } //// //foreach (DataColumn column in table.Columns) //{ // //导入内容 // headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption); //} // for (int i = 1; i < table.Rows.Count; i++) { for (int k = 0; k < table.Columns.Count; k++) { IRow headerRow1 = sheet.CreateRow(i); //ICell icell1top1 = headerRow1.CreateCell(k); //icell1top1.CellStyle = Getcellstyle(workbook, stylexls.默认); //- 创建当前 row 指定列索引的 cell sheet.SetColumnWidth(k, 30 * 256); //sheet.AutoSizeColumn(k);  //会按照值的长短 自动调节列的大小,但是数据量大,会陷入死循环 ICellStyle cell = headerRow1.CreateCell(k).CellStyle;// // //- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体! cell.SetFont(font10); ////- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”? ////- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。 ////背景图形 ////cell.CellStyle.FillBackgroundColor = HSSFColor.WHITE.index; ////cell.CellStyle.FillForegroundColor = HSSFColor.ORANGE.index; ////- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND cell.FillPattern = FillPatternType.NO_FILL; ////cell.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.LEFT; //- 细边缘 cell.BorderBottom = BorderStyle.THIN; cell.BorderLeft = BorderStyle.THIN; cell.BorderRight = BorderStyle.THIN; cell.BorderTop = BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } } // int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { //导出内容 dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; } return ms; } public static MemoryStream RenderToExcel(DataTable table, string strTitle ,int startsheet) { MemoryStream ms = new MemoryStream(); using (table) { IWorkbook workbook = new HSSFWorkbook(); for ( var _i = 0; _i < startsheet;_i++ ) { ISheet sheet_0 = workbook.CreateSheet(); } ISheet sheet = workbook.CreateSheet(); IRow headerRow = sheet.CreateRow(0); //ICell icell1top0 = headerRow.CreateCell(0); //icell1top0.CellStyle = Getcellstyle(workbook, stylexls.头); // //定义几种字体,也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 IFont font12 = workbook.CreateFont(); font12.FontHeightInPoints = 12; font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 //font12.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色 IFont font10 = workbook.CreateFont(); font10.FontHeightInPoints = 10; font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 //font10.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;//白色前景色 //font.Underline = 1;下划线 // for (int j = 0; j < table.Columns.Count; j++) { headerRow.Height = 25 * 20; //- 创建当前 row 指定列索引的 cell ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行 //- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体! cell.SetFont(font12); //- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”? //- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。 //背景图形 cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index; //- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND // cellStyle.FillPattern = FillPatternType.NO_FILL; cell.FillPattern = FillPatternType.SOLID_FOREGROUND; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.CENTER; //- 细边缘 cell.BorderBottom = BorderStyle.THIN; cell.BorderLeft = BorderStyle.THIN; cell.BorderRight = BorderStyle.THIN; cell.BorderTop = BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } // string[] txtval = strTitle.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < txtval.Length; i++) { headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim()); } //// //foreach (DataColumn column in table.Columns) //{ // //导入内容 // headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption); //} // for (int i = 1; i < table.Rows.Count; i++) { for (int k = 0; k < table.Columns.Count; k++) { IRow headerRow1 = sheet.CreateRow(i); //ICell icell1top1 = headerRow1.CreateCell(k); //icell1top1.CellStyle = Getcellstyle(workbook, stylexls.默认); //- 创建当前 row 指定列索引的 cell sheet.SetColumnWidth(k, 30 * 256); //sheet.AutoSizeColumn(k);  //会按照值的长短 自动调节列的大小,但是数据量大,会陷入死循环 ICellStyle cell = headerRow1.CreateCell(k).CellStyle;// // //- 设置这个样式的字体,如果没有设置,将与所有单元格拥有共同字体! cell.SetFont(font10); ////- 很费解 FillForegroundColor 这个属性,我设置了是背景色,可从字义上来说,这个似乎叫“前景色”? ////- 更令人无语的是,还有 FillBackgroundColor 属性。真不知道做什么的。 ////背景图形 ////cell.CellStyle.FillBackgroundColor = HSSFColor.WHITE.index; ////cell.CellStyle.FillForegroundColor = HSSFColor.ORANGE.index; ////- 这个是填充的模式,可以是网格、花式等。如果需要填充单色,请使用:SOLID_FOREGROUND cell.FillPattern = FillPatternType.NO_FILL; ////cell.CellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.LEFT; //- 细边缘 cell.BorderBottom = BorderStyle.THIN; cell.BorderLeft = BorderStyle.THIN; cell.BorderRight = BorderStyle.THIN; cell.BorderTop = BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } } // int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { //导出内容 dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; } return ms; } public static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName) { if (context.Request.Browser.Browser == "IE") fileName = HttpUtility.UrlEncode(fileName); context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName); context.Response.BinaryWrite(ms.ToArray()); } #region 定义单元格常用到样式的枚举 public enum stylexls { 头, url, 时间, 数字, 钱, 百分比, 中文大写, 科学计数法, 默认 } #endregion #region 定义单元格常用到样式 static ICellStyle Getcellstyle(IWorkbook wb, stylexls str) { ICellStyle cellStyle = wb.CreateCellStyle(); //定义几种字体 //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 IFont font12 = wb.CreateFont(); font12.FontHeightInPoints = 12; font12.FontName = "微软雅黑"; font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 IFont font = wb.CreateFont(); font.FontHeightInPoints = 10; font.FontName = "微软雅黑"; //font.Underline = 1;下划线 IFont fontcolorblue = wb.CreateFont(); fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE.index; fontcolorblue.IsItalic = true;//下划线 fontcolorblue.FontName = "微软雅黑"; //边框 cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.HAIR; cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR; cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR; cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.HAIR; //边框颜色 cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLACK.index; cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLACK.index; //背景图形,我没有用到过。感觉很丑 //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cellStyle.FillForegroundColor = HSSFColor.WHITE.index; // cellStyle.FillPattern = FillPatternType.NO_FILL; cellStyle.FillBackgroundColor = HSSFColor.BLUE.index; //水平对齐 cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT; //垂直对齐 cellStyle.VerticalAlignment = VerticalAlignment.CENTER; //自动换行 cellStyle.WrapText = true; //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对 cellStyle.Indention = 0; //上面基本都是设共公的设置 //下面列出了常用的字段类型 switch (str) { case stylexls.头: // cellStyle.FillPattern = FillPatternType.LEAST_DOTS; cellStyle.SetFont(font12); break; case stylexls.时间: IDataFormat datastyle = wb.CreateDataFormat(); cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd"); cellStyle.SetFont(font); break; case stylexls.数字: cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00"); cellStyle.SetFont(font); break; case stylexls.钱: IDataFormat format = wb.CreateDataFormat(); cellStyle.DataFormat = format.GetFormat("¥#,##0"); cellStyle.SetFont(font); break; case stylexls.url: fontcolorblue.Underline = 1; cellStyle.SetFont(fontcolorblue); break; case stylexls.百分比: cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%"); cellStyle.SetFont(font); break; case stylexls.中文大写: IDataFormat format1 = wb.CreateDataFormat(); cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0"); cellStyle.SetFont(font); break; case stylexls.科学计数法: cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00"); cellStyle.SetFont(font); break; case stylexls.默认: cellStyle.SetFont(font); break; } return cellStyle; } #endregion #endregion #region NPOI导出Excel表功能实现(多个工作簿) public static MemoryStream RenderToExcel(MemoryStream ms, IWorkbook workbook, DataTable table, string strTitle, string sheetname, int n) { //MemoryStream ms = new MemoryStream(); using (table) { //IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(sheetname); IRow headerRow = sheet.CreateRow(0); //定义几种字体,也可以一种字体,写一些公共属性,然后在下面需要时加特殊的 IFont font12 = workbook.CreateFont(); font12.FontHeightInPoints = 12; font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 IFont font10 = workbook.CreateFont(); font10.FontHeightInPoints = 10; font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 // for (int j = 0; j < table.Columns.Count; j++) { headerRow.Height = 25 * 20; //- 创建当前 row 指定列索引的 cell ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行 cell.SetFont(font12); //背景图形 cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index; cell.FillPattern = FillPatternType.SOLID_FOREGROUND; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.CENTER; //- 细边缘 cell.BorderBottom = BorderStyle.THIN; cell.BorderLeft = BorderStyle.THIN; cell.BorderRight = BorderStyle.THIN; cell.BorderTop = BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } // string[] txtval = strTitle.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < txtval.Length; i++) { headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim()); } for (int i = 1; i < table.Rows.Count; i++) { for (int k = 0; k < table.Columns.Count; k++) { IRow headerRow1 = sheet.CreateRow(i); //- 创建当前 row 指定列索引的 cell sheet.SetColumnWidth(k, 30 * 256); ICellStyle cell = headerRow1.CreateCell(k).CellStyle;// cell.SetFont(font10); cell.FillPattern = FillPatternType.NO_FILL; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.LEFT; //- 细边缘 cell.BorderBottom = BorderStyle.THIN; cell.BorderLeft = BorderStyle.THIN; cell.BorderRight = BorderStyle.THIN; cell.BorderTop = BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } } // int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { //导出内容 dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } //foreach (DataRow dr in dtSource.Rows) //{ // rowCount++; // //超出10000条数据 创建新的工作簿 // if (rowCount == 10000) // { // rowCount = 1; // sheetCount++; // newsheet = excelWorkbook.CreateSheet("Sheet" + sheetCount); // //循环导出列 // foreach (System.Collections.DictionaryEntry de in ListColumnsName) // { // HSSFRow newRow = excelSheet.CreateRow(0); // HSSFCell newCell = newRow.CreateCell(cellIndex); // newCell.SetCellValue(de.Value.ToString()); // cellIndex++; // } // //end // } // HSSFRow newRow = newsheet.CreateRow(rowCount); // InsertCell(dtSource, dr, newRow, newsheet, excelWorkbook); //} workbook.Write(ms); ms.Flush(); ms.Position = n; } return ms; } #endregion #region 读取excel文件 返回DataTable //public static DataTable ExcelToDatatable(Stream stream, string fileType, out string strMsg, string sheetName = null, int startrow = 0) //{ // strMsg = ""; // DataTable dt = new DataTable(); // ISheet sheet = null; // IWorkbook workbook = null; // try // { // #region 判断excel版本 // //2007以上版本excel // if (fileType == ".xlsx") // { // workbook = new XSSFWorkbook(stream); // } // //2007以下版本excel // else if (fileType == ".xls") // { // workbook = new HSSFWorkbook(stream); // } // else // { // throw new Exception("传入的不是Excel文件!"); // } // #endregion // if (!string.IsNullOrEmpty(sheetName)) // { // sheet = workbook.GetSheet(sheetName); // if (sheet == null) // { // sheet = workbook.GetSheetAt(0); // } // } // else // { // sheet = workbook.GetSheetAt(0); // } // if (sheet != null) // { // IRow firstRow = sheet.GetRow(startrow); // int cellCount = firstRow.LastCellNum; // for (int i = firstRow.FirstCellNum; i < cellCount; i++) // { // ICell cell = firstRow.GetCell(i); // if (cell != null) // { // string cellValue = cell.StringCellValue.Trim(); // if (!string.IsNullOrEmpty(cellValue)) // { // DataColumn dataColumn = new DataColumn(cellValue); // dt.Columns.Add(dataColumn); // } // } // } // DataRow dataRow = null; // //遍历行 // for (int j = sheet.FirstRowNum + 1; j <= sheet.LastRowNum; j++) // { // IRow row = sheet.GetRow(j); // dataRow = dt.NewRow(); // if (row == null || row.FirstCellNum < 0) // { // continue; // } // //遍历列 // var isadd = false; // for (int i = row.FirstCellNum; i < cellCount; i++) // { // ICell cellData = row.GetCell(i); // if (cellData != null && cellData.ToString() != "") // { // //判断是否为数字型,必须加这个判断不然下面的日期判断会异常 // if (cellData.CellType == CellType.NUMERIC) // { // //判断是否日期类型 // if (DateUtil.IsCellDateFormatted(cellData)) // { // dataRow[i] = cellData.DateCellValue; // } // else // { // dataRow[i] = cellData.ToString().Trim(); // } // } // else if (cellData.CellType == CellType.FORMULA) // { // try // { // dataRow[i] = cellData.StringCellValue; // } // catch // { // dataRow[i] = cellData.NumericCellValue; // } // } // else // { // dataRow[i] = cellData.ToString().Trim(); // } // isadd = true; // } // else // { // dataRow[i] = ""; // } // } // if (isadd) dt.Rows.Add(dataRow); // } // } // else // { // throw new Exception("没有获取到Excel中的数据表!"); // } // } // catch (Exception ex) // { // workbook = new HSSFWorkbook(stream); // strMsg = ex.Message; // } // return dt; //} public static DataTable ExcelToDataTable(string filePath, bool isColumnName) { 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 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) { sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet dataTable = new DataTable(); if (sheet != null) { int rowCount = sheet.LastRowNum;//总行数 if (rowCount > 0) { IRow firstRow = sheet.GetRow(0);//第一行 int cellCount = firstRow.LastCellNum;//列数 //构建datatable的列 if (isColumnName) { startRow = 1;//如果第一行是列名,则从第二行开始读取 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); } } } } 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; 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 e) { if (fs != null) { fs.Close(); } return null; } } #endregion } }