简体   繁体   中英

How to convert excel workbook to pdf without using excel interop library?

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 .

示例 Excel 数据

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;
         }
  }

download dll from http://www.gemboxsoftware.com/Spreadsheet/downloads

add reference and code

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");
    }
}

You may consider Aspose.Cells for the task. See the sample .NET code for your reference:

eg

Sample code:

// Open an Excel file 
Workbook workbook = new Workbook("Book1.xlsx"); 
// Save the document in PDF format 
workbook.Save("output.pdf", SaveFormat.Pdf);

PS. I am working as Support developer/ Evangelist at Aspose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM