当前位置:网站首页>一、excel转pdf格式jacob.jar
一、excel转pdf格式jacob.jar
2022-07-31 10:44:00 【HJHO】
1.下载jacob.jar包
jacob资料
2.将jacob-1.17-M2-x64.dll 存放在jdk/bin目录下


3.导入本地依赖
<dependency>
<groupId>com.jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.19</version>
<scope>system</scope>
<!--本地的jacob.jar的路径-->
<systemPath>D:\jacob-1.17-M2\jacob.jar</systemPath>
</dependency>
4.直接传入输入的excel和输出的pdf路径
示例代码:
package com.ysb.zy.util.pdf;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Utils {
/**
* 将excel到pdf文件
* @param inputFilePath
* @param outputFilePath
*/
public static void jacobExcelToPDFs(String inputFilePath, String outputFilePath) {
ActiveXComponent ax = null;
Dispatch excel = null;
try {
ComThread.InitSTA();
ax = new ActiveXComponent("Excel.Application");
ax.setProperty("Visible", new Variant(false));
//禁用宏
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();
//获取到sheets的集合对象
Dispatch sheets = Dispatch.get(excel, "sheets").toDispatch();
//获取到总表数
int count = Dispatch.get(sheets, "count").changeType(Variant.VariantInt).getInt();
for (int i = 1; i <= count; i++) {
//获取到sheet页
Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[]{1}, new int[1]).toDispatch();
Dispatch page = Dispatch.get(sheet, "PageSetup").toDispatch();
//设置横向打印还是纵向打印
Dispatch.put(page, "Orientation", 2);
//设置缩放,值为100或false
Dispatch.put(page, "Zoom", false);
//所有行为一页
Dispatch.put(page, "FitToPagesTall", false);
//所有列为一页(1或false)
Dispatch.put(page, "FitToPagesWide", 1);
}
//转换格式
Object[] obj2 = {
//PDF格式等于0
new Variant(0),
outputFilePath,
//0=标准(生成的PDF图片不会模糊),1=最小的文件
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();
}
}
public static void main(String[] args) {
// D:\test
jacobExcelToPDFs("D:\\test\\test.xlsx","D:\\test\\test.pdf");
}
}
边栏推荐
- GCD简单了解
- 【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
- 解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
- CoCube群机器人预览→资讯剧透←
- SQL study notes - REGEXP operator
- Day113.尚医通:用户认证、阿里云OSS、就诊人管理
- 突破传统可靠性测试:混沌工程优秀实践
- C#多态的实现
- 《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
- NowCoderTOP23-27 Binary tree traversal - continuous update ing
猜你喜欢
随机推荐
darknet 源码阅读笔记-01-activation_kernels.cu
SQL去重的三种方法汇总
ASP.NET 身份认证框架 Identity(一)
PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
细讲DDD领域驱动设计
双链表的创建
csdn文件导出为pdf
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
Can I find a Go job in 7 days?Learn Go with arrays and pointers
redis-企业级使用
一种用于保证多方子系统数据一致性的方法
csdn file export to pdf
1161. 最大层内元素和 (二叉树的层序遍历)
Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management
KVM虚拟化作业
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
The principle of v-model
LeetCode二叉树系列——101.对称二叉树
《云原生的本手、妙手和俗手》——2022全国新高考I卷作文
sql力扣刷题六









