excel转pdf的java实现

一、实现原理

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

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

二、前期准备

1、安装office软件

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

三、VBS转换脚本

1、excel_start.vbs

' 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

' 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(这里用不到)

' 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(如果不需要关闭的话也用不到)

' 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
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;
    }
}

五、运行测试

    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
***********************************************************************************************

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java中可以使用Apache POI和iText库来实现ExcelPDF功能。 首先,使用Apache POI读取Excel文件,将Excel文件中的内容读取到Java程序中。然后,使用iText库将读取到的内容写入到PDF文件中。 以下是一个简单的Java代码示例,演示如何将Excel文件换为PDF文件: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; public class ExcelToPdfConverter { public static void main(String[] args) { try { // 读取Excel文件 Workbook workbook = new HSSFWorkbook(new FileInputStream("input.xls")); // 创建PDF文档 Document pdfDoc = new Document(); PdfWriter.getInstance(pdfDoc, new FileOutputStream("output.pdf")); pdfDoc.open(); // 将Excel文件中的内容写入PDF文档中 for (int i = 0; i < workbook.getNumberOfSheets(); i++) { pdfDoc.newPage(); pdfDoc.add(workbook.getSheetAt(i).createDrawingPatriarch()); } // 关闭PDF文档和Excel文件 pdfDoc.close(); workbook.close(); System.out.println("Excel文件已成功换为PDF文件!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码示例中,我们使用了Apache POI库来读取Excel文件,使用iText库将Excel文件中的内容写入PDF文件中。请注意,在此过程中,我们使用了HSSFWorkbook类来处理.xls格式的Excel文件。如果您需要处理.xlsx格式的Excel文件,则需要使用XSSFWorkbook类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值