DCRExport.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* ==============================================================================
  2. * 功能描述:数据检查结果导出
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/11/5 14:33:42
  5. * ==============================================================================*/
  6. using System;
  7. using System.IO;
  8. using Microsoft.Win32;
  9. using NPOI.SS.UserModel;
  10. using NPOI.XSSF.UserModel;
  11. using Saga.PlugIn.CreateFacility;
  12. using SAGA.DotNetUtils;
  13. using SAGA.DotNetUtils.Others;
  14. namespace Saga.PlugIn.ModelCheck
  15. {
  16. /// <summary>
  17. /// DataCheckResultExport
  18. /// </summary>
  19. public static class DCRExport
  20. {
  21. public static string MCRPath = Path.Combine(AppBaseInfo.DllRunPath, @"ModelCheck\ExcelTemplate\模型检查结果输出格式-模版.xlsx");
  22. public static string VerticalPipePath = Path.Combine(AppBaseInfo.DllRunPath, @"VerticalPipeCheck\ExcelTemplate\立管对齐检查-模板.xlsx");
  23. /// <summary>
  24. /// 获取导出Excel
  25. /// </summary>
  26. /// <returns></returns>
  27. public static string GetExportPath(string defaultPath = null)
  28. {
  29. SaveFileDialog sflg = new SaveFileDialog();
  30. if (File.Exists(defaultPath))
  31. sflg.FileName = defaultPath;
  32. sflg.Filter = "Excel(*.xlsx)|*.xlsx";
  33. if (sflg.ShowDialog() != true) return defaultPath;
  34. string fileName = sflg.FileName;
  35. if (fileName.IsNullOrEmpty())
  36. {
  37. fileName = defaultPath;
  38. }
  39. return fileName;
  40. }
  41. private static string m_FileName= MCRPath;
  42. /// <summary>
  43. /// 设置模版地址
  44. /// </summary>
  45. /// <param name="fileName"></param>
  46. public static void SetTemplatePath(string fileName)
  47. {
  48. m_FileName = fileName;
  49. }
  50. private static IWorkbook m_ExportWorkBook;
  51. public static IWorkbook GetWorkbook(bool isreset = false)
  52. {
  53. if (isreset)
  54. {
  55. m_ExportWorkBook = null;
  56. }
  57. if (m_ExportWorkBook == null)
  58. {
  59. if (File.Exists(m_FileName))
  60. {
  61. using (var fileStream = new System.IO.FileStream(m_FileName, FileMode.Open, FileAccess.Read))
  62. {
  63. m_ExportWorkBook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook
  64. }
  65. }
  66. else
  67. {
  68. m_ExportWorkBook = null;
  69. }
  70. }
  71. return m_ExportWorkBook;
  72. }
  73. /// <summary>
  74. /// 联接到指定Sheet页
  75. /// </summary>
  76. /// <param name="sheetName"></param>
  77. /// <returns></returns>
  78. public static XSSFHyperlink CreateLink(string sheetName)
  79. {
  80. XSSFHyperlink link=new XSSFHyperlink(HyperlinkType.Document);
  81. link.Address = $"'{sheetName}'!A1";
  82. return link;
  83. }
  84. /// <summary>
  85. /// 重置workbook
  86. /// </summary>
  87. public static void ClearWorkbook(string path)
  88. {
  89. m_ExportWorkBook = null;
  90. SetTemplatePath(path);
  91. }
  92. /// <summary>
  93. /// 保存
  94. /// </summary>
  95. /// <param name="fileName"></param>
  96. /// <param name="book"></param>
  97. /// <returns></returns>
  98. public static bool Save(string fileName, IWorkbook book)
  99. {
  100. bool result = true;
  101. try
  102. {
  103. //bool allVisible = true;
  104. ////判断全部都隐藏不保存
  105. //for (int i = 0; i < book.NumberOfSheets - 1; i++)
  106. //{
  107. // bool tempVisible = book.IsSheetVeryHidden(i);
  108. // if (!tempVisible)
  109. // {
  110. // allVisible = false;
  111. // }
  112. //}
  113. //if (allVisible)
  114. //{
  115. // MessageShowBase.Infomation("所有检查项都通过检查,取消保存");
  116. // return result;
  117. //}
  118. MemoryStream ms = new MemoryStream();
  119. book.Write(ms);
  120. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  121. {
  122. byte[] data = ms.ToArray();
  123. fs.Write(data, 0, data.Length);
  124. fs.Flush();
  125. }
  126. ms.Close();
  127. ms.Dispose();
  128. }
  129. catch (Exception e)
  130. {
  131. Console.WriteLine(e);
  132. result = false;
  133. }
  134. return result;
  135. }
  136. }
  137. }