Excel到PDF C#库

我正在寻找一个MsExcel(.xsl和.xlsx)PDF转换器/库或API。 我想为我的C#.Net应用程序。

我喜欢商业图书馆,但买不起。

这些文章将帮助你!

PDF转换服务

iTextSharp的

Excel到PDF .NET

编辑 :我发现这个类的function。

public DataSet GetExcel(string fileName) { Application oXL; Workbook oWB; Worksheet oSheet; Range oRng; try { // creat a Application object oXL = new ApplicationClass(); // get WorkBook object oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // get WorkSheet object oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1]; System.Data.DataTable dt = new System.Data.DataTable("dtExcel"); DataSet ds = new DataSet(); ds.Tables.Add(dt); DataRow dr; StringBuilder sb = new StringBuilder(); int jValue = oSheet.UsedRange.Cells.Columns.Count; int iValue = oSheet.UsedRange.Cells.Rows.Count; // get data columns for (int j = 1; j <= jValue; j++) { dt.Columns.Add("column" + j, System.Type.GetType("System.String")); } //string colString = sb.ToString().Trim(); //string[] colArray = colString.Split(':'); // get data in cell for (int i = 1; i <= iValue; i++) { dr = ds.Tables["dtExcel"].NewRow(); for (int j = 1; j <= jValue; j++) { oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j]; string strValue = oRng.Text.ToString(); dr["column" + j] = strValue; } ds.Tables["dtExcel"].Rows.Add(dr); } return ds; } catch (Exception ex) { Label1.Text = "Error: "; Label1.Text += ex.Message.ToString(); return null; } finally { Dispose(); } 

编辑2:我也发现这篇文章帮助你!

http://www.c-sharpcorner.com/UploadFile/psingh/PDFFileGenerator12062005235236PM/PDFFileGenerator.aspx

我试图通过第三方包通过Interop直接进行COM交互,但是如果考虑到成本问题,这不是一个选项,我将使用Office 2007/2010的内置导出function来完成此操作。

您需要调用的方法是Workbook.ExportAsFixedFormat()

这里是我如何使用它的一个导出函数的例子:

 public bool ExportWorkbookToPdf(string workbookPath, string outputPath) { // If either required string is null or empty, stop and bail out if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath)) { return false; } // Create COM Objects Microsoft.Office.Interop.Excel.Application excelApplication; Microsoft.Office.Interop.Excel.Workbook excelWorkbook; // Create new instance of Excel excelApplication = new Microsoft.Office.Interop.Excel.Application(); // Make the process invisible to the user excelApplication.ScreenUpdating = false; // Make the process silent excelApplication.DisplayAlerts = false; // Open the workbook that you wish to export to PDF excelWorkbook = excelApplication.Workbooks.Open(workbookPath); // If the workbook failed to open, stop, clean up, and bail out if (excelWorkbook == null) { excelApplication.Quit(); excelApplication = null; excelWorkbook = null; return false; } var exportSuccessful = true; try { // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK) excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath); } catch (System.Exception ex) { // Mark the export as failed for the return value... exportSuccessful = false; // Do something with any exceptions here, if you wish... // MessageBox.Show... } finally { // Close the workbook, quit the Excel, and clean up regardless of the results... excelWorkbook.Close(); excelApplication.Quit(); excelApplication = null; excelWorkbook = null; } // You can use the following method to automatically open the PDF after export if you wish // Make sure that the file actually exists first... if (System.IO.File.Exists(outputPath)) { System.Diagnostics.Process.Start(outputPath); } return exportSuccessful; } 

尝试以下操作:

http://www.sautinsoft.com/convert-excel-xls-to-pdf/spreadsheet-xls-excel-to-pdf-export-component-asp.net.php

要么

http://www.html-to-pdf.net/excel-library.aspx

我想你可以操纵IText来做到这一点: http : //www.itextpdf.com/

也有http://www.aspose.com,但他们并不特别便宜&#x3002;

堆栈溢出的下面的答案可能会有所帮助。 https://stackoverflow.com/questions/891531/convert-xls-doc-files-to-pdf-with-c和https://stackoverflow.com/questions/769246/xls-to-pdf-conversion-inside-&#x51C0; 。 第二个答案有一个有趣的解决办法自动化开放办公室!