java 调用openoffice 服务 将excel ,word 转pdf

cd C:\Program Files\OpenOffice.org 3\program 
         soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 

 

 

 

 

import java.io.*;

import java.net.ConnectException;

 

import javax.activation.MimeType;

 

import org.junit.Test;

 

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;

import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.DocumentFamily;

import com.artofsolving.jodconverter.DocumentFormat;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

 

/**

 * Title: 描述: </p>

 * 

 * @author ming ming_7755@163.com

 * @Date:2010-8-4

 * @Revision:1.0 The Class ExcelToPdf.java

 */

 

public class ExcelToPdf {

 

public int DEFAULT_PORT = 8100;

 

public String DEFAULT_HOST = "127.0.0.1";

/**

* 执行前,请启动openoffice服务

* 进入$OO_HOME\program下

* 执行soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 

* @param xlsfile  

* @param targetfile

* @throws Exception

*/

public static void SaveAs(String xlsfile, String targetfile)

throws Exception {

File xlsf = new File(xlsfile);

File targetF = new File(targetfile);

// 获得文件格式

DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();

DocumentFormat pdfFormat = formatReg.getFormatByFileExtension("pdf");

DocumentFormat xlsFormat = formatReg.getFormatByFileExtension("xls");

// stream 流的形式

InputStream inputStream = new FileInputStream(xlsf);

OutputStream outputStream = new FileOutputStream(targetF);

 

/**

*/

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

System.out.println(connection);

try {

 

connection.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(

connection);

 

System.out.println("inputStream------" + inputStream);

System.out.println("outputStream------" + outputStream);

converter.convert(inputStream, xlsFormat, outputStream, pdfFormat);

} catch (ConnectException e) {

e.printStackTrace();

} finally {

if (connection != null) {

connection.disconnect();

connection = null;

}

}

}

 

@Test

public void testXlsToPDF() throws Exception {

String path1 = "D:/12.xls";

String path2 = "D:/1.pdf";

new ExcelToPdf().SaveAs(path1, path2);

System.out.println("ok");

}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值