Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 964
Mennan Avatar asked Nov 08 '22 20:11

Mennan


1 Answers

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;
         }
  }
like image 185
VVN Avatar answered Nov 14 '22 21:11

VVN