Mennan
Mennan

Reputation: 4497

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.

Sample excel data

Upvotes: 6

Views: 30918

Answers (3)

Amjad Sahi
Amjad Sahi

Reputation: 1931

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

e.g.

Sample code:

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

Also, see the document for your reference.

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

Upvotes: -1

VVN
VVN

Reputation: 1610

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

Upvotes: 0

Kutty Rajesh Valangai
Kutty Rajesh Valangai

Reputation: 635

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

Upvotes: 0

Related Questions