excel转pdf的java实现

一、实现原理

采用java调用vbs脚本调用office应用把excel转成pdf。

支持文件格式:xlsx,xls,csv

二、前期准备

1、安装office软件

2、准备vbs脚本文件,放到**C:\excel2pdf_script\**目录下。(本文只用2个文件)

三、VBS转换脚本

1、excel_start.vbs

vbscript 复制代码
' First, try to get a running instance.
On Error Resume Next
Dim excelApplication
Set excelApplication = GetObject(, "Excel.Application")

' If MS Excel is already running, the script is successful.
If Err = 0 Then
  WScript.Quit 3
End If
Err.clear

' Start MS Excel.
Set excelApplication = CreateObject("Excel.Application")
excelApplication.DisplayAlerts = False
' Add a workbook to keep open, otherwise Excel is shut down implicitly.
excelApplication.Workbooks.Add
If Err <> 0 Then
  WScript.Quit -6
End If

' Disable execution of macros.
excelApplication.ExcelBasic.DisableAutoMacros

' Exit and signal success.
WScript.Quit 3

2、excel_convert.vbs

vbscript 复制代码
' See http://msdn.microsoft.com/en-us/library/bb243311%28v=office.12%29.aspx
Const WdExportFormatPDF = 17
Const MagicFormatPDF = 999

Dim arguments
Set arguments = WScript.Arguments

' Transforms a file using MS Excel into the given format.
Function ConvertFile( inputFile, outputFile, formatEnumeration )

  Dim fileSystemObject
  Dim excelApplication
  Dim excelDocument

  ' Get the running instance of MS Excel. If Excel is not running, exit the conversion.
  On Error Resume Next
  Set excelApplication = GetObject(, "Excel.Application")
  If Err <> 0 Then
    WScript.Quit -6
  End If
  On Error GoTo 0

  ' Find the source file on the file system.
  Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
  inputFile = fileSystemObject.GetAbsolutePathName(inputFile)

  ' Convert the source file only if it exists.
  If fileSystemObject.FileExists(inputFile) Then

    ' Attempt to open the source document.
    On Error Resume Next
    Set excelDocument = excelApplication.Workbooks.Open(inputFile, , True)
    If Err <> 0 Then
      WScript.Quit -2
    End If
    On Error GoTo 0

    ' Convert: See http://msdn2.microsoft.com/en-us/library/bb221597.aspx
    On Error Resume Next
    If formatEnumeration = MagicFormatPDF Then
      excelDocument.ExportAsFixedFormat xlTypePDF, outputFile
    Else
      excelDocument.SaveAs outputFile, formatEnumeration
    End If

    ' Close the source document.
    excelDocument.Close False
    If Err <> 0 Then
      WScript.Quit -3
    End If
    On Error GoTo 0

    ' Signal that the conversion was successful.
    WScript.Quit 2

  Else

    ' Files does not exist, could not convert
    WScript.Quit -4

  End If

End Function

' Execute the script.
Call ConvertFile( WScript.Arguments.Unnamed.Item(0), WScript.Arguments.Unnamed.Item(1), CInt(WScript.Arguments.Unnamed.Item(2)) )

3、excel_assert.vbs(这里用不到)

vbscript 复制代码
' Configure error handling to jump to next line.
On Error Resume Next

' Try to get running MS Excel instance.
Dim excelApplication
Set excelApplication = GetObject(, "Excel.Application")

' Signal whether or not such an instance could not be found.
If Err <> 0 then
  WScript.Quit -6
Else
  WScript.Quit 3
End If

4、excel_shutdown.vbs(如果不需要关闭的话也用不到)

vbscript 复制代码
' Try to get currently running instance of MS Excel.
On Error Resume Next
Dim excelApplication
Set excelApplication = GetObject(, "Excel.Application")

' If no such instance can be found, MS Excel is already shut down.
If Err <> 0 Then
  WScript.Quit 3
End If

