/// <summary> /// Creates the report in file format /// </summary> /// <param name="facility">the result of a DPoW validation to be transformed into report form.</param> /// <param name="suggestedFilename">target file for the spreadsheet (warning, the extension is automatically determined depending on the format)</param> /// <param name="format">determines the excel format to use</param> /// <returns>true if successful, errors are cought and passed to Logger</returns> public bool Create(Facility facility, string suggestedFilename, SpreadSheetFormat format) { var ssFileName = Path.ChangeExtension(suggestedFilename, format == SpreadSheetFormat.Xlsx ? "xlsx" : "xls"); if (File.Exists(ssFileName)) { File.Delete(ssFileName); } try { using (var spreadsheetStream = new FileStream(ssFileName, FileMode.Create, FileAccess.Write)) { var result = Create(facility, spreadsheetStream, format); spreadsheetStream.Close(); return result; } } catch (Exception e) { Logger.ErrorFormat("Failed to save {0}, {1}", ssFileName, e.Message); return false; } }
/// <summary> /// Creates the report in file format /// </summary> /// <param name="facility">the result of a DPoW validation to be transformed into report form.</param> /// <param name="suggestedFilename">target file for the spreadsheet (warning, the extension is automatically determined depending on the format)</param> /// <param name="format">determines the excel format to use</param> /// <returns>true if successful, errors are cought and passed to Logger</returns> public bool Create(Facility facility, string suggestedFilename, SpreadSheetFormat format) { var ssFileName = Path.ChangeExtension(suggestedFilename, format == SpreadSheetFormat.Xlsx ? "xlsx" : "xls"); if (File.Exists(ssFileName)) { File.Delete(ssFileName); } try { using (var spreadsheetStream = new FileStream(ssFileName, FileMode.Create, FileAccess.Write)) { var result = Create(facility, spreadsheetStream, format); spreadsheetStream.Close(); return(result); } } catch (Exception ex) { Logger.LogError(0, ex, "Failed to save {file}", ssFileName); return(false); } }
/// <summary> /// Creates the report. /// </summary> /// <param name="reportFacility">the result of a DPoW validation to be transformed into report form.</param> /// <param name="destinationStream">target stream for the spreadsheet</param> /// <param name="format">determines the excel format to use</param> /// <returns>true if successful, errors are cought and passed to Logger</returns> public bool Create(Facility reportFacility, Stream destinationStream, SpreadSheetFormat format) { var workBook = format == SpreadSheetFormat.Xlsx // ReSharper disable once RedundantCast ? (IWorkbook) new XSSFWorkbook() // ReSharper disable once RedundantCast : (IWorkbook) new HSSFWorkbook(); var facReport = new FacilityReport(reportFacility); var summaryPage = workBook.CreateSheet("Summary"); if (!CreateSummarySheet(summaryPage, reportFacility)) { return(false); } // reports on Documents // if (reportFacility.Documents != null) { var documentsPage = workBook.CreateSheet("Documents"); if (!CreateDocumentDetailsSheet(documentsPage, reportFacility.Documents)) { return(false); } } var iRunningWorkBook = 1; // reports on AssetTypes details // // ReSharper disable once LoopCanBeConvertedToQuery // might restore once code is stable foreach (var assetType in facReport.AssetRequirementGroups) { // only report items with any assets submitted (a different report should probably be provided otherwise) if (assetType.GetSubmittedAssetsCount() < 1) { continue; } var firstOrDefault = assetType.RequirementCategories.FirstOrDefault(cat => cat.Classification == @"Uniclass2015"); if (firstOrDefault == null) { continue; } var tName = firstOrDefault.Code; var validName = WorkbookUtil.CreateSafeSheetName(string.Format(@"{0} {1}", iRunningWorkBook++, tName)); var detailPage = workBook.CreateSheet(validName); if (!CreateDetailSheet(detailPage, assetType)) { return(false); } } // reports on Zones details // ReSharper disable once LoopCanBeConvertedToQuery // might restore once code is stable foreach (var zoneGroup in facReport.ZoneRequirementGroups) { // only report items with any assets submitted (a different report should probably be provided otherwise) if (zoneGroup.GetSubmittedAssetsCount() < 1) { continue; } var firstOrDefault = zoneGroup.RequirementCategories.FirstOrDefault(cat => cat.Classification == @"Uniclass2015"); if (firstOrDefault == null) { continue; } var tName = firstOrDefault.Code; var validName = WorkbookUtil.CreateSafeSheetName(string.Format(@"{0} {1}", iRunningWorkBook++, tName)); var detailPage = workBook.CreateSheet(validName); if (!CreateDetailSheet(detailPage, zoneGroup)) { return(false); } } try { workBook.Write(destinationStream); } catch (Exception ex) { Logger.LogError(0, ex, "Failed to stream excel report"); return(false); } return(true); }
/// <summary> /// Creates the report. /// </summary> /// <param name="reportFacility">the result of a DPoW validation to be transformed into report form.</param> /// <param name="destinationStream">target stream for the spreadsheet</param> /// <param name="format">determines the excel format to use</param> /// <returns>true if successful, errors are cought and passed to Logger</returns> public bool Create(Facility reportFacility, Stream destinationStream, SpreadSheetFormat format) { var workBook = format == SpreadSheetFormat.Xlsx // ReSharper disable once RedundantCast ? (IWorkbook)new XSSFWorkbook() // ReSharper disable once RedundantCast : (IWorkbook)new HSSFWorkbook(); var facReport = new FacilityReport(reportFacility); var summaryPage = workBook.CreateSheet("Summary"); if (!CreateSummarySheet(summaryPage, reportFacility)) return false; // reports on Documents // if (reportFacility.Documents != null) { var documentsPage = workBook.CreateSheet("Documents"); if (!CreateDocumentDetailsSheet(documentsPage, reportFacility.Documents)) return false; } var iRunningWorkBook = 1; // reports on AssetTypes details // // ReSharper disable once LoopCanBeConvertedToQuery // might restore once code is stable foreach (var assetType in facReport.AssetRequirementGroups) { // only report items with any assets submitted (a different report should probably be provided otherwise) if (assetType.GetSubmittedAssetsCount() < 1) continue; var firstOrDefault = assetType.RequirementCategories.FirstOrDefault(cat => cat.Classification == @"Uniclass2015"); if (firstOrDefault == null) continue; var tName = firstOrDefault.Code; var validName = WorkbookUtil.CreateSafeSheetName(string.Format(@"{0} {1}", iRunningWorkBook++, tName)); var detailPage = workBook.CreateSheet(validName); if (!CreateDetailSheet(detailPage, assetType)) return false; } // reports on Zones details // ReSharper disable once LoopCanBeConvertedToQuery // might restore once code is stable foreach (var zoneGroup in facReport.ZoneRequirementGroups) { // only report items with any assets submitted (a different report should probably be provided otherwise) if (zoneGroup.GetSubmittedAssetsCount() < 1) continue; var firstOrDefault = zoneGroup.RequirementCategories.FirstOrDefault(cat => cat.Classification == @"Uniclass2015"); if (firstOrDefault == null) continue; var tName = firstOrDefault.Code; var validName = WorkbookUtil.CreateSafeSheetName(string.Format(@"{0} {1}", iRunningWorkBook++, tName)); var detailPage = workBook.CreateSheet(validName); if (!CreateDetailSheet(detailPage, zoneGroup)) return false; } try { workBook.Write(destinationStream); } catch (Exception e) { Logger.ErrorFormat("Failed to stream excel report: {1}", e.Message); return false; } return true; }