var templatePath = Server.MapPath(@"/Template/区域订单列表导出模板.xlsx"); //NPOIHelper.GetTemplateToExcel(templatePath,list); Workbook workbook = new Workbook(); workbook.Open(templatePath); Cells cells= workbook.Worksheets[0].Cells; if (list != null) { int row = 1;//交易单行数 //获取交易订单列表 foreach (var item in list) { var startmergepos = row; //// 获取交易订单对应的订单数,做合并行准备 var rowsorder = item.OrderList.Count; //合并单元格cells.Merge(1, 0, 3, 1) 参数1代表当前行,参数0代表当前行当前列即第一行第一列,参数3合并的行数,参数4合并的列数 cells.Merge(startmergepos, 0, rowsorder, 1); cells[startmergepos, 0].PutValue(item.DealCode); cells[startmergepos, 0].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center }); //设置单元格合并后垂直居中显示 cells.Merge(startmergepos, 1, rowsorder, 1); cells[startmergepos, 1].PutValue(item.TotalProductMoney.ToString("f2")); cells[startmergepos, 1].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center }); cells.Merge(startmergepos, 2, rowsorder, 1); cells[startmergepos, 2].PutValue(item.DeliveryFee.ToString("f2")); cells[startmergepos, 2].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center }); cells.Merge(startmergepos, 3, rowsorder, 1); cells[startmergepos, 3].PutValue((item.TotalMoney-item.RealityMoney).ToString("f2")); cells[startmergepos, 3].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center }); cells.Merge(startmergepos, 4, rowsorder, 1); cells[startmergepos, 4].PutValue(item.PayMentStr); cells[startmergepos, 4].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center }); cells.Merge(startmergepos, 5, rowsorder, 1); cells[startmergepos,5].PutValue(item.PayState==0?"未付款":"已付款"); cells[startmergepos, 5].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center }); for (int i = 0; i < rowsorder; i++) { cells[row + i, 6].PutValue(item.OrderList[i].OrderId); if (item.OrderList[i].ListOrder_Items!=null&&item.OrderList[i].ListOrder_Items.Count > 0) { cells[row + i, 7].PutValue(item.OrderList[i].ListOrder_Items[0].ProductName); cells[row + i, 8].PutValue(item.OrderList[i].ListOrder_Items[0].SpecHtml); cells[row + i, 8].SetStyle(new Style() { IsTextWrapped=true}); //cells.SetRowHeight(row + i, 200);//设置单元格高度 cells[row + i, 9].PutValue(item.OrderList[i].ListOrder_Items[0].Quantity); } else { cells[row + i, 7].PutValue(""); cells[row + i, 8].PutValue(""); cells[row + i, 9].PutValue(""); } cells[row + i, 10].PutValue(item.OrderList[i].TotalMoney); cells[row + i, 11].PutValue(item.OrderList[i].UserName); cells[row + i, 12].PutValue(item.OrderList[i].DepartMentName); cells[row + i, 13].PutValue(item.OrderList[i].CreateTime.ToString("yyyy-MM-dd hh:mm:ss")); cells[row + i, 14].PutValue(item.OrderList[i].StateString); if (item.OrderList[i].IsStock == 0) { cells[row + i, 15].PutValue("生产单"); } else if (item.OrderList[i].IsStock == 1) { cells[row + i, 15].PutValue("备库单"); } else if (item.OrderList[i].IsStock == 2) { cells[row + i, 15].PutValue("出库单"); } } //这是合并单元格后的行数,一定注意,要加上合并的行数,不然会实现不了上图的效果 row = startmergepos + rowsorder; } } var filename = HttpUtility.UrlEncode("区域订单列表", System.Text.Encoding.UTF8) +DateTime.Now.ToString("yyyyMMddhhmmss")+ ".xls"; //转换成流字节,输出浏览器下载 var byti = workbook.SaveToStream().GetBuffer(); ////通知浏览器保存文件,其实也就是输出到浏览器 Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename); Response.BinaryWrite(byti); Response.Flush(); Response.Close();