// comment: Step 1: get the Desktop object from the office // Step 2: open an empty Calc document // Step 3: enter an example text, set the numberformat to DM // Step 4: change the numberformat to EUR (Euro) // Step 5: use the DM/EUR factor on each cell with a content
publicstaticvoid main(String args[]) { // You need the desktop to create a document // The getDesktop method does the UNO bootstrapping, gets the // remote service manager and the desktop object.
com.sun.star.frame.XDesktop xDesktop = null;
xDesktop = getDesktop();
// create a sheet document
XSpreadsheetDocument xSheetdocument = null;
xSheetdocument = createSheetdocument( xDesktop );
System.out.println( "Create a new Spreadsheet" );
// get the collection of all sheets from the document
XSpreadsheets xSheets = null;
xSheets = xSheetdocument.getSheets();
// the Action Interface provides methods to hide actions, // like inserting data, on a sheet, that increase the performance
XActionLockable xActionInterface = null;
xActionInterface = UnoRuntime.queryInterface(
XActionLockable.class, xSheetdocument );
// lock all actions
xActionInterface.addActionLock();
com.sun.star.sheet.XSpreadsheet xSheet = null; try { // get via the index access the first sheet
XIndexAccess xElements = UnoRuntime.queryInterface(
XIndexAccess.class, xSheets );
// specify the first sheet from the spreadsheet
xSheet = UnoRuntime.queryInterface(
XSpreadsheet.class, xElements.getByIndex( 0 ));
} catch( Exception e) {
e.printStackTrace(System.err);
}
// get the interface to apply and create new numberformats
XNumberFormatsSupplier xNumberFormatSupplier = null;
xNumberFormatSupplier = UnoRuntime.queryInterface(
XNumberFormatsSupplier.class, xSheetdocument );
XNumberFormats xNumberFormats = null;
xNumberFormats = xNumberFormatSupplier.getNumberFormats();
// insert some example data in a sheet
createExampleData( xSheet, xNumberFormats );
System.out.println( "Insert example data and use the number format with the currency 'DM'" );
// Change the currency from the cells from DM to Euro
Convert( xSheet, xNumberFormats, "DM", "EUR", 1.95583f );
System.out.println( "Change the number format to EUR and divide the values with the factor 1.95583" );
// remove all locks, the user see all changes
xActionInterface.removeActionLock();
// Numberformat string with sNewSymbol
String sSimple = "0 [$" + sNewSymbol + "]"; // create a number format key with the sNewSymbol int iSimpleKey = NumberFormat( xNumberFormats, sSimple, xLanguage );
// you have to use the FormatSupplier interface to get the // CellFormat enumeration
XCellFormatRangesSupplier xCellFormatSupplier =
UnoRuntime.queryInterface(
XCellFormatRangesSupplier.class, xSheet );
// getCellFormatRanges() has the interfaces for the enumeration
XEnumerationAccess xEnumerationAccess =
UnoRuntime.queryInterface(
XEnumerationAccess.class,
xCellFormatSupplier.getCellFormatRanges() );
while( xRanges.hasMoreElements() ) { // the enumeration returns a cellrange
XCellRange xCellRange = UnoRuntime.queryInterface(
XCellRange.class, xRanges.nextElement());
// the PropertySet the get and set the properties from the cellrange
XPropertySet xCellProp = UnoRuntime.queryInterface(
XPropertySet.class, xCellRange );
// getPropertyValue returns an Object, you have to cast it to // type that you need
Object oNumberObject = xCellProp.getPropertyValue( "NumberFormat" ); int iNumberFormat = AnyConverter.toInt(oNumberObject);
// get the properties from the cellrange numberformat
XPropertySet xFormat = xNumberFormats.getByKey(iNumberFormat );
short fType = AnyConverter.toShort(xFormat.getPropertyValue("Type"));
String sCurrencySymbol = AnyConverter.toString(
xFormat.getPropertyValue("CurrencySymbol"));
// change the numberformat only on cellranges with a // currency numberformat if( ( (fType & com.sun.star.util.NumberFormat.CURRENCY) > 0) &&
( sCurrencySymbol.equals( sOldSymbol ) ) ) { boolean bThousandSep = AnyConverter.toBoolean(
xFormat.getPropertyValue("ThousandsSeparator")); boolean bNegativeRed = AnyConverter.toBoolean(
xFormat.getPropertyValue("NegativeRed")); short fDecimals = AnyConverter.toShort(
xFormat.getPropertyValue("Decimals")); short fLeadingZeros = AnyConverter.toShort(
xFormat.getPropertyValue("LeadingZeros"));
Locale oLocale = (Locale) AnyConverter.toObject( new com.sun.star.uno.Type(Locale.class),
xFormat.getPropertyValue("Locale"));
// create a new numberformat string
String sNew = xNumberFormats.generateFormat( iSimpleKey,
oLocale, bThousandSep, bNegativeRed,
fDecimals, fLeadingZeros );
// get the NumberKey from the numberformat int iNewNumberFormat = NumberFormat( xNumberFormats,
sNew, oLocale );
// set the new numberformat to the cellrange DM->EUR
xCellProp.setPropertyValue( "NumberFormat",
Integer.valueOf( iNewNumberFormat ) );
// iterate over all cells from the cellrange with an // content and use the DM/EUR factor
XCellRangesQuery xCellRangesQuery = UnoRuntime.queryInterface(
XCellRangesQuery.class, xCellRange );
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
// get the remote office service manager
xMCF = xContext.getServiceManager(); if( xMCF != null ) {
System.out.println("Connected to a running office ...");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.