Skip to main content
 首页 » 编程设计

c#之如何在 Sharepoint Web 部件中将 excel 文件(xlsx 文件)转换为 pdf

2024年01月02日100thcjp

我正在开发 Sharepoint Web 部件。我已经自定义了 Sharepoint 服务器功能区。 Sharepoint 服务器功能区现在包含一个按钮。单击此按钮后,我将 sharepoint 库中的 xlsx 文件下载到我的本地驱动器之一。现在我想将这个下载的 xlsx 文件转换成 pdf 文件。然后我想将此pdf文件上传到sharepoint中的一个文档库。为了自定义服务器功能区和处理按钮回发,我使用以下链接,其中提供了代码示例供下载

Customizing and Extending the SharePoint 2010 Server Ribbon

代码示例对我来说工作正常。我已将 xlsx 文件下载到本地文件夹。现在我想将本地文件夹中下载的 xlsx 文件转换为 pdf 文件所以我使用以下链接

Convert xlsx file to pdf file

上面链接中给出的代码示例也是我在控制台应用程序中的工作文件。我能够在控制台应用程序中将 xlsx 文件转换为 pdf 文件。但是当我将代码复制并粘贴到共享点项目中时,它不起作用。我无法理解为什么相同的代码在 Sharepoint 项目中不起作用?代码如下

        SautinSoft.UseOffice u = new SautinSoft.UseOffice(); 
 
        //Path to any local file 
        string inputFilePath =  "D:\\a.xlsx"; 
        //string inputFilePath = "http://shailesh-pc/TemplateInvoice/Template.xlsx"; 
        //Path to output resulted file 
        string outputFilePath = "D:\\afadfasfd.pdf"; 
        //string outputFilePath = "http://shailesh-pc/Invoice/aa.xlsx"; 
 
 
        //Prepare UseOffice .Net, loads MS Excel in memory 
        int ret = u.InitExcel(); 
 
        //Return values: 
        //0 - Loading successfully 
        //1 - Can't load MS Excel library in memory  
 
        if (ret == 1) 
            return; 
 
        //Converting 
        ret = u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.XLSX_to_PDF); 
 
 
 
 
        //Release MS Excel from memory 
        u.CloseExcel(); 
 
        //0 - Converting successfully 
        //1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported 
        //2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application 
        //3 - Converting failed, please contact with our Support Team 
        //4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010 
        if (ret == 0) 
        { 
            //Show produced file 
            System.Diagnostics.Process.Start(outputFilePath); 
        }        

在共享点项目中,我在 int ret = u.InitExcel(); 处得到值 0 但在行 ret = u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.XLSX_to_PDF); 在共享点项目中。为什么会这样。 sharepoint 项目是否存在任何问题,因此他们无法将 xlsx 文件转换为 pdf?你能告诉我在 Sharepoint 中将 xlsx 文件转换为 pdf 需要做什么吗?能否请您提供任何代码或链接,通过它我可以将 xlsx 文件转换为 pdf 文件?

请您参考如下方法:

请注意,Microsoft 不建议在此类情况下使用 Office 自动化:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

您可以在以下主题中查看更多详细信息:http://support.microsoft.com/kb/257757 .因此,最好考虑替代方案。