C# word、excel转PDF,读取json数据

╰+哭是因爲堅強的太久メ 2023-10-06 22:01 79阅读 0赞

在这里插入图片描述
在这里插入图片描述
使用json
在这里插入图片描述
在这里插入图片描述

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using MSWord = Microsoft.Office.Interop.Word;
  7. using Microsoft.Office.Interop.Excel;
  8. using System.IO;
  9. using System.Reflection;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Linq;
  12. //using Microsoft.Office.Tools.Word;
  13. namespace Console_WordSkill_All
  14. {
  15. class Program
  16. {
  17. static void Main(string[] args)
  18. {
  19. string str = Readjson("filePath");
  20. Console.WriteLine("结果为{0}", str);
  21. string postfix = CheckTrueFileName(str);
  22. Console.WriteLine(postfix);
  23. switch (postfix)
  24. {
  25. case ".doc":
  26. WordToPDF(str, postfix);
  27. break;
  28. case ".docx":
  29. WordToPDF(str, postfix);
  30. break;
  31. case ".xls":
  32. ConverterToPdf(str, postfix);
  33. break;
  34. case ".xlsx":
  35. ConverterToPdf(str, postfix);
  36. break;
  37. }
  38. //WordToPDF(str);
  39. }
  40. public static bool WordToPDF(string sourcePath, string postfix)
  41. {
  42. bool result = false;
  43. MSWord.Application application = new MSWord.Application();
  44. MSWord.Document document = null;
  45. try
  46. {
  47. application.Visible = false;
  48. document = application.Documents.Open(sourcePath);
  49. string PDFPath = sourcePath.Replace(postfix, ".pdf");//pdf存放位置
  50. Console.WriteLine(PDFPath);
  51. if (!File.Exists(@PDFPath))//存在PDF,不需要继续转换
  52. {
  53. document.ExportAsFixedFormat(PDFPath, MSWord.WdExportFormat.wdExportFormatPDF);
  54. }
  55. result = true;
  56. }
  57. catch (Exception e)
  58. {
  59. Console.WriteLine(e.Message);
  60. result = false;
  61. }
  62. finally
  63. {
  64. document.Close();
  65. }
  66. return result;
  67. }
  68. private static bool ConverterToPdf(string sourcePath, string postfix)
  69. {
  70. bool result = false;
  71. object missing = Type.Missing;
  72. Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
  73. // Microsoft.Office.Interop.Excel.ApplicationClass application = null;
  74. Microsoft.Office.Interop.Excel.Workbook workBook = null;
  75. try
  76. {
  77. //application = new Microsoft.Office.Interop.Excel.ApplicationClass();
  78. object target = sourcePath.Replace(postfix, ".pdf");//pdf存放位置
  79. Microsoft.Office.Interop.Excel.XlFixedFormatType type = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
  80. workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
  81. missing, missing, missing, missing, missing, missing, missing, missing, missing);
  82. Excel中某个工作簿另存为PDF文件
  83. //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets["工作簿名称或索引"];
  84. //worksheet.ExportAsFixedFormat(type, target, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
  85. //将整个Excel另存为PDF文件
  86. workBook.ExportAsFixedFormat(type, target, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
  87. result = true;
  88. }
  89. catch (Exception ex)
  90. {
  91. Console.WriteLine(ex.Message);
  92. result = false;
  93. }
  94. finally
  95. {
  96. if (workBook != null)
  97. {
  98. workBook.Close(false, missing, missing);
  99. workBook = null;
  100. }
  101. if (application != null)
  102. {
  103. application.Quit();
  104. application = null;
  105. }
  106. GC.Collect();
  107. GC.WaitForPendingFinalizers();
  108. }
  109. return result;
  110. }
  111. //读取json文件
  112. public static string Readjson(string key)
  113. {
  114. string jsonfile = "C:/create.json";//JSON文件路径
  115. using (System.IO.StreamReader file = new StreamReader(jsonfile, Encoding.Default))
  116. {
  117. using (JsonTextReader reader = new JsonTextReader(file))
  118. {
  119. JObject o = (JObject)JToken.ReadFrom(reader);
  120. var value = o[key].ToString();
  121. return value;
  122. }
  123. }
  124. }
  125. //判断文件类型
  126. public static string CheckTrueFileName(string path)
  127. {
  128. System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  129. System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
  130. string bx = " ";
  131. byte buffer;
  132. try
  133. {
  134. buffer = r.ReadByte();
  135. bx = buffer.ToString();
  136. buffer = r.ReadByte();
  137. bx += buffer.ToString();
  138. }
  139. catch (Exception exc)
  140. {
  141. Console.WriteLine(exc.Message);
  142. }
  143. r.Close();
  144. fs.Close();
  145. //真实的文件类型,数字
  146. //Console.WriteLine(bx);
  147. //文件名,包括后缀
  148. //Console.WriteLine(System.IO.Path.GetFileName(path));
  149. //文件名,不包括后缀
  150. //Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(path));
  151. //文件格式.doc
  152. //Console.WriteLine(System.IO.Path.GetExtension(path));
  153. return System.IO.Path.GetExtension(path);
  154. }
  155. }
  156. }

发表评论

表情:
评论列表 (有 0 条评论,79人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C# 读取PDF书签内容

    PDF书签常见于一些PDF格式的教程或文献等资料中,通过它,读者可以快速的知道每个章节讲述的内容,以此增加文档的可读性和结构性。本文将介绍如何使用C\快速读取一个PDF文档中的

    相关 C# word pdf

    将Word转换为带目录书签的PDF,待转换Word中应该有目录或书签,可以用Word中的标题来自动生成目录 office.interop.word 转pdf pub