Java converts an excel file into a PDF file

Source: Internet
Author: User

Java converts an excel file into a PDF file

Recently, when I was working on a project, I needed to convert the excel file into a PDF file. After I checked the information, I used only two methods: 1 POI + Itext 2 Jacob to call the excel save function.

The first method is to use POI to read the excel content and write it to a PDF file. The implementation is a little difficult, mainly because the excel sheet structure is not fixed, the content is not fixed, and images may exist. As a result, reading excel is complicated and the real implementation is still complicated.

The second method is to use jacob to call the function of saving an excel file as a pdf file. You can familiarize yourself with jacob's API. No exquisite programming skills.

This article uses the second method. to use this method, you need to install office and pdf software in the current environment. We recommend that you install office 2010. If you install an excelplug-in (saveasypandxps.exe) on the release page, the plug-in is officially available at Microsoft. The link is as follows:

Package com. bplead. module. sign. util;

Import com. jacob. activeX. ActiveXComponent;

Import com.jacb.com. Dispatch;

Import com.jacb.com. Variant;

Public class TransferTool {

Public static void els2pdf (String els, String pdf ){

System. out. println ("Starting excel ...");

Long start = System. currentTimeMillis ();

ActiveXComponent app = new ActiveXComponent ("Excel. Application ");

Try {

App. setProperty ("Visible", false );

Dispatch workbooks = app. getProperty ("Workbooks"). toDispatch ();

System. out. println ("opening document:" + els );

Dispatch workbook = Dispatch. invoke (workbooks, "Open", Dispatch. method, new Object [] {els, new Variant (false), new Variant (false)}, new int [3]). toDispatch ();

Dispatch. invoke (workbook, "SaveAs", Dispatch. Method, new Object [] {

Pdf, new Variant (57), new Variant (false ),

New Variant (57), new Variant (57), new Variant (false ),

New Variant (true), new Variant (57), new Variant (true ),

New Variant (true), new Variant (true)}, new int [1]);

Variant f = new Variant (false );

System. out. println ("to pdf" + pdf );

Dispatch. call (workbook, "Close", f );

Long end = System. currentTimeMillis ();

System. out. println ("completed... used:" + (end-start)/1000 + "s ");

} Catch (Exception e ){

System. out. println ("======== Error: Operation fail:" + e. getMessage ());

} Finally {

If (app! = Null ){

App. invoke ("Quit", new Variant [] {});

}

}

}

Public static void main (String [] args ){

Els2pdf ("f: ProjectTemplate.xlsx", "f: Artificial Intelligence ");

}

}

Run the above environment, need to download jacob's package, the package also contains 2 dll files, one is the jacob-1.17-x64.dll, this is 64-bit, and one is the jacob-1.17-x86.dll file, this is 32-bit. Include the jar package to the classpath directory, and the dll file to the jre/bin directory.

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.