java调用jacob生成pdf,word,excel横向
/*
* 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[]
*/
public byte[] jacob_Office2Pdf(byte[] srcFileBytes, String postfix) {
if (srcFileBytes == null || srcFileBytes.length == 0
|| postfix.equals("") || postfix == null) {
return null;
} else {
String officeTmplPath = Consts.getTempPath()
+ UUID.randomUUID().toString() + "." + postfix;
System.out.println(officeTmplPath);
String pdfTmplPath = generateDefaultOutputFilePath(officeTmplPath);
System.out.println(pdfTmplPath);
FileOutputStream outf = null;
BufferedOutputStream bufferout = null;
try {
outf = new FileOutputStream(officeTmplPath);
bufferout = new BufferedOutputStream(outf);
bufferout.write(srcFileBytes);
bufferout.flush();
bufferout.close();
outf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
File f = new File(officeTmplPath);
if (postfix.equalsIgnoreCase("doc")
|| postfix.equalsIgnoreCase("docx")) {
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs,//
"Open", //
officeTmplPath,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch(); Dispatch.call(doc,//
"SaveAs", //
pdfTmplPath, // FileName
wdFormatPDF);
Dispatch.call(doc, "Close", false);
if (app != null) {
app.invoke("Quit", new Variant[] {});
app = null;
}
ComThread.Release();
if (f.exists()) {
f.delete();
}
byte[] content;
try {
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(pdfTmplPath));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
content = out.toByteArray();
out.close();
File pdfFile = new File(pdfTmplPath);
if (pdfFile.exists()) {
pdfFile.delete();
}
return content;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
} else if (postfix.equalsIgnoreCase("xls")
|| postfix.equalsIgnoreCase("xlsx")) {
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", new Variant(false));
Object excels = app.getProperty("Workbooks").toDispatch();
Object excel = Dispatch.invoke(
(Dispatch) excels,
"Open",
Dispatch.Method,
new Object[] { officeTmplPath, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
// new Object[] { officeTmplPath, new Variant(false),
// new Variant(false) }, new int[9]).toDispatch();
// 横向打印(2013/05/24)
// 获取activate表格
Dispatch currentSheet = Dispatch.get((Dispatch) excel,
"ActiveSheet").toDispatch();
Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup")
.toDispatch();
Dispatch.put(pageSetup, "Orientation", new Variant(2));
// Dispatch.invoke((Dispatch)excel, "SaveAs", Dispatch.Method,
// new Object[] {
// pdfTmplPath, 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]);
try {
//如果第一个sheet为空则会抛出异常
Dispatch.call(currentSheet, "SaveAs", pdfTmplPath,
new Variant(57));
} catch (Exception e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
//自动调用第二个sheet
Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets")
.toDispatch();
// 获得几个sheet
// int count = Dispatch.get(sheets, "Count").getInt();
// System.out.println(count);
Dispatch sheet = Dispatch.invoke(sheets, "Item",
Dispatch.Get, new Object[] { new Integer(2) },
new int[1]).toDispatch();
pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch();
Dispatch.put(pageSetup, "Orientation", new Variant(2));
Dispatch.call(sheet, "SaveAs", pdfTmplPath, new Variant(57));
Dispatch.call((Dispatch) excel, "Close", new Variant(false));
byte[] content;
try {
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(pdfTmplPath));
ByteArrayOutputStream out = new ByteArrayOutputStream(
1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
content = out.toByteArray();
out.close();
File pdfFile = new File(pdfTmplPath);
if (pdfFile.exists()) {
pdfFile.delete();
}
return content;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
} finally {
if (app != null) {
app.invoke("Quit", new Variant[] {});
app = null;
}
ComThread.Release();
if (f.exists()) {
f.delete();
}
}
Dispatch.call((Dispatch) excel, "Close", new Variant(false));
byte[] content;
try {
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(pdfTmplPath));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
content = out.toByteArray();
out.close();
File pdfFile = new File(pdfTmplPath);
if (pdfFile.exists()) {
pdfFile.delete();
}
return content;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
} else {
if (f.exists()) {
f.delete();
}
return null;
}
} }
java调用jacob生成pdf,word,excel横向的更多相关文章
- java调用wkhtmltopdf生成pdf文件,美观,省事
最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...
- java调用jacob组件实现word转pdf,HTML等出现的问题
1.部署项目的服务器上必须安装WPS或Word office: 2.将jacob.jar文件放入%JAVA_HOME%\jre中: 3.将.dll文件放入%JAVA_HOME%\jre\bin中: 4 ...
- PDF/WORD/EXCEL 图片预览
一.PDF/WORD/EXCEL 转 XPS 转 第一页内容 转 图片 WORD.EXCEL转XPS (Office2010) public bool WordToXPS(string sourceP ...
- Java利用模板生成pdf并导出
1.准备工作 (1)Adobe Acrobat pro软件:用来制作导出模板 (2)itext的jar包 2.开始制作pdf模板 (1)先用word做出模板界面 (2)文件另存为pdf格式文件 (3) ...
- 使用Java类库POI生成简易的Excel报表
使用Java类库POI生成简易的Excel报表 1.需求 1.数据库生成报表需要转义其中字段的信息.比如 1,有效 2.无效等 2.日期格式的自数据需要转义其格式. 3.标题的格式和数据的格式需要分别 ...
- Java使用iText7生成PDF
前言 我们之前使用js库html2canvas + jspdf实现html转PDF.图片,并下载(详情请戳:html页面转PDF.图片操作记录),大致原理是将页面塞到画布里,以图片的方式放到PDF中, ...
- JAVA调用 keytool 生成keystore 和 cer 证书
keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据: 密钥实体( ...
- java 调用 keytool 生成keystore 和 cer 证书
keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据:密钥实体(K ...
- 【PDF】java使用Itext生成pdf文档--详解
[API接口] 一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...
随机推荐
- javaScript查找HTML元素
1.通过id查找 例:查找id="intro"元素 var x=document.getElementById("intro"); 2.通过标签名查找 例:查找 ...
- YTU 2903: A--A Repeating Characters
2903: A--A Repeating Characters 时间限制: 1 Sec 内存限制: 128 MB 提交: 50 解决: 30 题目描述 For this problem,you w ...
- bzoj 4753 最佳团体 —— 01分数规划+树形背包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4753 注意赋初值为 -inf: eps 设为 1e-3 会 WA ... 代码如下: #in ...
- MultipartResolver实现文件上传功能
转自:https://www.jb51.net/article/142736.htm springMVC默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文 ...
- Mysql数据库的数据类型、索引、锁、事务和视图
Mysql数据库的数据类型.索引.锁.事务和视图 数据的类型 1)数据类型: 数据长什么样? 数据需要多少空间来存放? 系统内置数据类型和用户定义数据类型 2)MySql 支持多种列类型: 数值类型 ...
- Extjs 4 MVC中全局配置文件
Extjs 4 Config和Mixins http://kldn.iteye.com/blog/1386622 http://www.fengfly.com/html/JavaScript/ExtJ ...
- sql server 日期模糊查询
转换成varchar类型 ) like '%2010-10-09%' 两个字段拼接成一个字段 SELECT C0252_ID, C0252_name,C0252_Addr, ((select top ...
- jvm学习理解
1.本文是转载别人所写的,因为这个jvm看很多遍老是忘,转载只是备忘和查看方便. 转载地址: https://mp.weixin.qq.com/s/reFDCkUdq1QGGDs_Mnuesg 图中涉 ...
- 洛谷 P4014 分配问题 【最小费用最大流+最大费用最大流】
其实KM更快--但是这道题不卡,所以用了简单粗暴的费用流,建图非常简单,s向所有人连流量为1费用为0的边来限制流量,所有工作向t连流量为1费用为0的边,然后对应的人和工作连(i,j,1,cij),跑一 ...
- 【OpenJ_Bailian - 4152 】最佳加法表达式(动态规划)
最佳加法表达式 Descriptions: 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆 ...