WordUtil.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using DocumentFormat.OpenXml.Wordprocessing;
  4. using Spire.Doc;
  5. using System.IO;
  6. using Break = DocumentFormat.OpenXml.Wordprocessing.Break;
  7. using Document = Spire.Doc.Document;
  8. namespace UniformMaterialManagementSystem.Utils
  9. {
  10. public static class WordUtil
  11. {
  12. /// <summary>
  13. /// 按时间生成14位流水号
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string GenerateFileSerialNumber()
  17. {
  18. DateTime now = DateTime.Now;
  19. string datePart = now.ToString("yyyyMMddHHmmss");
  20. long ticks = now.Ticks - new DateTime(2000, 1, 1, 0, 0, 0).Ticks;
  21. string ticksPart = ticks.ToString().Substring(0, 14 - datePart.Length);
  22. return datePart + ticksPart;
  23. }
  24. /// <summary>
  25. /// 根据模板生成新的word文档,并替换文档内容
  26. /// </summary>
  27. public static void GenerateWordByTemplate(string templateFilePath,string targetFilePath, Dictionary<string, string> replaceContentDic)
  28. {
  29. File.Copy(templateFilePath, targetFilePath, true);
  30. using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(targetFilePath, true))
  31. {
  32. MainDocumentPart? mainDocPart = wordDocument.MainDocumentPart;
  33. //替换word内容
  34. UpdateWordDocumentText(replaceContentDic, mainDocPart);
  35. }
  36. }
  37. /// <summary>
  38. /// 将word文档保存为pdf文档
  39. /// </summary>
  40. public static void SaveWordToPdf(string inputFilePath, string outputFilePath)
  41. {
  42. Document document = new Document();
  43. document.LoadFromFile(inputFilePath);
  44. document.SaveToFile(outputFilePath, FileFormat.PDF);
  45. }
  46. /// <summary>
  47. /// 根据tagId替换word文档中的内容
  48. /// </summary>
  49. private static void UpdateWordDocumentText(Dictionary<string, string> tagValueDict, MainDocumentPart? mainDocPart)
  50. {
  51. foreach (var pair in tagValueDict)
  52. {
  53. var tagId = pair.Key;
  54. var value = pair.Value;
  55. foreach (var sdtElement in mainDocPart.Document.Body.Descendants<SdtElement>())
  56. {
  57. if (sdtElement.SdtProperties.GetFirstChild<Tag>().Val == tagId)
  58. {
  59. OpenXmlElement? parantElement = sdtElement.Descendants<Paragraph>().SingleOrDefault();
  60. if (null == parantElement)
  61. {
  62. SdtContentRun? cr = sdtElement.Descendants<SdtContentRun>().SingleOrDefault();
  63. parantElement = cr;
  64. }
  65. if (null != parantElement)
  66. {
  67. Run? run = parantElement.Descendants<Run>().SingleOrDefault();
  68. if (null != run)
  69. {
  70. Text? t = run.Descendants<Text>().SingleOrDefault();
  71. if (null != t)
  72. {
  73. if (value.Contains(Environment.NewLine))
  74. {
  75. var values = value.Split(Environment.NewLine);
  76. foreach (var line in values)
  77. {
  78. run.Append(new Text(line));
  79. run.Append(new Break());
  80. }
  81. }
  82. else
  83. {
  84. run.AppendChild(new Text(value));
  85. }
  86. run.RemoveChild(t);
  87. }
  88. }
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }