Wednesday, July 30, 2008

sample program to opoen open office spread sheet from Java

//package SpreadSheetAPI;

import com.sun.star.beans.PropertyValue;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNamed;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XCellRangeAddressable;
import com.sun.star.sheet.XSheetCellCursor;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XUsedAreaCursor;
import com.sun.star.table.CellRangeAddress;
import com.sun.star.table.XCell;
import com.sun.star.text.XText;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import spreadsheetUtilities.*;

/**
* A Sample for access xls/ods from OOo API
* Requirement:
* 1.Install OOo 2.0
* 2.Add jars to your project.(In OOoInstallPath\program\classes)
* at least those jars must be included : juh.jar,jurt.jar,ridl.jar,unoil.jar
*
**/
public class ReadSheetCellsSample {

private XComponentContext xRemoteContext = null;

private XMultiComponentFactory xRemoteServiceManager = null;

public ReadSheetCellsSample() {
}

public static void main(String[] args) {
ReadSheetCellsSample helloTextTableShape1 = new ReadSheetCellsSample();
try {
helloTextTableShape1.outSheets();
} catch (java.lang.Exception e) {
e.printStackTrace();
} finally {
System.out.println("...End");
System.exit(0);
}

}

protected void outSheets() throws java.lang.Exception {
// xls,service
XComponent xCalcComponent = loadComponent("D:\\Temp\\sample.ods");

XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime
.queryInterface(XSpreadsheetDocument.class, xCalcComponent);

com.sun.star.sheet.XSpreadsheets xSheets = xSpreadsheetDocument.getSheets();
// XIndexAccesssheet
XIndexAccess xIndexedSheets = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xSheets);

int sheetCount = xIndexedSheets.getCount();
System.out.println("Sheet Count:" + sheetCount);

for (int i = 0; i < sheetCount; i++) {
Object sheet = xIndexedSheets.getByIndex(i);
XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
XSpreadsheet.class, sheet);
// XNamedSheet
XNamed xSheetName = (XNamed) UnoRuntime.queryInterface(
XNamed.class, sheet);
System.out.println("Sheet " + i + ":" + xSheetName.getName());

outSheet(xSheet);
}
//To wait in the application..
//wait(5000);
//CalctryCalcsoffice.bin?soffice.exe
//xCalcComponent.dispose();
}

protected void outSheet(XSpreadsheet xSheet) throws Exception {

//Cursor
XSheetCellCursor xCursor = xSheet.createCursor();

// XUsedAreaCursor,
XUsedAreaCursor xUsedCursor = (XUsedAreaCursor) UnoRuntime
.queryInterface(XUsedAreaCursor.class, xCursor);
xUsedCursor.gotoStartOfUsedArea(false);
xUsedCursor.gotoEndOfUsedArea(true);
XCellRangeAddressable xSheetRange = (XCellRangeAddressable) UnoRuntime
.queryInterface(XCellRangeAddressable.class, xCursor);
CellRangeAddress cellRangeAddress = xSheetRange.getRangeAddress();

System.out.println(cellRangeAddress.StartRow);
System.out.println(cellRangeAddress.StartColumn);
System.out.println(cellRangeAddress.EndRow);
System.out.println(cellRangeAddress.EndColumn);
int rowCount = cellRangeAddress.EndRow + 1;
int colCount = cellRangeAddress.EndColumn + 1;

System.out.println("row count:" + rowCount + ",column count" + colCount);

for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
System.out.println("Column : "+ col + " Row : "+row);
XCell cell = xCursor.getCellByPosition(col, row);
String formula = cell.getFormula();
XText xText = (XText) UnoRuntime.queryInterface(XText.class,
cell);
String text = xText.getString();
System.out.println("\t" + row + "," + col + "," + formula + ","
+ text);
cell.setValue(219);
cell.setFormula("=sum(A1,A2)");

}
}
}

protected XComponent loadComponent(String filePath)
throws java.lang.Exception {
String loadUrl = "file:///" + filePath;

xRemoteServiceManager = this.getRemoteServiceManager();
Object desktop = xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", xRemoteContext);

XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime
.queryInterface(XComponentLoader.class, desktop);
PropertyValue[] loadProps = new PropertyValue[3];

loadProps[0] = new PropertyValue();
loadProps[0].Name = "ReadOnly";
loadProps[0].Value = new Boolean(true);

loadProps[1] = new PropertyValue();
loadProps[1].Name = "DocumentTitle";
loadProps[1].Value = "PMGUI Report";

//Default all macros will be Run...
loadProps[2] = new PropertyValue();
loadProps[2].Name = "MacroExecutionMode";
loadProps[2].Value = 4;

//loadProps[2] = new PropertyValue();
//loadProps[2].Name = "Hidden";
//loadProps[2].Value = new Boolean(true);

//loadProps[1] = new PropertyValue();
//loadProps[1].Name = "FilterName";
//loadProps[1].Value = "writer_pdf_Export";

return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0,loadProps);
}

protected XMultiComponentFactory getRemoteServiceManager()
throws java.lang.Exception {
if (xRemoteContext == null && xRemoteServiceManager == null) {
try {
// First step: get the remote office component context
xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");
xRemoteServiceManager = xRemoteContext.getServiceManager();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
return xRemoteServiceManager;
}

}

No comments: