123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /* ==============================================================================
- * 功能描述:数据检查结果导出
- * 创 建 者:Garrett
- * 创建日期:2018/11/5 14:33:42
- * ==============================================================================*/
- using System;
- using System.IO;
- using Microsoft.Win32;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using Saga.PlugIn.CreateFacility;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Others;
- namespace Saga.PlugIn.ModelCheck
- {
- /// <summary>
- /// DataCheckResultExport
- /// </summary>
- public static class DCRExport
- {
- public static string MCRPath = Path.Combine(AppBaseInfo.DllRunPath, @"ModelCheck\ExcelTemplate\模型检查结果输出格式-模版.xlsx");
- public static string VerticalPipePath = Path.Combine(AppBaseInfo.DllRunPath, @"VerticalPipeCheck\ExcelTemplate\立管对齐检查-模板.xlsx");
- /// <summary>
- /// 获取导出Excel
- /// </summary>
- /// <returns></returns>
- public static string GetExportPath(string defaultPath = null)
- {
- SaveFileDialog sflg = new SaveFileDialog();
- if (File.Exists(defaultPath))
- sflg.FileName = defaultPath;
- sflg.Filter = "Excel(*.xlsx)|*.xlsx";
- if (sflg.ShowDialog() != true) return defaultPath;
- string fileName = sflg.FileName;
- if (fileName.IsNullOrEmpty())
- {
- fileName = defaultPath;
- }
- return fileName;
- }
- private static string m_FileName= MCRPath;
- /// <summary>
- /// 设置模版地址
- /// </summary>
- /// <param name="fileName"></param>
- public static void SetTemplatePath(string fileName)
- {
- m_FileName = fileName;
- }
- private static IWorkbook m_ExportWorkBook;
- public static IWorkbook GetWorkbook(bool isreset = false)
- {
- if (isreset)
- {
- m_ExportWorkBook = null;
- }
- if (m_ExportWorkBook == null)
- {
- if (File.Exists(m_FileName))
- {
- using (var fileStream = new System.IO.FileStream(m_FileName, FileMode.Open, FileAccess.Read))
- {
- m_ExportWorkBook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook
- }
- }
- else
- {
- m_ExportWorkBook = null;
- }
- }
- return m_ExportWorkBook;
- }
- /// <summary>
- /// 联接到指定Sheet页
- /// </summary>
- /// <param name="sheetName"></param>
- /// <returns></returns>
- public static XSSFHyperlink CreateLink(string sheetName)
- {
- XSSFHyperlink link=new XSSFHyperlink(HyperlinkType.Document);
- link.Address = $"'{sheetName}'!A1";
- return link;
- }
- /// <summary>
- /// 重置workbook
- /// </summary>
- public static void ClearWorkbook(string path)
- {
- m_ExportWorkBook = null;
- SetTemplatePath(path);
- }
- /// <summary>
- /// 保存
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="book"></param>
- /// <returns></returns>
- public static bool Save(string fileName, IWorkbook book)
- {
- bool result = true;
- try
- {
- //bool allVisible = true;
- ////判断全部都隐藏不保存
- //for (int i = 0; i < book.NumberOfSheets - 1; i++)
- //{
- // bool tempVisible = book.IsSheetVeryHidden(i);
- // if (!tempVisible)
- // {
- // allVisible = false;
- // }
- //}
- //if (allVisible)
- //{
- // MessageShowBase.Infomation("所有检查项都通过检查,取消保存");
- // return result;
- //}
- MemoryStream ms = new MemoryStream();
- book.Write(ms);
- using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- byte[] data = ms.ToArray();
- fs.Write(data, 0, data.Length);
- fs.Flush();
- }
- ms.Close();
- ms.Dispose();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- result = false;
- }
- return result;
- }
- }
- }
|