Openoffice.org calc 상용 조작 함수 모음

  :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;
    }

좋은 웹페이지 즐겨찾기