/*
* 传进一个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. shutdown的几种方式,shutdown abort的一些弊端有哪些

    1.shutdown normal     正常方式关闭数据库. 2.shutdown immediate     立即方式关闭数据库.     在SVRMGRL中执行shutdown immedia ...

  2. mongo04---基本查询

    核心: mongod: 数据库核心进程 mongos: 查询路由器,集群时用 mongo: 交互终端(客户端) 二进制导出导入: mongodump:导出bson数据 mongorestore: 导入 ...

  3. caioj1230: [图论补充]哈密顿路径

    保存模版 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> ...

  4. 1.ARC下是否有内存溢出等问题 2.@property参数 3.#import和@class的区别

    1.ARC下是否有内存溢出等问题? 答案:必须要担心啊,ARC也不是万能的.答案:必须要担心啊,ARC也不是万能的.这里主要是涉及到集合类的数据类型   比如数组,我们定义了一个可变数组muarr1, ...

  5. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...

  6. Swift4 构造体, 属性, 索引

    创建: 2018/02/19 完成: 2018/02/25 [任务表]TODO  构造体定义  定义的概要 struct 型名 { (变量/常量的定义) (构造函数的定义) (方法的定义) (其他定义 ...

  7. Swift4 函数, 元组, 运算符

    创建: 2018/02/19 完成: 2018/02/19 更新: 2018/02/25 修改标题 [Swift4 函数] -> [Swift4 函数, 元组, 运算符] 更新 :2018/03 ...

  8. C# a标签请求下载文件

    服务器文件后台处理方式: a标签: <a href="/FileUpload/DownloadFile?file=/UploadFiles/File/bfcd676b-13a8-419 ...

  9. Akka源码分析-Serialization

    今天我们来谈一下akka的序列化框架,其实序列化.反序列化是一个老生常谈的问题,那么我们为什么还要研究一下akka的序列化框架呢?不就是使用哪种序列化.反序列化方法的区别么?其实刚开始的时候我也是这么 ...

  10. IIS重新注册

    打开程序-运行-cmd:输入一下命令重新注册IISC:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i