一个excel内有4个sheet
进度表模板如下:
旧房表模板:
新开工模板:
在建项目模板:
源码如下:
protected void lnkExport_Click(object sender, EventArgs e)
{
。。。你的页面的查询条件
DataSet dataSet = new DataSet();
string[] lists = new string[4];
lists[0] = GetfiledsName(0);
lists[1] = GetfiledsName(1);//第二个sheet的可选择导出项
lists[2] = GetfiledsName(2);第三个sheet的可选择导出项
lists[3] = GetfiledsName(3);//第四个sheet的可选择导出项
//分别查询4个datatable,表示4个sheet的数据源
DataTable ds = tsmBll.GetSubsidyRecordsExport(startDate, endDate, RangeType, xzqh, xzc, ProjectName, "进度表", "/TemplateFile/旧房拆除情况模板.xls", lists[0]);
DataTable ds2 = tsmBll.GetSubsidyRecordsExport(startDate, endDate, RangeType, xzqh, xzc, ProjectName, "旧房拆除情况", "/TemplateFile/旧房拆除情况模板.xls", lists[1]);
DataTable ds3 = tsmBll.GetSubsidyRecordsExport(startDate, endDate, RangeType, xzqh, xzc, ProjectName, "新开工", "/TemplateFile/旧房拆除情况模板.xls", lists[2]);
DataTable ds4 = tsmBll.GetSubsidyRecordsExport(startDate, endDate, RangeType, xzqh, xzc, ProjectName, "在建项目", "/TemplateFile/旧房拆除情况模板.xls", lists[3]);
//将datatable放到dataset中去
DataTable temp = ds.Copy();
temp.TableName = "temp";
dataSet.Tables.Add(temp);
DataTable temp2 = ds2.Copy();
temp2.TableName = "temp2";
dataSet.Tables.Add(temp2);
DataTable temp3 = ds3.Copy();
temp3.TableName = "temp3";
dataSet.Tables.Add(temp3);
DataTable temp4 = ds4.Copy();
temp4.TableName = "temp4";
dataSet.Tables.Add(temp4);
DataSetToExcels(dataSet, "城中村改造报表.xls", "进度表,旧房拆除情况,新开工,在建项目", "/TemplateFile/城中村改造模板.xls", 5, null, lists, fristHead);
}
参数:数据源对象,导出后显示给用户的导出excel名称,excel的sheet名称集合,模板路径,插入数据源起始行,是否加尾部,4个sheet导出可选项的集合,标题
public static void DataSetToExcel(DataSet sourceDs, string fileName, string sheetName, string templateFilePath, int startRow, Action<HSSFWorkbook, ISheet, int, DataSet> CreateBottom, string lists, string fristHead)
{
//asp.net输出的Excel文件名
//如果文件名是中文的话,需要进行编码转换,否则浏览器看到的下载文件是乱码。
MemoryStream ms = ExportDataSetToExcel(sourceDs, sheetName, templateFilePath, startRow, CreateBottom, lists, fristHead) as MemoryStream;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
}
参数:数据源对象,4个sheet名称连接字符串,模板路径,起始插入行,是否插入尾部,4个sheet导出可选项的集合,标题
private static Stream ExportDataSetToExcel(DataSet sourceDs, string sheetName, string filePath, int startRow, Action<HSSFWorkbook, ISheet, int, DataSet> CreateBottom, string filednames, string fristHead)
{
//解析模板
MemoryStream ms = new MemoryStream();
string TempletFileName = System.Web.HttpContext.Current.Server.MapPath(filePath);
FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
HSSFWorkbook workbook = new HSSFWorkbook(file);
#region 设置样式
//设置数据行样式
ICellStyle rowData = workbook.CreateCellStyle();
IFont fontData = workbook.CreateFont();
rowData.Alignment = HorizontalAlignment.Center;
rowData.VerticalAlignment = VerticalAlignment.Center;
rowData.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
rowData.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
rowData.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
rowData.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
//fontData.FontHeightInPoints =11;
// fontData.FontName = "仿宋_GB2312";
fontData.FontName = "宋体";
rowData.SetFont(fontData);
#endregion
#region 生成excel数据
int indexnum = 1;//设置序号起始行
int startCell = 3;//合并单元格字段初始化,用于合并相应sheet中二级标题的单元格
int endCell = 4;//合并单元格字段初始化,用于合并相应sheet中二级标题的单元格
int cellnum =0;//添加可选导出项目列所在列数初始化
string filednames = "";
string[] lists = null;
int addCellNum = 0;//每个sheet添加的可选导出项列数
for (int i = 0; i < sheetNames.Length; i++)//一共几个sheet
{
var sheet = workbook.GetSheet(sheetNames[i]);//得到当前的sheet
if (i == 0)//进度表
{
//这是给第2行第一列的单元格设置值
sheet.GetRow(1).GetCell(0).SetCellValue("温州市城中村改造任务进度表" + fristHead); //第一行第0列的标题修改
}
if (i==2)//旧房
{
startCell = 2;
endCell = 3;
startRow = 4; //设置开始行
cellnum = 25;//添加列
sheet.GetRow(1).GetCell(0).SetCellValue("温州市城中村改造新开工项目统计表" + fristHead);//第一行第0列的标题修改
}
else if (i==1)
{
startRow = 5; //设置开始行
cellnum = 21;//添加列
sheet.GetRow(1).GetCell(0).SetCellValue("温州市城中村改造协议签订、旧房拆除情况统计表" + fristHead);
}
else if (i==3)//在建项目
{
startCell = 3;
endCell = 4;
cellnum = 14;//添加列
startRow = 5; //设置开始行
sheet.GetRow(1).GetCell(0).SetCellValue("温州市城中村改造项目建设情况统计表(含新开工、续建项目)" + fristHead);
}
if (filedLists[i].ToString() != "")
{
addCellNum = cellnum;
filednames = filedLists[i];
filednames = filednames.Substring(1); //删除第一个,
lists = filednames.Split(new char[] {','});
}
if(lists !=null)
{
foreach (var item in lists)
{
sheet.GetRow(startCell).CreateCell(addCellNum);//第3行的列创建
sheet.GetRow(endCell).CreateCell(addCellNum).SetCellValue(item);//第4行第21列加标题
sheet.GetRow(startCell).GetCell(addCellNum).CellStyle = rowData;//第三行的样式
sheet.GetRow(endCell).GetCell(addCellNum).CellStyle = rowData;//第四行的样式
addCellNum++;
}
sheet.AddMergedRegion(new CellRangeAddress(startCell, startCell, cellnum, addCellNum - 1));
sheet.GetRow(startCell).GetCell(cellnum).CellStyle = rowData;
sheet.GetRow(startCell).GetCell(cellnum).SetCellValue("可选项导出列");//第3行合并单元格
}
// excel内容从第一行开始
int rowIndex = startRow;
foreach (DataRow row in sourceDs.Tables[i].Rows)
{
// 第rowIndex行
var dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in sourceDs.Tables[i].Columns)
{
if (i != 0)
{
dataRow.CreateCell(0).SetCellValue(indexnum);
// 第column.Ordinal列
dataRow.CreateCell(column.Ordinal + 1).SetCellValue(row[column].ToString());
// 数据行样式
dataRow.Cells[column.Ordinal + 1].CellStyle = rowData;
dataRow.Cells[0].CellStyle = rowData;
}
else//进度表
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
// 数据行样式
dataRow.Cells[column.Ordinal].CellStyle = rowData;
}
}
rowIndex++;//行数
indexnum++;//列数
}
indexnum = 1;//让下一个sheet的序列号从1开始
}
#endregion
workbook.Write(ms);
ms.Flush();//关闭流
ms.Position = 0;//位置初始化
workbook = null;
return ms;
}