This is the 10th day of my participation in the First Challenge 2022, for more details: First Challenge 2022
Hello, hello, I am gray little ape, a super bug writing program ape!
There are many ways to use Excel to PDF, here I will introduce you to two common methods, respectively to deal with two different use scenarios, next I in the Springboot environment to give you a demonstration!
1. Convert PDF using Spire
Let’s start with an easy way to convert a sheet or an entire Excel file to PDF format with just a few lines of code.
However, one disadvantage of this method is that the JAR package is charged and can only export 3 sheet pages, which is not suitable for converting Excel files with multiple sheet pages.
Import workBook’s required dependencies into the POM file first:
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>2.2.0</version>
</dependency>
Copy the code
1. Use Spire to convert the entire Excel file to PDF
Using Workbook to export the code for the entire Excel file takes only two steps, so we can look directly at the code example:
@param inputFilePath Excel file path @param outputFilePath Export PDF file path */ public static void totalExcelToPDF(String inputFilePath, String outputFilePath) { Workbook wb = new Workbook(); Wb. loadFromFile(inputFilePath); Wb. saveToFile(outputFilePath, fileformat.pdf); }Copy the code
2, specify a single sheet page to PDF
Using Spire, you can convert a specified sheet page to PDF output by entering the subscript of the specified sheet page. A code example is as follows:
/** * Convert single sheet to PDF using spire ** @param inputFilePath Excel file path * @param outputFilePath Export PDF file path * @param sheetNum Public static void partExcelToPDF(String inputFilePath, String outputFilePath, Int sheetNum) {Workbook wb = new Workbook(); wb.loadFromFile(inputFilePath); Worksheet = wb.getWorksheets().get(sheetNum); SaveToPdf (outputFilePath); }Copy the code
Although the above method of using Spire to convert Excel to PDF is simple, it is not suitable for converting large Excel files, only suitable for files with less than 3 sheets.
Therefore, in the enterprise project development, if you want to convert a large number of Excel files or sheet pages of Excel files into PDF format as a whole, or recommend using the following method, and this method is stable and free!
2. Convert Excel to PDF using Jacob (recommended)
1. Environment preparation
Need to do some preparation before use of Jacob, Jacob first need to download package Jacob. Zip, download address: sourceforge.net/projects/ja…
After unpacking, you get the following:
If you’re on a 64-bit system, use x64 DLLS, and 32-bit systems use x86 DLLS. Then we need to put the DLL file into the bin directory of your JDK, as shown below:
So the preparatory work is done,
Next, we introduce Jacob’s dependencies in Maven:
<! --> <dependency> <groupId>com. Jacob </groupId> <artifactId> Jacob </artifactId> <version>1.19</version> <scope>system</scope> <! D:\DevInstall\ Jacob18, 19\ Jacob-1.19 \jacob.jar</systemPath> </dependency>Copy the code
Note: the value of the tag in the dependency above is the exact path of your jacob.jar. Change this to the path you just downloaded.
2. Export PDF files
The execution of Excel export PDF method is as follows: one of the more important places are annotated, the method can be directly used!
** @param inputFilePath Path to import Excel files * @param outputFilePath Path to export PDF files */ public static void jacobExcelToPDF(String inputFilePath, String outputFilePath) { ActiveXComponent ax = null; Dispatch excel = null; try { ComThread.InitSTA(); ax = new ActiveXComponent("Excel.Application"); ax.setProperty("Visible", new Variant(false)); // Disable the macro ax.setProperty("AutomationSecurity", new Variant(3)); Dispatch excels = ax.getProperty("Workbooks").toDispatch(); Object[] obj = { inputFilePath, new Variant(false), new Variant(false) }; excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); Object[] obj2 = {// PDF format is equal to 0 new Variant(0), outputFilePath, // 0= standard (generated PDF images do not blur), 1= minimum file new Variant(0)}; Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]); } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (excel ! = null) { Dispatch.call(excel, "Close", new Variant(false)); } if (ax ! = null) { ax.invoke("Quit", new Variant[]{}); ax = null; } ComThread.Release(); }}Copy the code
These are the two most common ways to export Excel to PDF in Java, and the second is recommended!
I’m Grey Ape, and I’ll see you next time!