导出EXCEL 模板,可以带数据
写一个三行的表头,有跨行和跨列,有时候会搞不清楚,所以记录下来
下面展示一些 代码
。
//引用
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
//用到的主要属性
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol);
public JsonResult DaoChuExcel()
{
try
{
//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
`` #region 头部
//给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("序号)");
//合并单元格(当前列,跨越列,当前行,跨越行)
CellRangeAddress region = new CellRangeAddress(0, 2, 0, 0);
sheet1.AddMergedRegion(region);
row1.CreateCell(1).SetCellValue("路名");
CellRangeAddress region2 = new CellRangeAddress(0, 2, 1, 1);
sheet1.AddMergedRegion(region2);
row1.CreateCell(2).SetCellValue("路段");
CellRangeAddress region3 = new CellRangeAddress(0, 2, 2, 2);
sheet1.AddMergedRegion(region3);
row1.CreateCell(3).SetCellValue("长度(公里)");
CellRangeAddress region4 = new CellRangeAddress(0, 2, 3, 3);
sheet1.AddMergedRegion(region4);
row1.CreateCell(4).SetCellValue("建设单位");
CellRangeAddress region5 = new CellRangeAddress(0, 2, 4, 4);
sheet1.AddMergedRegion(region5);
row1.CreateCell(5).SetCellValue("电力相关");
CellRangeAddress region6 = new CellRangeAddress(0, 0, 5, 13);
sheet1.AddMergedRegion(region6);
row1.CreateCell(14).SetCellValue("信息相关");
CellRangeAddress region7 = new CellRangeAddress(0, 0, 14, 19);
sheet1.AddMergedRegion(region7);
//给sheet1添加第二行的头部标题
NPOI.SS.UserModel.IRow row2 = sheet1.CreateRow(1);
row2.CreateCell(5).SetCellValue("电力井(个)");
CellRangeAddress region10 = new CellRangeAddress(1, 1, 5, 7);
sheet1.AddMergedRegion(region10);
row2.CreateCell(8).SetCellValue("电力排管(米)");
CellRangeAddress region11 = new CellRangeAddress(1, 1, 8, 10);
sheet1.AddMergedRegion(region11);
row2.CreateCell(11).SetCellValue("直埋电缆(米)");
CellRangeAddress region12 = new CellRangeAddress(1, 1, 11, 13);
sheet1.AddMergedRegion(region12);
row2.CreateCell(14).SetCellValue("信息井(个)");
CellRangeAddress region13 = new CellRangeAddress(1, 1, 14, 16);
sheet1.AddMergedRegion(region13);
row2.CreateCell(17).SetCellValue("信息排管(米)");
CellRangeAddress region14 = new CellRangeAddress(1, 1, 17, 19);
sheet1.AddMergedRegion(region14);
//给sheet1添加第三行的头部标题
NPOI.SS.UserModel.IRow row3 = sheet1.CreateRow(2);
row3.CreateCell(5).SetCellValue("总量");
row3.CreateCell(6).SetCellValue("完成量");
row3.CreateCell(7).SetCellValue("完成率");
row3.CreateCell(8).SetCellValue("总量");
row3.CreateCell(9).SetCellValue("完成量");
row3.CreateCell(10).SetCellValue("完成率");
row3.CreateCell(11).SetCellValue("总量");
row3.CreateCell(12).SetCellValue("完成量");
row3.CreateCell(13).SetCellValue("完成率");
row3.CreateCell(14).SetCellValue("总量");
row3.CreateCell(15).SetCellValue("完成量");
row3.CreateCell(16).SetCellValue("完成率");
row3.CreateCell(17).SetCellValue("总量");
row3.CreateCell(18).SetCellValue("完成量");
row3.CreateCell(19).SetCellValue("完成率");
#endregion`
//下面是填充数据
for (int i = 0; i < Datalist.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 3);
rowtemp.CreateCell(0).SetCellValue(Datalist[i]);
}
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
string fileName = "xxxxxxxx" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
string urlPath = "upload/execl/" + fileName; // 文件下载的URL地址,供给前台下载
string filePath = System.Web.HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路径
FileStream file = new FileStream(filePath, FileMode.Create);
book.Write(file);
file.Close();
return Tojson.retjson(true, "/" + urlPath);
}
catch (Exception ex)
{
return Tojson.retjson(false, ex.Message);
}
}
结果
最后附上导出excel 的效果图