123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using UniformMaterialManagementSystem.Entities;
- using UniformMaterialManagementSystem.Models;
- namespace UniformMaterialManagementSystem.Utils
- {
- public static class InspectionReportUtil
- {
- public static void ExportInspectionReportFile(InspectionReportModel inspectionReport)
- {
- const string templateFilePath = "Template\\军需物资质量监督检验报告模板.docx";
- SaveFileDialog saveFileDialog = new SaveFileDialog()
- {
- Title = "请选择检验报告保存位置",
- FileName = "被装材料检验报告-" + inspectionReport.ReportNo + "-" + WordUtil.GenerateFileSerialNumber(),
- Filter = "PDF文件格式 (*.pdf)|*.pdf",
- };
- if (saveFileDialog.ShowDialog() == false) return;
- var destinationFile = saveFileDialog.FileName;
- var dictionary = CreateDictionary(inspectionReport);
- var tempFile = $"Template\\temp_{WordUtil.GenerateFileSerialNumber()}.docx";
- WordUtil.GenerateWordByTemplate(templateFilePath, tempFile, dictionary);
- WordUtil.SaveWordToPdf(tempFile, destinationFile);
- //删除临时文件
- File.Delete(tempFile);
- MessageBox.Show("文档已生成!");
- }
- private static Dictionary<string, string> CreateDictionary(InspectionReportModel inspectionReport)
- {
- Dictionary<string, string> dictionary = new()
- {
- { "ReportNo", inspectionReport.ReportNo },
- { "Department", inspectionReport.Department },
- { "ReportTime", inspectionReport.ReportTime.ToString("yyyy-MM-dd") }
- };
- //检验类别
- switch (inspectionReport.InspectApply.InspCategory)
- {
- case "报样检验":
- dictionary.Add("FirstSelected", "\u25a0");//选中框
- break;
- case "首批检验":
- dictionary.Add("SecondSelected", "\u25a0");//选中框
- break;
- case "过程检验":
- dictionary.Add("ThirdSelected", "\u25a0");//选中框
- break;
- case "出厂检验":
- dictionary.Add("ForthSelected", "\u25a0");//选中框
- break;
- }
- dictionary.Add("ProductName", inspectionReport.InspectApply.ProductName);
- dictionary.Add("Company", inspectionReport.InspectApply.Company);
- dictionary.Add("InspQuantity", inspectionReport.InspectApply.InspQuantity + inspectionReport.InspectApply.Material.MeasureUnit);
- dictionary.Add("ProductDate", inspectionReport.InspectApply.StartProductDate.ToString("yyyy-MM-dd") + "至" + inspectionReport.InspectApply.EndProductDate.ToString("yyyy-MM-dd"));
- //采购机构、合同号
- StringBuilder purchaseCompanies = new StringBuilder();
- StringBuilder contractNos = new StringBuilder();
- foreach (var detail in inspectionReport.InspectApply.InspectApplyContractDetails)
- {
- purchaseCompanies.Append(detail.PurchaseCompanyShortName).Append(" ");
- contractNos.Append(detail.ContractNo).Append(";");
- }
- dictionary.Add("PurchaseCompany", purchaseCompanies.ToString().Substring(0, purchaseCompanies.Length - 1));
- dictionary.Add("ContractNumber", contractNos.ToString().Substring(0, contractNos.Length - 1));
- dictionary.Add("ReportBasis", inspectionReport.ReportBasis);
- dictionary.Add("ReportDesc", inspectionReport.ReportDesc);
- dictionary.Add("ConclusionDesc", inspectionReport.ConclusionDesc);
- //验收组
- for (int i = 0; i < 5; i++)
- {
- if (i < inspectionReport.InspectionReportDetails.Count)
- {
- var detail = inspectionReport.InspectionReportDetails[i];
- dictionary.Add("JobCategory" + i, detail.JobCategory);
- dictionary.Add("SupervisionUnit" + i, detail.SupervisionUnit);
- dictionary.Add("Inspector" + i, detail.Inspector);
- }
- else
- {
- dictionary.Add("JobCategory" + i, string.Empty);
- dictionary.Add("SupervisionUnit" + i, string.Empty);
- dictionary.Add("Inspector" + i, string.Empty);
- }
- }
- dictionary.Add("EditUser", inspectionReport.EditUser);
- return dictionary;
- }
- }
- }
|