C #, VB. NET how to convert Excel to PDF

Source: Internet
Author: User






In our daily work, we often need to convert Excel documents into PDF documents. Are you in distress how to C #, VB. NET programming how to convert an Excel document to a PDF document? Do you look up a lot of data, use a lot of code, but the effect is still not ideal after the conversion? Don't worry, this article will introduce a good way to convert, without the need to use a lot of code, but also to guarantee the effect of the conversion (the converted PDF document and the original Excel document).



I used a spire.xls for when I converted the document. NET components. The component can make the. NET application to quickly build, read, write, and modify Excel documents without installing Microsoft Office. It supports a variety of conversions, such as: Excel to PDF, Excel to HTML, Excel to CSV, Excel to Text, Excel to Image and Excel to XML. Spire.xls for. NET components can also convert charts, shapes, SmartArt graphics, and images in Excel documents to PDFs.



As we all know, there can be multiple worksheets in a workbook, and sometimes we don't need to convert all the worksheets into PDF format, we just need to convert the specific one, and of course, the same component I'm using can do the same. Below I will describe separately how to use C #, VB. NET programming way to convert Excel to PDF and how to convert a specific worksheet in Excel into a PDF.



In order to save everyone's time, I will provide the component at the end of the article, there is a need to go.





    • How to convert an Excel document to a PDF document programmatically in C #


Step 1: Create a new workbook and load the Excel document that you want to convert


Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Person Statistics.xlsx", ExcelVersion.Version2010);


Step 2: save and preview the PDF document


workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
System.Diagnostics.Process.Start("result.pdf");





The following is a comparison between the original Excel document and the target PDF document after the program was run:


Original Excel DocumentDestination PDF Document




C # full code:




using Spire.Xls;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Excel_To_PDF
{    class Program
    {        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\People Counting.xlsx", ExcelVersion.Version2010);
            workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
            System.Diagnostics.Process.Start("result.pdf");


        }
    }
}






    • How to C#,VB. NET programming to convert specific worksheets in Excel to PDF documents


Step 1: Create a new workbook and load the Excel document that you want to convert


Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Final Grade.xlsx");


Step 2: get the first worksheet (I selected the first sheet, you can choose according to your needs)


Worksheet sheet = workbook. Worksheets[0];


Step 3: convert the selected sheet to PDF and save


Sheet. Savetopdf ("Topdf.pdf");


Step 4: preview the PDF document


System.Diagnostics.Process.Start ("Topdf.pdf");



The following is a comparison between the original Excel document and the target PDF document after the program was run:








C # full code:




using Spire.Xls; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Worksheet_To_PDF
{class Program
     {static void Main (string [] args)
         {
             Workbook workbook = new Workbook ();
             workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Final Grade.xlsx");
             Worksheet sheet = workbook.Worksheets [0];
             sheet.SaveToPdf ("toPDF.pdf");
             System.Diagnostics.Process.Start ("toPDF.pdf");

         }
     }
}





Vb. NET full code:




mports Spire.XlsImports System.Collections.GenericImports System.LinqImports System.TextNamespace Worksheet_To_PDF Class Program Private Shared Sub Main (args As String ()) Dim workbook As New Workbook ()
             workbook.LoadFromFile ("C: \ Users \ Administrator \ Desktop \ Final Grade.xlsx") Dim sheet As Worksheet = workbook.Worksheets (0)
             sheet.SaveToPdf ("toPDF.pdf")
             System.Diagnostics.Process.Start ("toPDF.pdf") End Sub
     End ClassEnd Namespace










C #, VB. NET how to convert Excel to PDF


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.