' Try to shut down MS Excel.
excelApplication.Quit

' If this was impossible, exit with an error.
If Err <> 0 Then
  WScript.Quit -6
End If
On Error GoTo 0

' MS Excel was shut down successfully.
WScript.Quit 3

四、java调用VBS脚本执行转换

复制代码
ExcelToPdfUtil.java
java 复制代码
package com.lan.fts.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Excel转pdf
 * @author lanhezhong
 * @date 2021年4月28日
 */
public class ExcelToPdfUtil {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	private static String start_script="C:\\excel2pdf_script\\excel_start.vbs";
	private static String convert_script="C:\\excel2pdf_script\\excel_convert.vbs";
//	private static String shutdown_script="C:\\excel2pdf_script\\excel_shutdown.vbs";
	
	private static volatile ExcelToPdfUtil instance=null;
	
	private static Runtime cmdRuntime=null;
	
	private ExcelToPdfUtil(){
		
	}
	
	public static ExcelToPdfUtil getInstance(){
		if(instance==null){
			synchronized (ExcelToPdfUtil.class) {
				if(instance==null){
					instance = new ExcelToPdfUtil();
					cmdRuntime=Runtime.getRuntime();
					try {
						cmdRuntime.exec("wscript "+start_script);
					} catch (IOException e) {
						e.printStackTrace();
						throw new RuntimeException("wscript "+start_script+"fail.", e);
					}
				}
			}
		}
		return instance;
	}

	
    public boolean excelToPdf(String fromFilePath, String pdfFilePath){
		Process process = null;
    	try {
    		File f0 = new File(pdfFilePath);
    		if(f0.exists()){
    			return true;
    		}
			process = cmdRuntime.exec("wscript "+convert_script+" "+fromFilePath+" "+pdfFilePath+" 999");
			boolean b = process.waitFor(180, TimeUnit.SECONDS);
			if(!b){
				log.error("等待180s仍然转化失败。文件:{}", fromFilePath);
				return false;
			}
			File f = new File(pdfFilePath);
			if(f.exists() && f.length()>10){
				return true;
			}
			return false;
		} catch (IOException | InterruptedException e) {
    		log.error("文件转换失败:{}",fromFilePath, e);
			e.printStackTrace();
		} finally {
			try {
				process.destroyForcibly();
				process.destroy();
			}catch (Exception e){
				log.error("关闭进程失败", e);
			}
		}
    	return false;
    }
}

五、运行测试

java 复制代码
    public static void main(String[] args) {
        ExcelToPdfUtil.getInstance().excelToPdf("D:\\data\\out\\lanhezhong文件转换.xlsx", "D:\\data\\out\\lanhezhong文件转换.xlsx.pdf");
    }

运行结果:

总结:excel转pdf后,数据格式不是很好,和在excel中是不一样的。office转excel成pdf本身也有这个问题。

***********************************************************************************************
author:蓝何忠
email:lanhezhong@163.com
***********************************************************************************************

相关推荐
S. Dylan5 小时前
Edge浏览器打开PDF无法显示电子签章
edge·pdf
一马平川的大草原5 小时前
如何基于pdf2image实现pdf批量转换为图片
计算机视觉·pdf·文件拆分
m0_594526305 小时前
Python批量合并多个PDF
java·python·pdf
hairenjing11235 小时前
将图片添加到 PDF 的 5 种方法
pdf
✿゚卡笨卡6 小时前
pdf 添加页眉页脚,获取前五页
java·pdf
blegn6 小时前
PDF编辑工具Adobe Acrobat DC 2023安装教程(附安装包)
pdf·办公软件·office
patrickpdx7 小时前
如何调整pdf的页面尺寸
pdf
丁德双9 小时前
winform 加载 office excel 插入QRCode图片如何设定位置
c#·excel
星月前端10 小时前
【vue-pdf】简单封装pdf预览组件
javascript·vue.js·pdf
IT铺子10 小时前
Excel 个人时间管理工具
excel