/// <summary> /// /// </summary> /// <param name="saveUrl"></param> /// <param name="wpsFilename"></param> /// <returns></returns> private string XlsWpsToPdf(string fileSource, string wpsFilename) { if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); } string path = Path.GetDirectoryName(fileSource); var pdfSavePath = Path.Combine(path, string.Format("{0}_{1}.pdf", wpsFilename, Guid.NewGuid().ToString())); try { XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; //xls 转pdf dynamic doc = wps.Application.Workbooks.Open(fileSource, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); doc.ExportAsFixedFormat(targetType, pdfSavePath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); //设置隐藏菜单栏和工具栏 //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar); doc.Close(); doc = null; } catch (Exception ex) { //targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath); throw; } finally { Dispose(); } return(pdfSavePath); }
void ExportBookAs(Workbook book, XlFixedFormatType format, string extension) { var filename = GetTargetName(book, extension); WriteVerbose($"Exporting a workbook: {filename}"); book.ExportAsFixedFormat(format, filename); }
protected override bool doConvertToPdf(string saveAsFile) { XlFixedFormatType type = XlFixedFormatType.xlTypePDF; this.wb.ExportAsFixedFormat(type, saveAsFile, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); return(true); }
public int Convert(string sourcePath, string targetPath) { if (!File.Exists(sourcePath)) { return(ErrorMessages.FileNotExist); } XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; ApplicationClass application = new ApplicationClass();; Workbook workBook = null; try { workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //设置格式,导出成PDF Worksheet sheet = (Worksheet)workBook.Worksheets[1]; //下载从1开始 //把sheet设置成横向 sheet.PageSetup.Orientation = XlPageOrientation.xlLandscape; //可以设置sheet页的其他相关设置,不列举 sheet.ExportAsFixedFormat(targetType, targetPath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); return(ErrorMessages.ConvertSuccess); } catch (COMException ex) { _logger.Error(ex.StackTrace); _logger.Error(ex.Message); return(ErrorMessages.OfficeToPdfUninstall); } catch (Exception ex) { _logger.Error(ex.Message); return(ErrorMessages.ConvertFailed); } finally { if (workBook != null) { workBook.Close(true, missing, missing); } if (application != null) { application.Quit(); } GC.Collect(); GC.WaitForPendingFinalizers(); } }
private void ExportWorkbookToPDF(string workbook, string output) { XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; ApplicationClass excelApplication = null; Workbook excelWorkbook = null; if (string.IsNullOrEmpty(workbook) || string.IsNullOrEmpty(output)) { throw new NullReferenceException("Cannot create PDF copy " + "from empty workbook."); } try { excelApplication = new ApplicationClass(); excelWorkbook = excelApplication.Workbooks.Open(@workbook); //excelApplication.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape; // Zoom property must be false, otherwise the "FitToPages" properties // are ignored. //excelWorkbook.PageSetup.Zoom = false; // these set the number of pages tall or wide the worksheet will be // scaled to when printed. //excelWorkbook.PageSetup.FitToPagesTall = 1; //excelWorkbook.PageSetup.FitToPagesWide = 1; //excelWorkbook.ExportAsFixedFormat(targetType, Path.GetDirectoryName(@"d:\temp\test.pdf")); excelWorkbook.ExportAsFixedFormat(targetType, @output); //richTextBox1.Text += DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + ":已匯出PDF檔:" + output + "。\r\n"; myUI(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + ":已匯出PDF檔:" + output + "。\r\n", richTextBox1); //MessageBox.Show("Diretory not exist!"); } catch (Exception e) { //Console.WriteLine(e.Message); //Console.ReadLine(); excelWorkbook.Close(false, Type.Missing, Type.Missing); //richTextBox1.Text += DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + ": " + e.Message + e.ToString() + "\r\n"; myUI(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + ": " + e.Message + e.ToString() + "\r\n", richTextBox1); //throw; } finally { //excelWorkbook.Close(true); //以下內容全都不能少!! excelWorkbook.Close(false, Type.Missing, Type.Missing); excelApplication.Workbooks.Close(); excelApplication.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication); GC.Collect(); } }
private bool convertPDF(string excelFile) { string pdfFile = excelFile.Replace(".xlsx", ".pdf"); // PDFファイルフルパス XlFixedFormatType format = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality quality = XlFixedFormatQuality.xlQualityStandard; Microsoft.Office.Interop.Excel.Application app = null; Workbook workbook = null; try { app = new Microsoft.Office.Interop.Excel.Application(); workbook = app.Workbooks.Open(excelFile); //---ブックを開いて workbook.ExportAsFixedFormat(format, pdfFile, quality); //--- PDF形式で出力 return(true); } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } finally { if (workbook != null) { // Wakamatsu 20170313 workbook.Close(false); // Wakamatsu 20170313 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); workbook = null; } app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; if (excelFile != "" && delFlag == true) { if (File.Exists(excelFile)) { try { System.Threading.Thread.Sleep(1000); // 対象Excelファイル削除 File.Delete(excelFile); } catch (IOException) { } } } } }
/// <summary> /// Excel转PDF /// </summary> /// <param name="excelPath"></param> /// <param name="pdfPath"></param> public static bool ExcelConvertToPDF(string excelPath, string pdfPath, int FromIndex, int ToIndex) { excelPath = CommonUtils.GetPhysicsPath(excelPath); pdfPath = CommonUtils.GetPhysicsPath(pdfPath); bool result = false; XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; Application application = null; Microsoft.Office.Interop.Excel.Workbook workBook = null; try { application = new Application(); object type = targetType; workBook = application.Workbooks.Open(excelPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); object From = FromIndex == -1 ? missing : FromIndex + 1; object To = ToIndex == -1 ? missing : ToIndex + 1; workBook.ExportAsFixedFormat(targetType, pdfPath, XlFixedFormatQuality.xlQualityStandard, false, false, From, To, missing, missing); result = true; } catch (Exception ex) { result = false; throw ex; } finally { if (workBook != null) { workBook.Close(false, missing, missing); workBook = null; } if (workBook != null) { workBook.Close(false, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } } return(result); }
private void convertExcel(string fileName) { string str = Path.GetFileNameWithoutExtension(fileName) + ".pdf"; Application application = (Application)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046"))); Workbook workbook = null; object missing = Type.Missing; string filename = this.workDir + str; XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality xlQualityMinimum = XlFixedFormatQuality.xlQualityMinimum; bool openAfterPublish = false; bool includeDocProperties = true; bool ignorePrintAreas = true; object from = Type.Missing; object to = Type.Missing; try { workbook = application.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); if (workbook != null) { workbook.ExportAsFixedFormat(xlTypePDF, filename, xlQualityMinimum, includeDocProperties, ignorePrintAreas, from, to, openAfterPublish, missing); } } catch (Exception exception) { MessageBox.Show("Excel conversion failed for file " + fileName + ". \n Error caught: " + exception.Message); } finally { if (workbook != null) { workbook.Close(false, missing, missing); workbook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
//将excel文档转换成PDF格式 private bool FConvertEXL(string sourcePath, string targetPath //, XlFixedFormatType targetType ) { XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; bool result; object missing = Type.Missing; Excel.ApplicationClass application = null; Workbook workBook = null; try { application = new Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } System.GC.Collect(); System.GC.WaitForPendingFinalizers(); System.GC.Collect(); System.GC.WaitForPendingFinalizers(); } return(result); }
/// <summary> /// Deze methode start een nieuwe instantie van een Office Excel applicatie. /// De print service naar /// van het Office pakket wordt gebruikt om wordt documenten om te zetten naar een PDF-bestand. /// </summary> /// <param name="vehicle_id">voor welke auto?</param> /// <param name="pdfNamen">Voor welke documenten?</param> public void ConverteerExcelNaarPDF(string vehicle_id, List <string> pdfNamen) { if (excelFiles.Count() == 0) { HaalBestandsNamenOp(vehicle_id, pdfNamen); } if (pdfNamen.Count != 0 && officeAanwezig) { paramExportFormat = XlFixedFormatType.xlTypePDF; paramExportQuality = XlFixedFormatQuality.xlQualityStandard; foreach (FileInfo excelFile in excelFiles) { ConverteerBestand(excelFile); } } }
//将excel文档转换成PDF格式 public static string Convert(string sourcePath, XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF) { bool result; object missing = Type.Missing; Excel.Application application = null; Workbook workBook = null; object target = sourcePath.Replace("xls", "pdf"); try { application = new Excel.Application(); object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(target.ToString()); }
/// <summary> /// 把Excel文件转换成PDF格式文件 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目标文件路径</param> /// <returns>true:转换成功;false:转换失败</returns> public static bool XLSConvertToPDF(string sourcePath, string targetPath) { bool result = false; XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; Microsoft.Office.Interop.Excel.ApplicationClass application = null; Workbook workBook = null; try { application = new Microsoft.Office.Interop.Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(result); }
private bool ConvertToPDF(string sourcePath, string targetPath, XlFixedFormatType targetType) { //bool result; //object missing = Type.Missing; //Microsoft.Office.Interop.Excel.ApplicationClass application = null; //Microsoft.Graph.Workbook workBook = null; //try //{ // application = new Microsoft.Office.Interop.Excel.ApplicationClass(); // object target = targetPath; // object type = targetType; // workBook = (Microsoft.Graph.Workbook)application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, // missing, missing, missing, missing, missing, missing, missing, missing, missing); // workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); // result = true; // return result; //} //catch (Exception ex) //{ // throw ex; //} //finally //{ // if (workBook != null) // { // workBook.Close(true, missing, missing); // workBook = null; // } // if (application != null) // { // application.Quit(); // application = null; // } // GC.Collect(); // GC.WaitForPendingFinalizers(); // GC.Collect(); // GC.WaitForPendingFinalizers(); //} return(true); }
/// <summary> /// Excel格式转换(Excel文件不能为空,默认转成PDF) /// </summary> /// <param name="sourcePath">源路径</param> /// <param name="targetPath">目标路径</param> /// <param name="targetFileType">转换类型</param> /// <returns></returns> public static bool ExcelConvert(string sourcePath, string targetPath, XlFixedFormatType targetFileType = XlFixedFormatType.xlTypePDF) { Microsoft.Office.Interop.Excel.ApplicationClass excelApplication = null; Workbook excelWorkBook = null; try { excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass(); excelWorkBook = excelApplication.Workbooks.Open(sourcePath); excelWorkBook.ExportAsFixedFormat(targetFileType, targetPath, XlFixedFormatQuality.xlQualityStandard); } catch (Exception e) { Console.WriteLine(e.Message); throw; } finally { if (excelWorkBook != null) { excelWorkBook.Close(); } if (excelApplication != null) { int k; var t = new IntPtr(excelApplication.Hwnd); GetWindowThreadProcessId(t, out k); var p = System.Diagnostics.Process.GetProcessById(k); excelApplication.Quit(); p.Kill(); } GC.Collect(); GC.WaitForPendingFinalizers(); } return(true); }
public static Boolean CreateReportPDFFromExcel(string excelFilePath, String pdfFilePath, Int32 reportIndex) { Boolean flag = false; if (!System.IO.File.Exists(excelFilePath)) { return(flag); } ApplicationClass excelApplication = new ApplicationClass(); Workbook excelWorkBook = null; string paramSourceBookPath = excelFilePath; object paramMissing = Type.Missing; XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityMinimum; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = true; object paramFromPage = 1; object paramToPage = 1; try { // Open the source workbook. excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); // Save it in the target format. if (excelWorkBook != null) { if (!Directory.Exists(Path.GetDirectoryName(pdfFilePath))) { Directory.CreateDirectory(Path.GetDirectoryName(pdfFilePath)); } Worksheet reportSheet = excelWorkBook.Sheets[reportIndex] as Worksheet; if (reportSheet != null) { if (File.Exists(pdfFilePath)) { File.Delete(pdfFilePath); } reportSheet.PageSetup.Zoom = 90; reportSheet.PageSetup.LeftMargin = excelApplication.CentimetersToPoints(3); reportSheet.PageSetup.RightMargin = excelApplication.CentimetersToPoints(2); reportSheet.PageSetup.TopMargin = excelApplication.CentimetersToPoints(2.5); reportSheet.PageSetup.BottomMargin = excelApplication.CentimetersToPoints(2.5); reportSheet.ExportAsFixedFormat(paramExportFormat, pdfFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); } } flag = true; } catch (Exception e) { throw e; } finally { // Close the workbook object. if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } // Close the ApplicationClass object. if (excelApplication != null) { excelApplication.Quit(); excelApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(flag); }
public static string ConvertData(string files) { FileInfo newFile = new FileInfo(files); //if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + @"\Report\")) //Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + @"\Report\"); ApplicationClass excelApplication = new ApplicationClass(); Workbook excelWorkBook = null; excelApplication.Caption = System.Guid.NewGuid().ToString().ToUpper(); string paramSourceBookPath = files;//System.Windows.Forms.Application.StartupPath + @"\" + files;// @"D:\test.xls"; object paramMissing = Type.Missing; string name = Path.GetFileNameWithoutExtension(files); //string paramExportFilePath = System.Windows.Forms.Application.StartupPath + @"\Report\" + name + ".pdf"; string paramExportFilePath = newFile.DirectoryName + @"\\" + name + ".pdf"; //MessageBox.Show(paramExportFilePath); XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = true; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; try { // Open the source workbook. excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); // Save it in the target format. if (excelWorkBook != null) { excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); // MessageBox.Show(paramExportFilePath); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); // Respond to the error. } finally { // Close the workbook object. if (excelWorkBook != null) { excelApplication.DisplayAlerts = false; excelWorkBook.Close(false, paramMissing, paramMissing); Marshal.ReleaseComObject(excelWorkBook); Marshal.FinalReleaseComObject(excelWorkBook); excelWorkBook = null; } // Quit Excel and release the ApplicationClass object. if (excelApplication != null) { excelApplication.Quit(); Config.EnsureProcessKilled(IntPtr.Zero, excelApplication.Caption); Marshal.ReleaseComObject(excelApplication); Marshal.FinalReleaseComObject(excelApplication); excelApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(paramExportFilePath); }
public static void ConvertData(string files) { ApplicationClass excelApplication = new ApplicationClass(); Workbook excelWorkBook = null; string paramSourceBookPath = System.Windows.Forms.Application.StartupPath + @"\" + files;// @"D:\test.xls"; object paramMissing = Type.Missing; string name = Path.GetFileNameWithoutExtension(files); string paramExportFilePath = System.Windows.Forms.Application.StartupPath + @"\Report\" + name + ".pdf"; //MessageBox.Show(paramExportFilePath); XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = true; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; try { // Open the source workbook. excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); // Save it in the target format. if (excelWorkBook != null) { excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); // MessageBox.Show(paramExportFilePath); } } catch //(Exception ex) { // MessageBox.Show(ex.Message.ToString()); // Respond to the error. } finally { // Close the workbook object. if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } // Quit Excel and release the ApplicationClass object. if (excelApplication != null) { excelApplication.Quit(); excelApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
public void ExportAsFixedFormat(XlFixedFormatType type, object filename, object quality, object includeDocProperties, object ignorePrintAreas, object from, object to, object openAfterPublish, object fixedFormatExtClassPtr) { throw new NotImplementedException(); }
/// <summary>导出为指定格式的文件。 /// </summary> /// <param name="Type">要导出为的文件格式类型。</param> /// <param name="Filename">要保存的文件的文件名。可以包括完整路径,否则 Excel 2007 会将文件保存在当前文件夹中。</param> /// <param name="Quality">可选 XlFixedFormatQuality。指定已发布文件的质量。</param> /// <param name="IncludeDocProperties">若要包括文档属性,则为 True;否则为 False。</param> /// <param name="IgnorePrintAreas">若要忽略发布时设置的任何打印区域,则为 True;否则为 False。</param> /// <param name="From">发布的起始页码。如果省略此参数,则从起始位置开始发布。</param> /// <param name="To">发布的终止页码。如果省略此参数,则发布至最后一页。</param> /// <param name="OpenAfterPublish">若要在发布文件后在查看器中显示文件,则为 True;否则为 False。</param> /// <param name="FixedFormatExtClassPtr">Pointer to the FixedFormatExt class</param> public void ExportAsFixedFormat(XlFixedFormatType Type, string Filename = null, XlFixedFormatQuality? Quality = null, bool? IncludeDocProperties = null, bool? IgnorePrintAreas = null, int? From = null, int? To = null, bool? OpenAfterPublish = null, object FixedFormatExtClassPtr = null) { _objaParameters = new object[9] { Type, Filename == null ? System.Type.Missing : Filename, Quality == null ? System.Type.Missing : Quality, IncludeDocProperties == null ? System.Type.Missing : IncludeDocProperties, IgnorePrintAreas == null ? System.Type.Missing : IgnorePrintAreas, From == null ? System.Type.Missing : From, To == null ? System.Type.Missing : To, OpenAfterPublish == null ? System.Type.Missing : OpenAfterPublish, FixedFormatExtClassPtr == null ? System.Type.Missing : FixedFormatExtClassPtr }; _objRange.GetType().InvokeMember("ExportAsFixedFormat", BindingFlags.InvokeMethod, null, _objRange, _objaParameters); }
private void convertirexcel() { Microsoft.Office.Interop.Excel.Application word = new Microsoft.Office.Interop.Excel.Application(); Workbook excelWorkBook = null; string paramSourceBookPath = @"C:\Users\raul\Documents\Visual Studio 2012\Projects\WorkFlow Seguros Futuro\WorkFlow Seguros Futuro\files\" + FileUpload.FileName; object paramMissing = Type.Missing; string paramExportFilePath = Server.MapPath("~/files/" + Path.GetFileNameWithoutExtension(FileUpload.FileName) + ".pdf"); XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = true; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; try { excelWorkBook = word.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); if (excelWorkBook != null) { excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); } } catch (Exception ex) { // agregar mensaje de error } finally { if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } if (word != null) { word.Quit(); word = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
public void ExportAsFixedFormat(XlFixedFormatType type, object filename, object quality, object includeDocProperties, object ignorePrintAreas, object from, object to, object openAfterPublish, object fixedFormatExtClassPtr) { throw new NotImplementedException(); }
/// <summary> /// Deze methode start een nieuwe instantie van een Office Excel applicatie. /// De print service naar /// van het Office pakket wordt gebruikt om wordt documenten om te zetten naar een PDF-bestand. /// </summary> /// <param name="vehicle_id">voor welke auto?</param> /// <param name="pdfNamen">Voor welke documenten?</param> public void ConverteerExcelNaarPDF(string vehicle_id, List<string> pdfNamen) { if (excelFiles.Count() == 0) HaalBestandsNamenOp(vehicle_id, pdfNamen); if (pdfNamen.Count != 0 && officeAanwezig) { paramExportFormat = XlFixedFormatType.xlTypePDF; paramExportQuality = XlFixedFormatQuality.xlQualityStandard; foreach (FileInfo excelFile in excelFiles) { ConverteerBestand(excelFile); } } }
static void ExcelToPDF(string sourcePath, string ExportPath) { Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application(); Workbook excelWorkBook = null; string paramSourceBookPath = sourcePath; //@"C:\Users\Lucky s\Desktop\New folder (2)\StocksOnTheMove.xls"; object paramMissing = Type.Missing; string paramExportFilePath = ExportPath; // @"C:\Users\Lucky s\Desktop\New folder (2)\Test.pdf"; XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; bool paramOpenAfterPublish = true; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = false; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; object oMissing = System.Reflection.Missing.Value; // object Format=XlFixedFormatType XlSaveAsAccessMode Mode = XlSaveAsAccessMode.xlNoChange; //excelWorkBook.Application.DisplayAlerts = false; try { // Open the source workbook. excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); excelWorkBook.Activate(); // Save it in the target format. if (excelWorkBook != null) { //excelWorkBook.SaveAs(paramExportFilePath); // excelWorkBook.SaveAs(paramExportFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing); excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); } } catch (Exception ex) { // Respond to the error. } finally { // Close the workbook object. if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } // Quit Excel and release the ApplicationClass object. if (excelApplication != null) { excelApplication.Quit(); excelApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
void ExcelToPDF(string sourcePath, string ExportPath) { Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application(); Workbook excelWorkBook = null; string paramSourceBookPath = sourcePath; object paramMissing = Type.Missing; string paramExportFilePath = ExportPath;// @"C:\Users\Lucky s\Desktop\New folder (2)\Test.pdf"; XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = false; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; object oMissing = System.Reflection.Missing.Value; // XlSaveAsAccessMode Mode = XlSaveAsAccessMode.xl; // excelWorkBook.Application.DisplayAlerts = false; try { // Open the source workbook. excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); excelWorkBook.Activate(); //Formating Worksheet sheet = (Worksheet)excelWorkBook.Worksheets[1]; sheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait; // Zoom property must be false, otherwise the "FitToPages" properties // are ignored. sheet.PageSetup.Zoom = false; // these set the number of pages tall or wide the worksheet will be // scaled to when printed. sheet.PageSetup.FitToPagesTall = 1; sheet.PageSetup.FitToPagesWide = 1; // sheet.DisplayPageBreaks = true; sheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; sheet.UsedRange.Font.Size = 14; //sheet.UsedRange.Borders.Weight = 2; //sheet.UsedRange.Cells.Borders.Weight = 2; sheet.UsedRange.Font.Name = "Arial Narrow"; sheet.PageSetup.LeftMargin = 0.5; sheet.PageSetup.RightMargin = 0.5; sheet.PageSetup.TopMargin = 1; sheet.PageSetup.BottomMargin = 0.5; //End // Save it in the target format. if (excelWorkBook != null) { excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); } } catch (Exception ex) { // Respond to the error. } finally { // Close the workbook object. if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } // Quit Excel and release the ApplicationClass object. if (excelApplication != null) { excelApplication.Quit(); excelApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
public static bool FileToPDF(FileInterface file, string outputPath) { try { if (ArquivoAberto(file.file.FullName)) { DialogResult closed; do { closed = MessageBox.Show(String.Format("O arquivo {0} não pode ser convertido pois está sendo " + "usado por outro processo neste momento. Por favor, feche o arquivo " + "e clique em 'Sim' se deseja continuar. Para cancelar a conversão deste arquivo, clique em 'Não'.", file.file.Name), "Erro - Arquivo aberto", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (!ArquivoAberto(file.file.FullName)) { break; } }while (closed.Equals(DialogResult.Yes)); if (closed.Equals(DialogResult.No)) { Arquivo arquivo = new Arquivo(file.file.Name, file.file.Extension, "Cancelado", "Conversão cancelada pelo usuário"); Arquivos.Add(arquivo); return(false); } } if (file.file.Extension.Contains("doc")) { string wordDoc = file.file.FullName; string output = RetornarOutputCorreto(outputPath, file.file.Name.Replace(file.file.Extension, "")); Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application(); wordDocument = appWord.Documents.Open(wordDoc); wordDocument.ExportAsFixedFormat(output, WdExportFormat.wdExportFormatPDF); object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument; object routeDocument = false; appWord.Quit(ref saveOption, ref originalFormat, ref routeDocument); Arquivo arquivo = new Arquivo(file.file.Name, file.file.Extension, "Convertido", "Arquivo convertido com sucesso!"); Arquivos.Add(arquivo); return(true); } else { Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook excelWorkBook = null; string paramSourceBookPath = file.file.FullName; object paramMissing = Type.Missing; //string paramExportFilePath = outputPath + String.Format(@"\{0}.pdf", file.file.Name.Replace(file.file.Extension, "")); string paramExportFilePath = RetornarOutputCorreto(outputPath, file.file.Name.Replace(file.file.Extension, "")); XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = true; object paramFromPage = Type.Missing; object paramToPage = Type.Missing; excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); if (excelWorkBook != null) { excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); } if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } if (excelApplication != null) { excelApplication.Quit(); excelApplication = null; } Arquivo arquivo = new Arquivo(file.file.Name, file.file.Extension, "Convertido", "Arquivo convertido com sucesso!"); Arquivos.Add(arquivo); return(true); } } catch (Exception erro) { Arquivo arquivo = new Arquivo(file.file.Name, file.file.Extension, "Erro", erro.Message); Arquivos.Add(arquivo); return(false); } }