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導入


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM