如何将excel工作簿转换为pdf而不使用excel互操作库? [英] How to convert excel workbook to pdf without using excel interop library?

查看:301
本文介绍了如何将excel工作簿转换为pdf而不使用excel互操作库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 应用程序excelApplication = new Application(); 
工作簿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.interop库。因为我不想在服务器端安装ms办公室。



重点是:
我尝试过一些dll将excel转换为pdf,但我couldnt



谢谢。



下载

解决方案

尝试使用以下代码段。

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

在这个 saveAsLocation 是完整的路径的 execelworkBook 。之后,它将使用库 Spire.Xls 转换为pdf。为此添加引用 Spire.Xls ,可以使用管理Nuget软件包添加到您的项目。

  private bool SaveAsPdf(string saveAsLocation)
{
string saveas =(saveAsLocation.Split('。')[0])+.pdf ;
try
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(saveAsLocation);

//以PDF格式保存文档

workbook.SaveToFile(saveas,Spire.Xls.FileFormat.PDF);
返回true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
返回false;
}
}


I have an xlsx file that includes charts each in sheets. I want to save it as pdf file.

I made a sample using 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);

This code works on visual studio but it doesnt work on server without ms office 2007 or higher version installed.

Question is:

How can i convert (FREE) excel to pdf(include charts) without using excel.interop library. Because i dont want to install ms office on server side.

The point is that also: I tried some dll to convert excel to pdf, but i couldnt convert CHARTS only text i could convert successfully.

Thanks.

Download file.

解决方案

Try with this following piece of code.

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

In this the saveAsLocation is the full path of the execelworkBook.After that it is converted into pdf using library Spire.Xls.For that add the reference Spire.Xls,which can be added to your project using 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;
         }
  }

这篇关于如何将excel工作簿转换为pdf而不使用excel互操作库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