public void RenameReport() { if (ReportFileName == "") { ReportFileName = "TestResult"; } else { ReportFileName = ReportFileName.Replace(".html", ""); } var currentFilePath = Path.Combine(ReportFolder, "index.html"); var newFilePath = Path.Combine(ReportFolder, $"{ReportFileName}_{DateTime.Now:yyyyMMdd-HHmmss}.html"); File.Move(currentFilePath, newFilePath); }
private void ConvertToPdfIfNeeded(string tmpReportFile) { // convert docx or pptx to pdf if (ReportFileName.Contains(".pdf")) { if (tmpReportFile.Contains(".docx")) { try { Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application(); Document wordDocument = appWord.Documents.Open(tmpReportFile); wordDocument.ExportAsFixedFormat(ReportFileName, WdExportFormat.wdExportFormatPDF); wordDocument.Close(); appWord.Quit(); } catch (Exception) { // Error if office not installed, then do not save as pdf ReportFileName = ReportFileName.Replace(".pdf", SelectedTemplateFile.Extension); File.Copy(tmpReportFile, ReportFileName, true); } } else if (tmpReportFile.Contains(".pptx")) { try { Microsoft.Office.Interop.PowerPoint.Application appPowerpoint = new Microsoft.Office.Interop.PowerPoint.Application(); Presentation appPres = appPowerpoint.Presentations.Open(tmpReportFile); appPres.ExportAsFixedFormat(ReportFileName, PpFixedFormatType.ppFixedFormatTypePDF); appPres.Close(); appPowerpoint.Quit(); } catch (Exception) { // Error if office not installed, then do not save as pdf ReportFileName = ReportFileName.Replace(".pdf", SelectedTemplateFile.Extension); File.Copy(tmpReportFile, ReportFileName, true); } } /* Reports too ugly and unusable when converted from excel to pdf * else if (tmpReportFile.Contains(".xlsx")) * { * Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application(); * Workbook excelDoc = appExcel.Workbooks.Open(tmpReportFile); * excelDoc.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, ReportFileName); * excelDoc.Close(); * appExcel.Quit(); * } */ else { string report = ReportFileName.Replace(".pdf", SelectedTemplateFile.Extension); File.Copy(tmpReportFile, report, true); } } else { //Copy report file to the selected destination File.Copy(tmpReportFile, ReportFileName, true); } }