Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行
Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行
模版格式,图格式是最简单的格式,但实际效果不是这种,实际效果图如图2
图2 ,注意看红色部分,一对一是正常的,但是有一对多的订单,就得把前面的合并居中,后面对应多行显示
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();
一般步骤:
var templatePath = Server.MapPath(@"/Template/区域订单列表导出模板.xlsx");
1、初始化模版
Workbook workbook = new Workbook();
workbook.Open(templatePath);
2、获取模版的单元
Cells cells= workbook.Worksheets[0].Cells;
3、对不同的行的单元格赋值
cells[0, 0].PutValue("我是第一行第一列,也即是第一行第一个单元格");
说一下合并单元格,这个没捷径可走,只能提供什么意思,具体业务具体去合并
//合并单元格cells.Merge(1, 0, 3, 1) 参数1代表当前行,参数0代表当前行当前列即第一行第一列,参数3合并的行数,参数4合并的列数
cells.Merge(1, 0, 3, 1);
设置单元格的样式
cells[startmergepos, 1].SetStyle(new Style() { HorizontalAlignment = TextAlignmentType.Center });
设置单元格的高度
//cells.SetRowHeight(row + i, 200);