AsposeHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Aspose.Cells;
  2. using Aspose.Words;
  3. using System.Data;
  4. namespace OASystem.API.OAMethodLib.File
  5. {
  6. /// <summary>
  7. /// asponse 文档操作
  8. /// </summary>
  9. public class AsposeHelper
  10. {
  11. public const string asposeKey = "DQo8TGljZW5zZT4NCjxEYXRhPg0KPExpY2Vuc2VkVG8+VGhlIFdvcmxkIEJhbms8L0xpY2Vuc2VkVG8+DQo8RW1haWxUbz5ra3VtYXIzQHdvcmxkYmFua2dyb3VwLm9yZzwvRW1haWxUbz4NCjxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgU21hbGwgQnVzaW5lc3M8L0xpY2Vuc2VUeXBlPg0KPExpY2Vuc2VOb3RlPjEgRGV2ZWxvcGVyIEFuZCAxIERlcGxveW1lbnQgTG9jYXRpb248L0xpY2Vuc2VOb3RlPg0KPE9yZGVySUQ+MjEwMzE2MTg1OTU3PC9PcmRlcklEPg0KPFVzZXJJRD43NDQ5MTY8L1VzZXJJRD4NCjxPRU0+VGhpcyBpcyBub3QgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KPFByb2R1Y3RzPg0KPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0Pg0KPC9Qcm9kdWN0cz4NCjxFZGl0aW9uVHlwZT5Qcm9mZXNzaW9uYWw8L0VkaXRpb25UeXBlPg0KPFNlcmlhbE51bWJlcj4wM2ZiMTk5YS01YzhhLTQ4ZGItOTkyZS1kMDg0ZmYwNjZkMGM8L1NlcmlhbE51bWJlcj4NCjxTdWJzY3JpcHRpb25FeHBpcnk+MjAyMjA1MTY8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCjxMaWNlbnNlVmVyc2lvbj4zLjA8L0xpY2Vuc2VWZXJzaW9uPg0KPExpY2Vuc2VJbnN0cnVjdGlvbnM+aHR0cHM6Ly9wdXJjaGFzZS5hc3Bvc2UuY29tL3BvbGljaWVzL3VzZS1saWNlbnNlPC9MaWNlbnNlSW5zdHJ1Y3Rpb25zPg0KPC9EYXRhPg0KPFNpZ25hdHVyZT5XbkJYNnJOdHpCclNMV3pBdFlqOEtkdDFLSUI5MlFrL2xEbFNmMlM1TFRIWGdkcS9QQ2NqWHVORmp0NEJuRmZwNFZLc3VsSjhWeFExakIwbmM0R1lWcWZLek14SFFkaXFuZU03NTJaMjlPbmdyVW40Yk0rc1l6WWVSTE9UOEpxbE9RN05rRFU0bUk2Z1VyQ3dxcjdnUVYxbDJJWkJxNXMzTEFHMFRjQ1ZncEE9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+DQo=";
  12. #region DownWordToModel
  13. /// <summary>
  14. /// 根据Word模板进行数据的导出Word操作
  15. /// </summary>
  16. /// <param name="tempFileName">模板文件名称</param>
  17. /// <param name="saveFolderName">保存文件夹名称</param>
  18. /// <param name="dic">数据源 key 对应 value</param>
  19. /// <param name="listDt">需要循环的数据DataTable 多个注意DataTableName </param>
  20. /// <returns>返回的文档路径</returns>
  21. public static string ExpertWordToModel(string tempFileName, string saveFolderName, string saveFilName, Dictionary<string, object> dic,
  22. List<DataTable>? listDt)
  23. {
  24. try
  25. {
  26. removeWatermark();//加载监听,用于去除水印
  27. List<string> fieldNames = new List<string>(); //字段名字符串数组
  28. List<object> fieldValues = new List<object>(); //字段值数组
  29. foreach (string key in dic.Keys)
  30. {
  31. fieldNames.Add(key);
  32. fieldValues.Add(dic[key]);
  33. }
  34. string tempPath = string.Format("{0}/Word/Template/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), tempFileName);
  35. //// 将文本编码为字节序列
  36. //byte[] encodedBytes = Encoding.UTF8.GetBytes(tempPath);
  37. //// 解码字节序列为文本
  38. //tempPath = Encoding.UTF8.GetString(encodedBytes);
  39. //载入模板
  40. Document doc = new Document(tempPath);
  41. doc.MailMerge.Execute(fieldNames.ToArray(), fieldValues.ToArray());
  42. //将我们获得的DataTable类型的数据:EduDataTable放入doc方法中做处理
  43. if (listDt != null && listDt.Count > 0)
  44. {
  45. foreach (DataTable dt in listDt)
  46. {
  47. if (dt != null && dt.Rows.Count > 0)
  48. {
  49. doc.MailMerge.ExecuteWithRegions(dt);
  50. }
  51. }
  52. }
  53. //合并模版,相当于页面的渲染
  54. doc.MailMerge.Execute(new[] { "PageCount" }, new object[] { doc.PageCount });
  55. //"OfficeTempBasePath": "C:/Server/File/OA2023/Office/"
  56. //saveFolderName:TencentOCR
  57. string path = string.Format("{0}/Word/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), saveFolderName);
  58. if (!System.IO.Directory.Exists(path))
  59. {
  60. System.IO.Directory.CreateDirectory(path);//不存在就创建文件夹
  61. }
  62. string saveFilePath = string.Format("{0}/{1}", path, saveFilName);
  63. //保存合并后的文档
  64. doc.Save(saveFilePath);
  65. saveFilePath = saveFilePath.Replace("D:/FTP/File/OA2023/", AppSettingsHelper.Get("OfficeBaseUrl")); //返回下载地址
  66. return saveFilePath; //返回下载地址
  67. }
  68. catch (Exception ex)
  69. {
  70. return ex.Message;
  71. }
  72. }
  73. /// <summary>
  74. /// 加载监听,用于去除水印
  75. /// </summary>
  76. public static void removeWatermark()
  77. {
  78. new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9F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1cmU+CjwvTGljZW5zZT4=")));
  79. }
  80. ///// <summary>
  81. ///// 加载监听,用于去除水印
  82. ///// </summary>
  83. //public static void removeWatermark()
  84. //{
  85. // new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4yMDA2MDIwMTI2MzM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+OTM2ZTVmZDEtODY2Mi00YWJmLTk1YmQtYzhkYzBmNTNhZmE2PC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjEwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT5wSkpjQndRdnYxV1NxZ1kyOHFJYUFKSysvTFFVWWRrQ2x5THE2RUNLU0xDQ3dMNkEwMkJFTnh5L3JzQ1V3UExXbjV2bTl0TDRQRXE1aFAzY2s0WnhEejFiK1JIWTBuQkh1SEhBY01TL1BSeEJES0NGbWg1QVFZRTlrT0FxSzM5NVBSWmJRSGowOUNGTElVUzBMdnRmVkp5cUhjblJvU3dPQnVqT1oyeDc4WFE9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));
  86. //}
  87. #endregion
  88. #region DownExcelToModel
  89. /// <summary>
  90. /// 根据Word模板进行数据的导出Word操作
  91. /// </summary>
  92. /// <param name="tempFileName">Excel模板文件名称</param>
  93. /// <param name="saveFolderName">保存文件夹名称</param>
  94. /// <param name="saveFilName">保存文件名称</param>
  95. /// <param name="dic">数据源 key 对应 value</param>
  96. /// <param name="listDt">需要循环的数据DataTable 多个注意DataTableName </param>
  97. /// <returns>返回的文档路径</returns>
  98. public static string ExpertExcelToModel(string tempFileName, string saveFolderName, string saveFilName, Dictionary<string, object> dic,
  99. List<DataTable>? listDt)
  100. {
  101. try
  102. {
  103. string tempPath = string.Format("{0}/Excel/Template/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), tempFileName);
  104. //载入模板
  105. Workbook wbook = new Workbook(tempPath);
  106. //默认第一个工作表
  107. Worksheet wSheet = wbook.Worksheets[0];
  108. WorkbookDesigner designer = new WorkbookDesigner(wbook);
  109. //数据源 多个
  110. if (listDt != null && listDt.Count > 0)
  111. {
  112. foreach (var item in listDt)
  113. {
  114. if (item.Rows.Count > 0)
  115. {
  116. //designer.SetDataSource(item.TableName, item);
  117. designer.SetDataSource(item);
  118. }
  119. }
  120. }
  121. //报表单位
  122. if (dic.Count > 0)
  123. {
  124. foreach (var item in dic)
  125. {
  126. designer.SetDataSource(item.Key, item.Value);
  127. }
  128. }
  129. designer.Process();
  130. string path = string.Format("{0}/Excel/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), saveFolderName);
  131. if (!System.IO.Directory.Exists(path))
  132. {
  133. System.IO.Directory.CreateDirectory(path);//不存在就创建文件夹
  134. }
  135. string saveFilePath = string.Format("{0}/{1}", path, saveFilName);
  136. //保存合并后的文档
  137. wbook.Save(saveFilePath);
  138. //saveFilePath = saveFilePath.Replace("C:/Server/File/0A2023/", AppSettingsHelper.Get("OfficeBaseUrl")); //返回下载地址
  139. saveFilePath = AppSettingsHelper.Get("OfficeBaseUrl") + "/Office/Excel/" + saveFolderName + "/" + saveFilName;
  140. return saveFilePath; //返回下载地址
  141. }
  142. catch (Exception ex)
  143. {
  144. return ex.Message;
  145. }
  146. }
  147. #endregion
  148. #region Aspose.Words 21.8.0 去水印
  149. /// <summary>
  150. /// Aspose.Words 去水印
  151. /// version 21.8.0
  152. /// </summary>
  153. public static void removewatermark_v2180()
  154. {
  155. new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String(asposeKey)));
  156. }
  157. #endregion
  158. #region List<T> 转 Datatbale
  159. /// <summary>
  160. /// 将List转换为DataTable
  161. /// </summary>
  162. /// <param name="newTableName">newTableName</param>
  163. /// <param name="list">请求数据</param>
  164. /// <returns></returns>
  165. public static DataTable ListToDataTable<T>(string newTableName, List<T> list)
  166. {
  167. //创建一个名为"tableName"的空表
  168. DataTable dt = new DataTable(newTableName);
  169. //创建传入对象名称的列
  170. foreach (var item in list.FirstOrDefault().GetType().GetProperties())
  171. {
  172. dt.Columns.Add(item.Name);
  173. }
  174. //循环存储
  175. foreach (var item in list)
  176. {
  177. //新加行
  178. DataRow value = dt.NewRow();
  179. //根据DataTable中的值,进行对应的赋值
  180. foreach (DataColumn dtColumn in dt.Columns)
  181. {
  182. int i = dt.Columns.IndexOf(dtColumn);
  183. //基元元素,直接复制,对象类型等,进行序列化
  184. if (value.GetType().IsPrimitive)
  185. {
  186. value[i] = item.GetType().GetProperty(dtColumn.ColumnName).GetValue(item);
  187. }
  188. else
  189. {
  190. value[i] = JsonConvert.SerializeObject(item.GetType().GetProperty(dtColumn.ColumnName).GetValue(item));
  191. }
  192. }
  193. dt.Rows.Add(value);
  194. }
  195. return dt;
  196. }
  197. #endregion
  198. }
  199. }