Auto generation of pdf from Excel

Pawan Musale 1 Reputation point
2021-04-08T04:09:04.753+00:00

I have a folder with subfolders in it. Each folder will have some Excel files coming into it whenever user generates Excel reports. I want to convert these Excel reports into pdf files automatically. Any one of the following will work:

  1. Generate pdf automatically whenever a new Excel file is generated or added into a folder OR
  2. Generate pdf files for all Excel files at a set time once in a day.

Request experts to guide how this can be achieved.

Thank you in advance.

Regards,
Pawan

Developer technologies Visual Basic for Applications
{count} votes

2 answers

Sort by: Most helpful
  1. Amin Dodin 1 Reputation point
    2021-04-15T12:17:36.75+00:00

    If you have MS Excel installed on the PC, you can use a PDF printer to achieve this.

    Here are the short steps. More detailed steps are below:

    1. Create a batch file that sends all files in a directory and subfolders inside it to the printer driver, which will then save a separate PDF file for each Excel file.
    2. Set the default printer to be a virtual printer that saves the print jobs it receives as PDF files.
    3. Use the Windows Task Scheduler to run the batch file once every day.

    Here are the detailed steps:

    1. Create a .BAT file (for example, name it prntX2Pdf.bat) and paste the following code inside it. Change “C:” to your actual driver letter and change “C:\test\excel” to the actual folder that contains the XLSX files: C:
      cd C:\test\excel
      for /r %%a in (*.xlsx) do powershell -command "start-process -filepath '%%a' -verb print"
    2. To save the print jobs as PDF, one possible option is LEADTOOLS ePrint, which can be configured to save the output PDF with the same name of the source Excel file. (Disclaimer: I am a LEADTOOLS employee).
    3. Run the Windows Task Scheduler and define a new scheduled task as shown in the following screenshot:

    88108-task.png

    If you’d like to try this and you don’t have LEADTOOLS ePrint, you can download a free evaluation edition from this page.

    0 comments No comments

  2. David 151 Reputation points
    2021-04-26T08:45:26.073+00:00

    For conversion part, you could try Spire.XLS for .NET library. It offers a straightforward method to convert Excel files to PDF. Below is the code snippet for your reference.

    using Spire.Xls;
    
    namespace ToPDF
    {
        class Program
        {
            static void Main(string[] args)
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("Sample.xlsx");
                workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
             }
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.