一、使用Jodconverter 利用OpenOffice3.X将Office文件转换成HTML,PDF
JAVA中使用jodconverter 利用OpenOffice3.X将office文件转换成HTML或是PDF
项目中,经常有用户将一些Office文档上传到应用中。之后如果需要查看的话,只能下载,才能查看。
为了达到更好的用户体验,需要不用下载Office文件就能在网页中查看文件内容。于是只好将Office文件转换成HTML或是PDF直接显示在网页中。就实现了在网页中直接查看的效果。
这种方法需要在服务端安装OpenOffice3.X的。但是比起需要在客户端安装office还是在服务器端安装更好一点。下面就是根据网上查找的一些资料,初步实现了转换工能。
第一步:下载 jodconverter-core-3.0-beta-4.zip
解开后导入下列jar
jodconverter-core-3.0-beta-4.jar
commons-cli-1.1.jar
commons-io-1.4.jar
json-20090211.jar
juh-3.2.1.jar
jurt-3.2.1.jar
ridl-3.2.1.jar
unoil-3.2.1.jar
导入相关的包:
第二步:下载OpenOffice3.X,安装
自已写转换调用:
1.OpenOfficeConverter.java
2.FileUtils.java
/**************************转换调用类********************/
package com.rd.utils;
import java.io.File;
import java.io.FileNotFoundException;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
public class OpenOfficeConverter {
private static OfficeManager officeManager;
private static String OFFICE_HOME = "C:\\Program Files (x86)\\OpenOffice.org 3";
private static int port[] = {8100};
private static final String HTML = ".html";
private static final String PDF = ".pdf";
/**
* 转换格式
* @param inputFile 需要转换的原文件路径
* @param fileType 要转换的目标文件类型 html,pdf
*/
public void convert(String inputFile, String fileType) {
String outputFile = FileUtils.getFilePrefix(inputFile)+fileType;
if(inputFile.endsWith(".txt")){
String odtFile = FileUtils.getFilePrefix(inputFile)+".odt";
if(new File(odtFile).exists()){
System.out.println("odt文件已存在!");
inputFile = odtFile;
}else{
try {
FileUtils.copyFile(inputFile,odtFile);
inputFile = odtFile;
} catch (FileNotFoundException e) {
System.out.println("文档不存在!");
e.printStackTrace();
}
}
}
startService();
System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(inputFile),new File(outputFile));
stopService();
System.out.println();
}
public static void main(String[] args){
OpenOfficeConverter cov = new OpenOfficeConverter();
String inputFile= "d:\\使用手册1.4.doc";
cov.convert(inputFile,HTML);
}
public static void startService(){
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
System.out.println("准备启动服务....");
configuration.setOfficeHome(OFFICE_HOME);//设置OpenOffice.org安装目录
configuration.setPortNumbers(port); //设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时
officeManager = configuration.buildOfficeManager();
officeManager.start(); //启动服务
System.out.println("office转换服务启动成功!");
} catch (Exception ce) {
System.out.println("office转换服务启动失败!详细信息:" + ce);
}
}
public static void stopService(){
System.out.println("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭office转换成功!");
}
}
/****************文件工具类****************************/
package com.rd.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileUtils {
public static String getFilePrefix(String fileName){
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(0, splitIndex);
}
public static String getFileSufix(String fileName){
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{
File sFile = new File(inputFile);
File tFile = new File(outputFile);
FileInputStream fis = new FileInputStream(sFile);
FileOutputStream fos = new FileOutputStream(tFile);
int temp = 0;
try {
while ((temp = fis.read()) != -1) {
fos.write(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、Jacob来调用excel另存功能
需要下载jacob的包,该包还包含2个dll文件,一个是jacob-1.17-x64.dll,这个是64位的,还有一个是jacob-1.17-x86.dll文件,这个是32位的。将jar包包含到classpath目录,dll文件包含到jre/bin目录即可
package com.excel;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class ExcelToPdf {
private String path;
public static boolean runFlag = false;
public ExcelToPdf(String path) {
this.path = path;
}
public void saveExcelAsPdf(String filePath, String outFile) {
ComThread.InitSTA();
ActiveXComponent actcom = new ActiveXComponent("Excel.Application");
try {
System.out.println((new Date()).toString()
+ " start convert from : " + filePath + " to " + outFile);
actcom.setProperty("Visible", new Variant(false));
Dispatch excels = actcom.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[] { filePath, new Variant(false),
new Variant(false) }, new int[9]).toDispatch();
Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets")
.toDispatch();
// 获得几个sheet
int count = Dispatch.get(sheets, "Count").getInt();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
outFile, 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]);
Dispatch.call(excel, "Close", new Variant(false));
if (actcom != null) {
actcom.invoke("Quit", new Variant[] {});
actcom = null;
}
ComThread.Release();
File temp = new File(filePath);
temp.renameTo(new File(filePath + "." + getDateStr()));
temp = new File(filePath);
temp.deleteOnExit();
temp = null;
System.out.println((new Date()).toString() + " convert ok : "
+ filePath + " to " + outFile);
} catch (Exception es) {
es.printStackTrace();
}
}
public void listAllFile() {
runFlag = true;
String fileName = "", appdex = "";
File temp = null;
try {
File[] list = new File(path).listFiles(new FileFilter() {
public boolean accept(File pathname) {
boolean x = false;
if (pathname.getName().toLowerCase().endsWith(".xls")) {
x = true;
}
return x;
}
});
System.out.println((new Date()).toString()
+ " Total Convert File : " + list.length);
for (int i = 0; i < list.length; i++) {
fileName = list[i].getName().substring(0,
list[i].getName().indexOf("."));
appdex = list[i].getName().substring(
list[i].getName().indexOf("."));
temp = new File(path + fileName + ".pdf");
if (temp.exists()) {
temp.renameTo(new File(path + fileName + "-" + getDateStr()
+ ".pdf"));
}
saveExcelAsPdf(path + fileName + appdex, path + fileName
+ ".pdf");
}
} catch (Exception ex) {
ex.printStackTrace();
}
runFlag = false;
}
public String getDateStr() {
Calendar cl = Calendar.getInstance();
cl.setTime(new Date());
String str = cl.get(Calendar.YEAR) + "" + (cl.get(Calendar.MONTH) + 1)
+ "" + cl.get(Calendar.DATE) + "" + cl.get(Calendar.HOUR) + ""
+ cl.get(Calendar.MINUTE) + "" + cl.get(Calendar.SECOND);
return str;
}
public static void main(String[] args) {
ExcelToPdf etp = new ExcelToPdf("e:/czc.xls");
etp.saveExcelAsPdf("e:/czc.xls", "E:/aa.pdf");
// byte[] data = etp.jacob_Office2Pdf1(null, null);
// // ByteArrayInputStream bis=new ByteArrayInputStream(data);
// try {
// BufferedOutputStream bos = new BufferedOutputStream(
// new FileOutputStream("E:/c.pdf"));
// bos.write(data);
// bos.close();
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("E:/bb.pdf0.pdf"));
BufferedInputStream bis1=new BufferedInputStream(new FileInputStream("E:/bb.pdf1.pdf"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("E:/hb.pdf"));
byte[] array=new byte[1024];
while(bis.read(array)!=-1){
bos.write(array);
}
bis.close();
while(bis1.read(array)!=-1){
bos.write(array);
}
bis1.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[]
*/
public byte[] jacob_Office2Pdf(byte[] srcFileBytes, String postfix) {
String pdfTmplPath = "E:/bb.pdf";
String officeTmplPath = "E:/czc.xls";
File f = new File(officeTmplPath);
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();
// 获取activate表格
Dispatch currentSheet = Dispatch.get((Dispatch) excel, "ActiveSheet")
.toDispatch();
Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup")
.toDispatch();
Dispatch.put(pageSetup, "Orientation", new Variant(2));
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);
// 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;
}
}
public byte[] jacob_Office2Pdf1(byte[] srcFileBytes, String postfix) {
String pdfTmplPath = "E:/bb.pdf";
String officeTmplPath = "E:/czc.xls";
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();
// 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);
// System.out.println(count);
Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get,
new Object[] { new Integer(2) }, new int[1]).toDispatch();
Dispatch 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 = null;
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(pdfTmplPath));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
try {
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
content = out.toByteArray();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File pdfFile = new File(pdfTmplPath);
if (pdfFile.exists()) {
pdfFile.delete();
}
return content;
}
}
以上是jacob利用另存把excel转成pdf文件,但是我没有找到多个sheet转化为一个pdf的方法,我使用遍历sheet保存多个pdf文件,通过itextpdf再将这多个PDF合成一个,效率偏低不过能够实现
package com.excel;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
public class PdfOperate
{
private static final int N = 3;
public static void main(String[] args)
{
String[] files = {"E:/bb.pdf0.pdf", "E:/bb.pdf1.pdf"};
String savepath = "E:\\temp.pdf";
mergePdfFiles(files, savepath);
//partitionPdfFile("C:\\a.pdf");
}
public static void mergePdfFiles(String[] files, String savepath)
{
try
{
Document document = new Document(new PdfReader(files[0]).getPageSize(1));
PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
document.open();
for(int i=0; i<files.length; i++)
{
PdfReader reader = new PdfReader(files[i]);
int n = reader.getNumberOfPages();
for(int j=1; j<=n; j++)
{
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
}
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch(DocumentException e) {
e.printStackTrace();
}
}
public static void partitionPdfFile(String filepath)
{
Document document = null;
PdfCopy copy = null;
try
{
PdfReader reader = new PdfReader(filepath);
int n = reader.getNumberOfPages();
if(n < N)
{
System.out.println("The document does not have " + N + " pages to partition !");
return;
}
int size = n/N;
String staticpath = filepath.substring(0, filepath.lastIndexOf("\\")+1);
String savepath = null;
ArrayList<String> savepaths = new ArrayList<String>();
for(int i=1; i<=N; i++)
{
if(i < 10)
{
savepath = filepath.substring(filepath.lastIndexOf("\\")+1, filepath.length()-4);
savepath = staticpath + savepath + "0" + i + ".pdf";
savepaths.add(savepath);
}
else
{
savepath = filepath.substring(filepath.lastIndexOf("\\")+1, filepath.length()-4);
savepath = staticpath + savepath + i + ".pdf";
savepaths.add(savepath);
}
}
for(int i=0; i<N-1; i++)
{
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(savepaths.get(i)));
document.open();
for(int j=size*i+1; j<=size*(i+1); j++)
{
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
document.close();
}
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(savepaths.get(N-1)));
document.open();
for(int j=size*(N-1)+1; j<=n; j++)
{
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch(DocumentException e) {
e.printStackTrace();
}
}
}