/*
* 传进一个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横向的更多相关文章

  1. java调用wkhtmltopdf生成pdf文件,美观,省事

    最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...

  2. java调用jacob组件实现word转pdf,HTML等出现的问题

    1.部署项目的服务器上必须安装WPS或Word office: 2.将jacob.jar文件放入%JAVA_HOME%\jre中: 3.将.dll文件放入%JAVA_HOME%\jre\bin中: 4 ...

  3. PDF/WORD/EXCEL 图片预览

    一.PDF/WORD/EXCEL 转 XPS 转 第一页内容 转 图片 WORD.EXCEL转XPS (Office2010) public bool WordToXPS(string sourceP ...

  4. Java利用模板生成pdf并导出

    1.准备工作 (1)Adobe Acrobat pro软件:用来制作导出模板 (2)itext的jar包 2.开始制作pdf模板 (1)先用word做出模板界面 (2)文件另存为pdf格式文件 (3) ...

  5. 使用Java类库POI生成简易的Excel报表

    使用Java类库POI生成简易的Excel报表 1.需求 1.数据库生成报表需要转义其中字段的信息.比如 1,有效 2.无效等 2.日期格式的自数据需要转义其格式. 3.标题的格式和数据的格式需要分别 ...

  6. Java使用iText7生成PDF

    前言 我们之前使用js库html2canvas + jspdf实现html转PDF.图片,并下载(详情请戳:html页面转PDF.图片操作记录),大致原理是将页面塞到画布里,以图片的方式放到PDF中, ...

  7. JAVA调用 keytool 生成keystore 和 cer 证书

    keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据: 密钥实体( ...

  8. java 调用 keytool 生成keystore 和 cer 证书

    keytool是一个Java数据证书的管理工具, keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里, 包含两种数据:密钥实体(K ...

  9. 【PDF】java使用Itext生成pdf文档--详解

    [API接口]  一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...

随机推荐

  1. Synthesizing Images of Humans in Unseen Poses

    Synthesizing Images of Humans in Unseen Poses balakg/posewarp-cvpr2018 https://github.com/balakg/pos ...

  2. 设计模式-(17)策略模式 (swift版)

    一,概念: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们可以相互替换,让算法独立于使用它的客户而独立变化. 二,使用场景 1.针对同一类型问题的多种处理方式,仅仅是具体行为有差别时: ...

  3. XMU 1050 Diffuse Secret 【最短路】

    1050: Diffuse Secret Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 10  Solved: 8[Submit][Status][We ...

  4. Atom vim mode

    /******************************************************************** * Atom vim mode * 说明: * 想找一个具有 ...

  5. 【HAOI 2015】 树上操作

    [题目链接] 点击打开链接 [算法] 树链剖分 子树的DFS序是连续的一段! [代码] #include<bits/stdc++.h> using namespace std; #defi ...

  6. 【POJ 3140】 Contestants Division

    [题目链接] 点击打开链接 [算法] 树形DP ans = min{ | total - 2 * sum[k] | } (sum为以k为根的子树的权值和) [代码] #include <algo ...

  7. 【POJ 1155】TELE

    [题目链接] 点击打开链接 [算法] 树形DP f[i][j]表示以i为根的子树中,选了j个叶子节点,所能带来的最大收益 不难发现这就是一个经典的背包问题,不过是在树上做背包罢了 最后,判断f[1][ ...

  8. P4455 [CQOI2018]社交网络

    这个题仔细一看就是生成树计数,但是我这个记性是真的差,早就忘了.复习了一下高斯消元,然后这个题就是很裸的题了. ps:高斯消元解行列式的时候要取反. 题干: 题目背景 当今社会,在社交网络上看朋友的消 ...

  9. Linux下安装Redis及搭建主从

    Linux下安装Redis 首先在官网下载对应版本的redis包,这里本人使用的是redis-4.0.8.tar.gz.   然后在服务器中存放redis包的路径下执行tar –vxf redis-4 ...

  10. 前端性能优化之WebP

    此文已由作者吴维伟授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前端性能优化是一件很琐碎的事情.它不像其它很多技术,在确切有限的步骤下就可以把功能做好.它就像是在打扫屋子, ...