`

save excel as pdf

 
阅读更多

1.下载jar包:

jacob-1.7.jar,见附件

将.dll文件放到system32文件夹下

 

2.实现类

import java.io.*;
import java.util.Calendar;
import java.util.Date;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ExcelToPdf {
	private String path;

	public ExcelToPdf(String path) {
		this.path = path;
	}

	public void saveExcelAsPdf(String filePath, String outFile) {
		ComThread.InitSTA();
		ActiveXComponent actcom = new ActiveXComponent("Excel.Application");
		try {
			System.out.println((new Date()).toString()
					+ "  start convert from : " + filePath + " to " + outFile);
			actcom.setProperty("Visible", new Variant(false));
			Dispatch excels = actcom.getProperty("Workbooks").toDispatch();
			Dispatch excel = Dispatch.invoke(
					excels,
					"Open",
					Dispatch.Method,
					new Object[] { filePath, new Variant(false),
							new Variant(false) }, new int[9]).toDispatch();
			Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
					outFile, new Variant(57), new Variant(false),
					new Variant(57), new Variant(57), new Variant(false),
					new Variant(true), new Variant(57), new Variant(false),
					new Variant(true), new Variant(false) }, new int[1]);
			Dispatch.call(excel, "Close", new Variant(false));
			if (actcom != null) {
				actcom.invoke("Quit", new Variant[] {});
				actcom = null;
			}
			ComThread.Release();
			System.out.println((new Date()).toString() + "  convert ok : "
					+ filePath + " to " + outFile);
		} catch (Exception es) {
			es.printStackTrace();
		}
	}

 

3.测试类

import java.io.File;

public class Test{
	public static void main(String[] args) throws Exception {
		String path = "C:/A.xls";
		System.out.println("Convert Path : " + path);
		try {
			File file = new File(path);
			if (file.exists()) {
				ExcelToPdf et = new ExcelToPdf(path);
				et.saveExcelAsPdf(path, "C:/S.pdf");
			} else {
				System.out.println("Path Not Exist,Pls Comfirm: " + path);
			}
		} catch (Exception ex) {
			System.out
					.println("Pls Check Your Format,Format Must Be: java com/olive/util/RunTask Path(Exist Path) Frequency(Run Frequency,int)");
			ex.printStackTrace();
		}
	}

}

 

分享到:
评论

相关推荐

    Microsoft Save as PDF or XPS V1.0

    微软发布的Microsoft Office 2007、2010办公软件插件,解决Word Excel转PDF。安装插件后,可以将Word、Excel直接通过Office2007另存为PDF格式文件。

    excel转pdf工具

    很好用的excel转pdf工具,安装后直接打开需要转换的excel文件,点击save as pdf 就完成转换。

    Excel、Word转PDF时,异常com.jacob.com.ComFailException: Invoke of: SaveAs

    NULL 博文链接:https://liumayulingyan.iteye.com/blog/1900122

    pdf 与EXCEL,WORD等系列办公软件转化,EXCEL合并工作表

    方法: 只要安装完后直接打开EXCEL,左上脚就有个 save as pdf 按钮 2. 有时候我们一张EXCEL里面有很多的工作表sheet1,sheet2......sheetn,如何把这边合并成一个工作表SHEET呢? 方法: 只要拷贝以下代码,在想要...

    office 2007 加载项 (save as pdf)

    这是0ffice 2007中的一项插件,可以将word文档,Excel文件转为pdf,实在是方便至极,经本人试用有效,呵呵

    Dw2Pdf Dw2Excel

    解决DataWindow原有的SaveAs功能,只能把数据内容输出到文件的缺陷。组件不依赖Excel,支持Excel 2003(xls)、Excel 2007-2021(xlsx),大大方便了PB开发人员解决报表输出为Excel文件的难题,提高工作效率,提升软件的...

    基于jacob插件实现 word,excel,ppt 转换 pdf

    Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { target, new Variant(57), new Variant(false), new Variant(57), new Variant(57), new Variant(false), new Variant(true), new ...

    Office转PDF再转图片

    由于工作需要需要把上传文件直接在网页中浏览,通过网络...附件包含:2个安装文件(SaveAsPDF和gs);4个引用DLL;1个office文件转PDF 截图类,1个配置文件。配置文件需要添加进asp.net Web.config文件中,附件中有说明。

    Excel VBA与数据库整合应用范例精讲

    实例10-7 将工作表全部数据保存为文本文件(SaveAs) 实例10-8 将工作表全部数据保存为文本文件(循环) 实例10-9 将数据库数据导出为文本文件 实例10-10 将文本文件保存为Access数据库(Access) 实例10-11 将...

    Excel_2013_VBA_and_Macros

    SAVE TIME AND SUPERCHARGE EXCEL 2013 WITH VBA AND MACROS! Use Excel® 2013 VBA and Macros to automate virtually any routine task, and save yourself hours, days, maybe even weeks. Then, learn how to...

    jacob-1.18-M2

    Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {pdfFile, new Variant(57), new Variant(false), new Variant(57), new Variant(57), new Variant(false), new Variant(true), new ...

    DocTo:简单的命令行实用程序,用于将.doc和.xls文件转换为任何受支持的格式,例如文本,RTF,CSV或PDF

    DocTo和XLSToDocTo 一个简单的实用程序,用于将Microsoft Word文档'.doc'和... 从Github发行版下载发行版-https: 更多信息,请参见更多示例,请参见产品特点将Doc / RTF / Text文件转换为任何Word SaveAs类型Doc /

    visio教程PDF

    12.1.1 File下拉菜单相Save As窗口 12.1.2 Properties对话框 12.1.3 后继保存 12.1.4 打印到文件 12.2 小结 第13章 打开绘图文件 13.1 打开已存在的Visio绘图文件 13.1.1 在启动时打开一个已...

    ms xls pdf

    The file formats that are available in the Save As dialog box vary, depending on what type of sheet is active (a worksheet, chart sheet, or other type of sheet).

    DEAP2.1软件

    an Excel file, open the file in Excel and then use the File/SaveAs option in Excel and save it as a text file. However, be sure to remove any column names first - the data file should only contain ...

    Araxis Merge Professional 2012.4162 (x86/x64).part1

    Directly open and compare the text from Microsoft Office (Word and Excel), OpenDocument, PDF and RTF files. Compare images and binary files. Synchronize folders. Perform code reviews and audits. Work...

    Araxis Merge Professional 2012.4162 (x86/x64).part2

    Directly open and compare the text from Microsoft Office (Word and Excel), OpenDocument, PDF and RTF files. Compare images and binary files. Synchronize folders. Perform code reviews and audits. Work...

    FastReport 5.1.11 D7-XE7 FullSource

    + Added OnGetCustomData/OnSaveCustomData events which allow to save custom data inside report template(look new CustomReportData demo) + Added OnLoadDetailTemplate event - used to load detail reports ...

    NopCommerce (ASP.NET商城) v2.0 源码包.zip

    Download catalog as PDF. Currency values had 4 decimal places Select payment/shipping method radio button text was not clickable Hide prices for non-registered customers didn't work on tier prices ...

    NopCommerce (ASP.NET商城) v2.0.zip

    Download catalog as PDF. Currency values had 4 decimal places Select payment/shipping method radio button text was not clickable Hide prices for non-registered customers didn't work on tier prices ...

Global site tag (gtag.js) - Google Analytics