Home » excel » Excel to PDF C# library

Excel to PDF C# library

Posted by: admin March 7, 2020 Leave a comment

Questions:

I am looking for a MsExcel (.xsl and .xlsx) to PDF converter/library or API. I want it for my C# .Net application.

I like commercial libraries, but can’t afford much.

How to&Answers:

These articles will be help for you !

PDF Converter Services

iTextSharp

Excel to PDF .NET

EDIT: I found this class function.

public DataSet GetExcel(string fileName)
    {
        Application oXL;
        Workbook oWB;
        Worksheet oSheet;
        Range oRng;
        try 
        {
            //  creat a Application object
            oXL = new ApplicationClass();
            //   get   WorkBook  object
            oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value);

            //   get   WorkSheet object 
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
            System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            DataRow dr;

            StringBuilder sb = new StringBuilder();
            int jValue = oSheet.UsedRange.Cells.Columns.Count;
            int iValue = oSheet.UsedRange.Cells.Rows.Count;
            //  get data columns
            for (int j = 1; j <= jValue; j++)
            {
                dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
            }

            //string colString = sb.ToString().Trim();
            //string[] colArray = colString.Split(':');

            //  get data in cell
            for (int i = 1; i <= iValue; i++)
            {
                dr = ds.Tables["dtExcel"].NewRow();
                for (int j = 1; j <= jValue; j++)
                {
                    oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
                    string strValue = oRng.Text.ToString();
                    dr["column" + j] = strValue;
                }
                ds.Tables["dtExcel"].Rows.Add(dr);
            }
            return ds;
        }
        catch (Exception ex)
        {
            Label1.Text = "Error: ";
            Label1.Text += ex.Message.ToString();
            return null;
        }
        finally
        {
            Dispose();
        } 

EDIT 2: Also i found this article help for you!

http://www.c-sharpcorner.com/UploadFile/psingh/PDFFileGenerator12062005235236PM/PDFFileGenerator.aspx

Answer:

I’ve tried to move away from direct COM interaction via Interop through third-party packages, but when that’s not an option due to cost considerations, I’ll use Office 2007/2010’s built-in export functionality to accomplish this.

The method you need to call is Workbook.ExportAsFixedFormat()

Here is an example of how I use it an an export function:

public bool ExportWorkbookToPdf(string workbookPath, string outputPath)
{
    // If either required string is null or empty, stop and bail out
    if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
    {
        return false;
    }

    // Create COM Objects
    Microsoft.Office.Interop.Excel.Application excelApplication;
    Microsoft.Office.Interop.Excel.Workbook excelWorkbook;

    // Create new instance of Excel
    excelApplication = new Microsoft.Office.Interop.Excel.Application();

    // Make the process invisible to the user
    excelApplication.ScreenUpdating = false;

    // Make the process silent
    excelApplication.DisplayAlerts = false;

    // Open the workbook that you wish to export to PDF
    excelWorkbook = excelApplication.Workbooks.Open(workbookPath);

    // If the workbook failed to open, stop, clean up, and bail out
    if (excelWorkbook == null)
    {
        excelApplication.Quit();

        excelApplication = null;
        excelWorkbook = null;

        return false;
    }

    var exportSuccessful = true;
    try
    {
        // Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
        excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
    }
    catch (System.Exception ex)
    {
        // Mark the export as failed for the return value...
        exportSuccessful = false;

        // Do something with any exceptions here, if you wish...
        // MessageBox.Show...        
    }
    finally
    {
        // Close the workbook, quit the Excel, and clean up regardless of the results...
        excelWorkbook.Close();
        excelApplication.Quit();

        excelApplication = null;
        excelWorkbook = null;
    }

    // You can use the following method to automatically open the PDF after export if you wish
    // Make sure that the file actually exists first...
    if (System.IO.File.Exists(outputPath))
    {
        System.Diagnostics.Process.Start(outputPath);
    }

    return exportSuccessful;
}

Answer:

Try the following:

http://www.sautinsoft.com/convert-excel-xls-to-pdf/spreadsheet-xls-excel-to-pdf-export-component-asp.net.php

or

http://www.html-to-pdf.net/excel-library.aspx

I think you can manipulate IText to do this as well: http://www.itextpdf.com/

There is also http://www.aspose.com but they are not especially cheap.

The following answer on stack overflow may help. https://stackoverflow.com/questions/891531/convert-xls-doc-files-to-pdf-with-c and https://stackoverflow.com/questions/769246/xls-to-pdf-conversion-inside-net
. The second answer has an interesting solution automating open office!!