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");
}
}