using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OfficeOpenXml; using Spire.Xls; using System.IO; namespace WebApplication.controller { public class printInfo { public void WriteDataToExcelTemplate(string templatePath, string outputPath, string data,string name) { //WriteDataToExcelTemplate(templatePath, outputPath, data); // 加载 Excel 模板文件 ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (ExcelPackage package = new ExcelPackage(new FileInfo(templatePath))) { switch (name) { case "采购申请": ExcelWorksheet worksheetCG = package.Workbook.Worksheets[0]; string nameCG = worksheetCG.ToString(); if (nameCG == "采购申请") { //int colCount = worksheet.Dimension.End.Column; //get Column Count //int rowCount = worksheet.Dimension.End.Row; //get row count // 遍历数据,逐行写入到工作表中 int i = 10; // 第一行是标题,从第二行开始写入数据 worksheetCG.Cells["B2"].Value = "合同编号"; worksheetCG.Cells["F2"].Value = "创建时间"; worksheetCG.Cells["B3"].Value = "拥有者"; worksheetCG.Cells["F4"].Value = "所属部门"; worksheetCG.Cells["B4"].Value = "主题"; worksheetCG.Cells["B5"].Value = "合同名称"; worksheetCG.Cells["B6"].Value = "供应商"; worksheetCG.Cells["B7"].Value = "联系人"; worksheetCG.Cells["F7"].Value = "联系方式"; worksheetCG.Cells["B8"].Value = "采购内容类型"; worksheetCG.Cells["F8"].Value = "合同金额"; //for (int row = 10; row <= data.Count; row++) //{ //} worksheetCG.Cells["B11"].Value = "合同事由"; worksheetCG.Cells["B12"].Value = "备注"; } break; case "OA审批": JObject arrdata = JsonConvert.DeserializeObject(data); ExcelWorksheet worksheetOA = package.Workbook.Worksheets[0]; string nameOA = worksheetOA.ToString(); if (nameOA == "OA审批") { worksheetOA.Cells["B2"].Value = arrdata["F0000001"].ToString(); //标题 worksheetOA.Cells["F2"].Value = arrdata["F0000006"].ToString(); //创建人 worksheetOA.Cells["B3"].Value = arrdata["F0000004"].ToString(); //审批单号 worksheetOA.Cells["B4"].Value = arrdata["F0000007"].ToString(); //结束时间 worksheetOA.Cells["F4"].Value = arrdata["F0000002"].ToString(); //发起时间 worksheetOA.Cells["B5"].Value = arrdata["F0000009"].ToString(); //时长 worksheetOA.Cells["B6"].Value = arrdata["F0000010"].ToString(); //同行人 worksheetOA.Cells["B7"].Value = arrdata["F0000011"].ToString(); //事由 worksheetOA.Cells["B8"].Value = arrdata["F0000012"].ToString(); //费用出处 worksheetOA.Cells["B9"].Value = arrdata["F0000005"].ToString(); //附件 worksheetOA.Cells["B10"].Value = arrdata["F0000013"].ToString(); //说明 worksheetOA.Cells["B11"].Value = arrdata["F0000014"].ToString(); //审批流程 } break; } //excelToPdf(new FileInfo(outputPath), outputPath) // 保存 Excel 文件 package.SaveAs(new FileInfo(outputPath)); } } /// /// Excel转PDF /// /// 文件地址 /// 输出文件地址 public void excelToPdf(string LoadFromFile, string SaveToPdf) { Workbook workbook = new Workbook(); workbook.LoadFromFile(LoadFromFile); Worksheet sheet = workbook.Worksheets[0]; sheet.SaveToPdf(SaveToPdf); } } }