using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Data; using System.IO; using System.Web; namespace WX.CRM.Common { public class ExcelHandler { /// /// 由DataSet导出Excel /// /// /// 指定Excel工作表名称 /// /// Excel工作表 public static void ExportDataSetToExcel(DataSet sourceDs, string fileName, string sheetName) { MemoryStream ms = ExportDataSetToExcel(sourceDs, sheetName) as MemoryStream; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); HttpContext.Current.Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.End(); ms.Close(); ms = null; } /// /// 由DataSet导出Excel /// /// 数据源 /// 文件夹名 /// 文件名 /// 指定Excel工作表名称 public static void SaveDataSetToExcel(DataSet sourceDs, string folderPath, string fileName, string sheetName) { Stream stream = ExportDataSetToExcel(sourceDs, sheetName); FileUnit.WriteWithDirectory(folderPath, fileName, stream); } /// /// 由DataSet导出Excel /// /// 要导出数据的DataTable /// 工作表名称 /// Excel工作表 private static Stream ExportDataSetToExcel(DataSet sourceDs, string sheetName) { HSSFWorkbook workbook = new HSSFWorkbook(); MemoryStream ms = new MemoryStream(); string[] sheetNames = sheetName.Split(','); int sheetSize = 65530; int rowCount = sourceDs.Tables[0].Rows.Count; int sheetCout = (rowCount / sheetSize) + 1; for (int i = 0; i < sheetNames.Length; i++) { for (int j = 0; j < sheetCout; j++) { ISheet sheet = workbook.CreateSheet(sheetNames[i] + j); IRow headerRow = sheet.CreateRow(0); // handling header. foreach (DataColumn column in sourceDs.Tables[i].Columns) headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // handling value. int rowIndex = 1; int mcount = (j + 1) * sheetSize > rowCount ? rowCount : (j + 1) * sheetSize; for (int m = (j * sheetSize); m < mcount; m++) { DataRow row = sourceDs.Tables[i].Rows[m]; IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in sourceDs.Tables[i].Columns) { ICell icell = dataRow.CreateCell(column.Ordinal); ICellValeSet(column.DataType.FullName, row[column].ToString(), icell); //dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } } } workbook.Write(ms); ms.Flush(); ms.Position = 0; workbook = null; return ms; } private static void ICellValeSet(string dbType, string dbValue, ICell icell) { switch (GetCellType(dbType)) { case stylexls.数字: int wovalue = 0; if (int.TryParse(dbValue, out wovalue)) { icell.SetCellValue(wovalue); } else { icell.SetCellValue(dbValue); } break; case stylexls.小数: double dobValue = 0; if (double.TryParse(dbValue, out dobValue)) { icell.SetCellValue(dobValue); } else { icell.SetCellValue(dbValue); } break; case stylexls.时间: DateTime timeValue; if (DateTime.TryParse(dbValue, out timeValue)) { icell.SetCellValue(timeValue.ToString("yyyy/MM/dd HH:mm:ss")); } else { icell.SetCellValue(""); } break; case stylexls.默认: icell.SetCellValue(dbValue.ToString()); break; } } private static void ICellValeSet(ExcelCell cell, ICell icell) { switch (GetCellType(cell.type)) { case stylexls.数字: int wovalue = 0; if (int.TryParse(cell.nvalue, out wovalue)) { icell.SetCellValue(wovalue); } else { icell.SetCellValue(cell.nvalue); } break; case stylexls.小数: double dobValue = 0; if (double.TryParse(cell.nvalue, out dobValue)) { icell.SetCellValue(dobValue); } else { icell.SetCellValue(cell.nvalue); } break; case stylexls.时间: DateTime timeValue; if (DateTime.TryParse(cell.nvalue, out timeValue)) { icell.SetCellValue(timeValue.ToString("yyyy/MM/dd HH:mm:ss")); } else { icell.SetCellValue(""); } break; case stylexls.默认: icell.SetCellValue(cell.nvalue.ToString()); break; } } private static stylexls GetCellType(string fullTypeName) { if (fullTypeName == null) return stylexls.默认; if (fullTypeName.IndexOf("System.Decimal") > -1) return stylexls.小数; if (fullTypeName.IndexOf("System.Int") > -1) return stylexls.数字; if (fullTypeName.IndexOf("System.DateTime") > -1) return stylexls.时间; return stylexls.默认; } } #region 定义单元格常用到样式的枚举 public enum stylexls { 头, url, 时间, 数字, 小数, 钱, 百分比, 中文大写, 科学计数法, 默认 } #endregion public class ExcelCell { /// /// 英文字段名称 /// public string fild { get; set; } /// /// 字段的数据类型 /// public string type { get; set; } /// /// 字段的中文标题 /// public string title { get; set; } /// /// 字段的值 /// public string nvalue { get; set; } } }