轉載:http://blog.csdn.net/kdzxiaoli/article/details/4274351
/*******************************************************
* 操作Open office.org Calc文檔的常用函數
* @author lishijin
* @date 2009-06-13 19:554
*******************************************************/
package com.lishijin.pm.common.base;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.container.XIndexAccess;
import com.sun.star.document.XStorageBasedDocument;
import com.sun.star.embed.ElementModes;
import com.sun.star.embed.XStorage;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.io.XInputStream;
import com.sun.star.io.XOutputStream;
import com.sun.star.io.XSeekable;
import com.sun.star.io.XStream;
import com.sun.star.io.XTruncate;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.table.XCell;
import com.sun.star.table.XCellRange;
import com.sun.star.table.XColumnRowRange;
import com.sun.star.table.XTableColumns;
import com.sun.star.table.XTableRows;
import com.sun.star.text.XText;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XMergeable;
/**
* Oracle from export data to openoffice Calc
* @author lsj
*/
public class OOoCommon {
/** Constructor. */
public OOoCommon() {
}
/**
* openoffice程序加載
*/
public static Object getBootstrap() throws Exception {
// 加載opernOffice
XComponentContext xcomponentcontext = null;
XMultiComponentFactory xmulticomponentfactory = null;
try {
xcomponentcontext = Bootstrap.bootstrap();
xmulticomponentfactory = xcomponentcontext.getServiceManager();
} catch (BootstrapException e) {
e.printStackTrace();
}
Object desktop = xmulticomponentfactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xcomponentcontext);
return desktop;
}
/**
* 讀取模板文件生成組件對象
* @param templateFileUrl 模板文件URL
* @return 組件對象
*/
public static XComponent getSpreadsheetComponent(Object desktop, String templateFileUrl) throws Exception {
XComponentLoader xComponentLoader =(XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, desktop);
PropertyValue[] loadProps = new PropertyValue[2];
loadProps[0] = new PropertyValue();
loadProps[0].Name = "Hidden";//隱示打開
loadProps[0].Value = new Boolean(true);
loadProps[1] = new PropertyValue();
loadProps[1].Name = "AsTemplate";//模板文件標誌
loadProps[1].Value = new Boolean(true);
XComponent xSpreadsheetComponent =
xComponentLoader.loadComponentFromURL(templateFileUrl, "_blank",0, loadProps);
return xSpreadsheetComponent;
}
/**
* 讀取Calc文件文件取得組件對象
* @param fileUrl 模板文件URL
* @return 組件對象
*/
public static XComponent readSpreadsheetComponent(Object desktop, String fileUrl) throws Exception {
XComponentLoader 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);
XComponent xSpreadsheetComponent =
xComponentLoader.loadComponentFromURL(fileUrl, "_blank",0, loadProps);
return xSpreadsheetComponent;
}
/**
* 取得Calc文件對象
* @param xSpreadsheetComponent 組件對象
* @return Calc文件對象
*/
public static XSpreadsheetDocument getXSpreadsheetDocument(XComponent xSpreadsheetComponent) throws Exception {
XSpreadsheetDocument xSpreadsheetDocument =
(XSpreadsheetDocument)UnoRuntime.queryInterface(XSpreadsheetDocument.class,xSpreadsheetComponent);
return xSpreadsheetDocument;
}
/**
* 通過Sheet名稱取得Sheet對象
* @param xSpreadsheetDocument Calc文件對象
* @param sheetName Sheet名稱
* @return Sheet對象
*/
public static XSpreadsheet getXSpreadsheetByName(XSpreadsheetDocument xSpreadsheetDocument, String sheetName) throws Exception {
XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
Object sheet = xSpreadsheets.getByName(sheetName);
XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheet);
return xSpreadsheet;
}
/**
* 通過Sheet序號取得Sheet對象
* @param xSpreadsheetDocument Calc文件對象
* @param index Sheet序號
* @return Sheet對象
*/
public static XSpreadsheet getXSpreadsheetByIndex(XSpreadsheetDocument xSpreadsheetDocument, int index) throws Exception {
XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
// get via the index access the first sheet
XIndexAccess xElements = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
// specify the first sheet from the spreadsheet
XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(
XSpreadsheet.class, xElements.getByIndex(index));
return xSpreadsheet;
}
/**
* 取得錶行對象
* @param spreadSheet Sheet對象
* @return 錶行對象
*/
public static XTableRows getXTableRows(XSpreadsheet spreadSheet) {
XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, spreadSheet);
XColumnRowRange xCRRange =
( com.sun.star.table.XColumnRowRange ) UnoRuntime.queryInterface(
com.sun.star.table.XColumnRowRange.class, xSpreadsheet );
XTableRows xRows = xCRRange.getRows();
return xRows;
}
/**
* 取得表列對象
* @param spreadSheet Sheet對象
* @return 表列對象
*/
public static XTableColumns getXTableColumns(XSpreadsheet spreadSheet) {
XSpreadsheet xSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, spreadSheet);
XColumnRowRange xCRRange =
( com.sun.star.table.XColumnRowRange ) UnoRuntime.queryInterface(
com.sun.star.table.XColumnRowRange.class, xSpreadsheet );
XTableColumns xColumns = xCRRange.getColumns();
return xColumns;
}
/**
* 插入列
* @param xColumns 列對象
* @param index 插入序號(0-Based)
* @param count 插入的列數
*/
public static void insertColumns(XTableColumns xColumns, int index, int count) {
xColumns.insertByIndex(index,count);
}
/**
* 插入行
* @param xRows 行對象
* @param index 插入序號(0-Based)
* @param count 插入的行數
*/
public static void insertRows(XTableRows xRows, int index, int count) {
xRows.insertByIndex(index,count);
}
/**
* 刪除列
* @param xColumns 列對象
* @param index 刪除序號(0-Based)
* @param count 刪除的列數
*/
public static void removeColumns(XTableColumns xColumns, int index, int count) {
xColumns.removeByIndex(index,count);
}
/**
* 刪除行
* @param xColumns 行對象
* @param index 刪除序號(0-Based)
* @param count 刪除的行數
*/
public static void removeRows(XTableRows xRows, int index, int count) {
xRows.removeByIndex(index,count);
}
/**
* 寫入單元格
* @param xSpreadsheet Sheet對象
* @param col 列番號
* @param row 行番號
* @param value 寫入的字符串
*/
public static void setTextValueOfXCellAtPosition(XSpreadsheet xSpreadsheet, int col,
int row, String value) throws Exception {
XCell xCell = xSpreadsheet.getCellByPosition(col, row);
XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
xCellText.setString(value);
}
/**
* 寫入單元格整形數據類型
* @param xSpreadsheet Sheet對象
* @param col 列番號
* @param row 行番號
* @param value 寫入的字符串
*/
public static void setNumValueOfXCellAtPosition(XSpreadsheet xSpreadsheet, int col,
int row, double value) throws Exception {
XCell xCell = xSpreadsheet.getCellByPosition(col, row);
xCell.setValue(value);
}
/**
* 取得單元格對象
* @param xSpreadsheet Sheet對象
* @param col 列番號
* @param row 行番號
* @return 單元格對象
*/
public static XCell getXCellByPosition(XSpreadsheet xSpreadsheet, int col, int row) throws Exception {
return xSpreadsheet.getCellByPosition(col, row);
}
/**
* 取得單元格(字符串類型)
* @param xSpreadsheet Sheet對象
* @param col 列番號
* @param row 行番號
* @return 取得的字符串
*/
public static String getTextValueOfXCellAtPosition(XSpreadsheet xSpreadsheet, int col,int row)
throws Exception {
XCell xCell = xSpreadsheet.getCellByPosition(col, row);
XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
return xCellText.getString();
}
/**
* 取得單元格(整形數據類型)
* @param xSpreadsheet Sheet對象
* @param col 列番號
* @param row 行番號
* @return 取得的字符串
*/
public static double getNumValueOfXCellAtPosition(XSpreadsheet xSpreadsheet, int col,int row)
throws Exception {
XCell xCell = xSpreadsheet.getCellByPosition(col, row);
double iCellValue = xCell.getValue();
return iCellValue;
}
/**
* 獲取域
* @param xSpreadsheet Sheet對象
* @param col 開始列號
* @param row 開始行號
* @param col1 結束列號
* @param row1 結束行號
* @return 域對象
*/
public static XCellRange getRange(XSpreadsheet xSpreadsheet, int col, int row, int col1,
int row1) throws Exception {
XCellRange xCellRange = xSpreadsheet.getCellRangeByPosition(col, row,
col1, row1);
return xCellRange;
}
/**
* 單元格合併
* @param xSpreadsheet sheet對象
* @param col 開始列號
* @param row 開始行號
* @param col1 結束列號
* @param row1 結束行號
*/
public static void mergeCellRange(XSpreadsheet xSpreadsheet, int col, int row, int col1, int row1)
throws Exception {
XCellRange xCellRange = OOoCommon.getRange(xSpreadsheet,col,row,col1,row1);
XMergeable xMerge = (XMergeable)
UnoRuntime.queryInterface(XMergeable.class, xCellRange);
//合併單元格
xMerge.merge(true);
}
/**
* 將XComponent對象轉換成byte[]數據類型
* @param xComponent 組件對象
* @param byte數據
*/
public static byte[] getRawDataFromStream(XComponent xComponent) throws Exception {
XStorageBasedDocument xStorageBasedDocument = (XStorageBasedDocument) UnoRuntime
.queryInterface(XStorageBasedDocument.class, xComponent);
XStorage xStorage = xStorageBasedDocument.getDocumentStorage();
XStorage substorage = xStorage.openStorageElement("Versions",
ElementModes.READWRITE);
XStream stream = substorage.openStreamElement("0.0.1",
ElementModes.READWRITE);
XOutputStream os = stream.getOutputStream();
XTruncate truncate = (XTruncate) UnoRuntime.queryInterface(
XTruncate.class, os);
truncate.truncate();
PropertyValue[] argh = new PropertyValue[2];
argh[0] = new PropertyValue();
argh[0].Name = "FilterName";
argh[0].Value = "StarOffice XML (Calc)";
argh[1] = new PropertyValue();
argh[1].Name = "OutputStream";
argh[1].Value = os;
// create storable object from document component and store to stream
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
XStorable.class, xComponent);
xStorable.storeToURL("private:stream", argh);
// get input stream and skeekable so we can read the stream
XInputStream is = (XInputStream) UnoRuntime.queryInterface(
XInputStream.class, os);
XSeekable xSeekable = (XSeekable) UnoRuntime.queryInterface(
XSeekable.class, os);
xSeekable.seek(0);
// read and return the bytes
byte[][] t_bytes = new byte[1][(int) xSeekable.getLength()];
is.readBytes(t_bytes, (int) xSeekable.getLength());
return t_bytes[0];
}
/**
* 將絕對路徑轉換成URL
* @throws IOException IO異常
* @throws FileNotFoundException 文件找不到異常
* @return 文件URL
*/
public static String fileToUrl(String strPath) throws IOException,FileNotFoundException {
File file = new File(strPath);
String sbTmp = "file:///";
String target = file.toURL().toString().substring(6);
return sbTmp + target;
}