package org.gfg.test.servlet;
import java.io.File;
import java.net.ConnectException;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import ooo.connector.BootstrapSocketConnector;
public class ExcelTOPdf {
/**
* ????EXCEL???????????
* @param rootSourceFilePath
* @param globalVariable
*/
private static void convertExcel(String rootSourceFilePath, String globalVariable,String outputFilePath) {
XComponentLoader xComponentLoader=null;
Object doc=null;
try {
// ????OpenOffice
XComponentContext xRemoteContext = BootstrapSocketConnector
.bootstrap(globalVariable);
XMultiComponentFactory xRemoteServiceManager = xRemoteContext
.getServiceManager();
Object desktop = xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", xRemoteContext);
xComponentLoader = (XComponentLoader) UnoRuntime
.queryInterface(XComponentLoader.class, desktop);
PropertyValue[] loadProps = new PropertyValue[1];
loadProps[0] = new PropertyValue();
loadProps[0].Name = "Hidden";
loadProps[0].Value = new Boolean(true);
doc = xComponentLoader
.loadComponentFromURL("file:///"+rootSourceFilePath, "_blank", 0,
loadProps);
XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime
.queryInterface(XSpreadsheetDocument.class,
doc);
com.sun.star.style.XStyleFamiliesSupplier xFamiliesSupplier =
(com.sun.star.style.XStyleFamiliesSupplier) UnoRuntime.queryInterface(
com.sun.star.style.XStyleFamiliesSupplier.class, xSpreadsheetDocument);
com.sun.star.container.XNameAccess xFamiliesNA = xFamiliesSupplier.getStyleFamilies();
Object aPageStylesObj = xFamiliesNA.getByName("PageStyles");
com.sun.star.container.XNameContainer xPageStylesNA = (com.sun.star.container.XNameContainer)
UnoRuntime.queryInterface(com.sun.star.container.XNameContainer.class, aPageStylesObj);
String []stylename=xPageStylesNA.getElementNames();
for(int j=0;j<stylename.length;j++){
Object style=xPageStylesNA.getByName(stylename[j]);
com.sun.star.style.XStyle xPageStyleNA = (com.sun.star.style.XStyle)
UnoRuntime.queryInterface(com.sun.star.style.XStyle.class, style);
if(xPageStyleNA.isInUse()){
XPropertySet xPropSet = (XPropertySet)UnoRuntime. queryInterface(
XPropertySet.class, xPageStyleNA);
try{
xPropSet.setPropertyValue("ScaleToPages", new Short((short)1));
}catch(Exception e1){
e1.printStackTrace();
}
}
}
//转化
// Preparing properties for converting the document
PropertyValue[] propertyValues = new PropertyValue[2];
// Setting the flag for overwriting
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Overwrite";
propertyValues[0].Value = new Boolean(true);
// Setting the filter name
propertyValues[1] = new PropertyValue();
propertyValues[1].Name = "FilterName";
propertyValues[1].Value = "writer_pdf_Export";
// Composing the URL by replacing all backslashs
File outputFile = new File(outputFilePath);
String outputUrl = "file:///" + outputFile.getAbsolutePath().replace('\\', '/');
// Getting an object that will offer a simple way to store
// a document to a URL.
XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, doc);
// Storing and converting the document
storable.storeToURL(outputUrl, propertyValues);
} catch (java.lang.Exception e) {
e.printStackTrace();
} finally {
if(doc!=null){
XComponent component = (XComponent) UnoRuntime.queryInterface(XComponent.class, doc);
component.dispose();
}
}
}
public static void main(String[] args) {
convertExcel("c:\\test.xls","C:\\Program Files (x86)\\OpenOffice 4\\program","c:\\test.pdf");
System.exit(0);
}
}
Openoffice转换多列EXCEL为PDF行列对应解决方法
5星 · 超过95%的资源 需积分: 50 116 浏览量
2017-08-03
13:58:52
上传
评论 1
收藏 1.27MB ZIP 举报
jackscsnd
- 粉丝: 2
- 资源: 3
- 1
- 2
前往页