如何将excel工作簿转换为pdf而不使用excel互操作库?

我有一个xlsx文件,其中包括每个图表中的图表。 我想保存为PDF文件。

我用excel.interop做了一个示例:

Application excelApplication = new Application(); Workbook wkb = excelApplication.Workbooks.Open(oSlideMaster._FILE_PATH, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlNormalLoad); var basePath = HttpRuntime.AppDomainAppPath; wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, ExportExcelToPdfFullPathWithoutExt); 

此代码适用于Visual Studio,但它不能在没有安装ms office 2007或更高版本的服务器上运行。

问题是:

我怎样才能转换(免费)excel到pdf(包括图表),而无需使用excel.interop库。 因为我不想在服务器端安装ms office。

重点也是:我试过一些DLL转换为PDF格式,但我不能转换CHARTS只有文本我可以成功转换。

谢谢。

下载文件 。

示例excel数据

试试下面这段代码。

  excelworkBook.SaveAs(saveAsLocation); excelworkBook.Close(); excel.Quit(); bool flag=SaveAsPdf(saveAsLocation); 

在这个saveAsLocationsaveAsLocation的完整path。之后,它被转换成pdf使用库Spire.Xls 。为此添加引用Spire.Xls ,可以使用Manage Nuget Packages添加到您的项目。

 private bool SaveAsPdf(string saveAsLocation) { string saveas = (saveAsLocation.Split('.')[0]) + ".pdf"; try { Workbook workbook = new Workbook(); workbook.LoadFromFile(saveAsLocation); //Save the document in PDF format workbook.SaveToFile(saveas, Spire.Xls.FileFormat.PDF); return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } 

http://www.gemboxsoftware.com/Spreadsheet/downloads下载DLL

添加参考和代码

 using System; using System.Drawing; using System.Text; using GemBox.Spreadsheet; class Sample { [STAThread] static void Main(string[] args) { // If using Professional version, put your serial key below. SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY"); ExcelFile ef = ExcelFile.Load("test.xlsx"); ef.Save("Convert.pdf"); } }