C# 使用aspose.cell编写打印功能

 

public ActionResult KyzPrint(string KeyValue)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
Aqjd_Jcjl jcjl = _repo.Find<Aqjd_Jcjl>(KeyValue);//实体类查询模型

Workbook wb = new Workbook(Server.MapPath("~/App_Data/Reports/Jdjc/客运站综合检查表.xlsx"));//打印模板存储地址
Worksheet ws = wb.Worksheets[0];//获取的打印模板的第一个sheet单页
Cells cells = ws.Cells;//获取单页的行
cells[1, 1].PutValue(jcjl.Jcdx);//对每个单行插入自己要插入的值cell【第几行,第几列】,都是从零开始切记
cells[3, 3].PutValue(jcjl.JcTime.ToString("yyyy-MM-dd HH:mm:ss"));
cells[4, 1].PutValue(jcjl.JcryName);
List<string> Jcxms = pfjgs.GroupBy(s => s.Jcxm).Select(s => s.Key).ToList();
int row = 6;
for (int i = 0; i < Jcxms.Count(); i++)
{
List<Aqjd_Pfjg> pfs = pfjgs.FindAll(s => s.Jcxm.Equals(Jcxms[i])).ToList();
cells.Merge(row, 0, pfs.Count(), 1);//合并单元格cells.Merge(oldCount, 12, y , x);其中oldCount为合并开始列,如我需要将2M-8M进行合并那么oldCount应为2M所在的行数既为:212表示合并开始行,如我需要将2M-8M进行合并那么12就为2M所在的列数即为:12y表示合并多少列,如2M-8M共7列x表示合并多少行如2M-8M共1行

cells[row, 0].PutValue(Jcxms[i]);

for (int j = 0; j < pfs.Count; j++)
{
cells.Merge(row, 1, 1, 3);
cells[row, 1].PutValue(pfs[j].Jcnr);
cells[row, 2].PutValue("通过" == pfs[j].isTg ? "通过" : pfs[j].Bz);
row++;
}
}

Style style = new Style();//新建单元格样式实例
style.HorizontalAlignment = TextAlignmentType.Left;//字体靠左对齐
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;////去除右边的边框,如果希望在两个单元格之间不要边框,需要分别两个单元格之前进行分别去除
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

cells.Merge(Row2 + 2, 0, 1, 2);

cells[Row2 +2, 0].SetStyle(style);//设置单元格格式
cells[Row2 + 2, 0].PutValue("检查人员确认签名: ");
cells.Merge(Row2 + 1, 2, 1, 3);
cells[Row2 + 2, 1].GetStyle().Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;;//去除左边的边框
cells[Row2 + 2, 1].GetStyle().HorizontalAlignment = TextAlignmentType.Right;//字体靠右对齐
cells[Row2 + 2, 1].PutValue("日期: 年 月 日");
//style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//去除上边框
//style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//去除下边框
//style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//去除左边框
//style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;//去除右边框
//style.HorizontalAlignment = TextAlignmentType.Center;//字体居中对齐
var docStream = new MemoryStream(); //创建文件流
wb.Save(docStream, SaveFormat.Pdf);//保存赋值的模板进行打印
docStream.Position = 0;
return new FileStreamResult(docStream, "application/pdf");//打印pdf
}

 

//更多单元格文字操作在https://www.cnblogs.com/hEnius/p/10494506.html查看

//关于更多更新尽请期待,下期分享Excel导入

原文地址:https://www.cnblogs.com/lovecwq/p/13083380.html