/*
* 传进一个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. C项目实践--家庭财务管理系统

    1.功能需求分析 家庭财务管理系统给家庭成员提供了一个管理家庭财务的平台,系统可以对家庭成员的收入和支出进行增加,删除.修改和查询等操作,并能统计总收入和总支出.其主要功能需求描述如下: (1)系统主 ...

  2. ABAP WEBRFC

    通过WEBRFC实现在网页下载SMW0上传的文件 FUNCTION zhr_download_test. *"---------------------------------------- ...

  3. git 如何让单个文件回退到指定的版本【转】

    本文转载自:http://blog.csdn.net/ikscher/article/details/43851643 1.进入到文件所在文件目录,或者能找到文件的路径查看文件的修改记录 1 $ gi ...

  4. USACO44 TimeTravel 时间旅行(链表)

    第一眼看到这题,woc,这不是主席树!?旁边HZ也表示同意,然后cGh队长就慢悠悠的过来:“想什么,USACO会有主席树!?” ↓打脸不解释,大家可以去%ta的博客(这样ta就不会D飞我了~)http ...

  5. YTU 2960: 改错题--小鼠标,你要干什吗?

    2960: 改错题--小鼠标,你要干什吗? 时间限制: 1 Sec  内存限制: 128 MB 提交: 118  解决: 62 题目描述 鼠标双击不同的图标产生不同的效果,比如双击文档(documen ...

  6. 【CJOJ】Contest4 - A+B Series

    Position:http://oj.changjun.com.cn/contest.php?cid=4 A经典题目 // <A.cpp> - Sun Oct 9 15:28:01 201 ...

  7. 并不对劲的bzoj3998:loj2102:p3975:[TJOI2015]弦论

    题目大意 对于一个给定的长度为n(\(n\leq5*10^5\))的字符串, 分别求出不同位置的相同子串算作一个.不同位置的相同子串算作多个时,它的第k(\(k\leq10^9\))小子串是什么 题解 ...

  8. bzoj 3991 寻宝游戏

    题目大意: 一颗树 有一个点的集合 对于每个集合的答案为 从集合内一个点遍历集合内所有点再返回的距离最小值 每次可以选择一个点 若在集合外便加入集合 若在集合内就删除 求每次操作后这个集合的答案 思路 ...

  9. 【ZJOI 2006】 物流运输

    [题目链接] 点击打开链接 [算法] 令cost(i,j) = 第i天到第j天走相同的路线,路线长度的最小值 那么,只需筛选出第i天到第j天可以装卸货物的码头,然后将这些码头之间连边,跑弗洛伊德(或其 ...

  10. 【转】vue中的钩子函数。。

    前言 在vue开发SPA应用的过程中,多数情况下我们需要解决一个问题 就是在路由跳转的过程中需要更新你SPA应用的 title , 这一节不说其他,就展示如何使用 vue-router 的 导航钩子  ...