using System; using unoidl.com.sun.star.lang; using unoidl.com.sun.star.uno; using unoidl.com.sun.star.frame; using unoidl.com.sun.star.util;
namespace cliversion
{ publicclass Version
{ public Version()
{ try
{ // System.Diagnostics.Debugger.Launch();
//link with cli_ure.dll
uno.util.WeakBase wb = new uno.util.WeakBase(); using ( SpreadsheetSample aSample = new SpreadsheetSample() )
{
aSample.doCellRangeSamples();
aSample.terminate();
}
} catch (System.Exception )
{ //This exception is thrown if we link with a library which is not //available throw;
}
}
}
class SpreadsheetSample: SpreadsheetDocHelper
{ public SpreadsheetSample()
{
} /** All samples regarding the service com.sun.star.sheet.SheetCellRange. */ publicvoid doCellRangeSamples()
{
unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet( 0 );
unoidl.com.sun.star.table.XCellRange xCellRange = null;
unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
unoidl.com.sun.star.table.CellRangeAddress aRangeAddress = null;
// Preparation
setFormula( xSheet, "B5", "First cell" );
setFormula( xSheet, "B6", "Second cell" ); // Get cell range B5:B6 by position - (column, row, column, row)
xCellRange = xSheet.getCellRangeByPosition( 1, 4, 1, 5 );
// --- Change cell range properties. ---
xPropSet = (unoidl.com.sun.star.beans.XPropertySet) xCellRange; // from com.sun.star.styles.CharacterProperties
xPropSet.setPropertyValue( "CharColor", new uno.Any( (Int32) 0x003399 ) );
xPropSet.setPropertyValue( "CharHeight", new uno.Any( (Single) 20.0 ) ); // from com.sun.star.styles.ParagraphProperties
xPropSet.setPropertyValue( "ParaLeftMargin", new uno.Any( (Int32) 500 ) ); // from com.sun.star.table.CellProperties
xPropSet.setPropertyValue( "IsCellBackgroundTransparent", new uno.Any( false ) );
xPropSet.setPropertyValue( "CellBackColor", new uno.Any( (Int32) 0x99CCFF ) );
// --- Replace text in all cells. ---
unoidl.com.sun.star.util.XReplaceable xReplace =
(unoidl.com.sun.star.util.XReplaceable) xCellRange;
unoidl.com.sun.star.util.XReplaceDescriptor xReplaceDesc =
xReplace.createReplaceDescriptor();
xReplaceDesc.setSearchString( "cell" );
xReplaceDesc.setReplaceString( "text" ); // property SearchWords searches for whole cells!
xReplaceDesc.setPropertyValue( "SearchWords", new uno.Any( false ) ); int nCount = xReplace.replaceAll( xReplaceDesc );
// --- Cell range data ---
prepareRange( xSheet, "A9:C30", "XCellRangeData" );
xCellRange = xSheet.getCellRangeByName( "A10:C30" );
unoidl.com.sun.star.sheet.XCellRangeData xData =
(unoidl.com.sun.star.sheet.XCellRangeData) xCellRange;
uno.Any [][] aValues =
{ new uno.Any [] { new uno.Any( "Name" ), new uno.Any( "Fruit" ), new uno.Any( "Quantity" ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Apples" ), new uno.Any( (Double) 3.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 7.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Apples" ), new uno.Any( (Double) 3.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Apples" ), new uno.Any( (Double) 9.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Apples" ), new uno.Any( (Double) 5.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 6.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 3.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Apples" ), new uno.Any( (Double) 8.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 1.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 2.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 7.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Apples" ), new uno.Any( (Double) 1.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Apples" ), new uno.Any( (Double) 8.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 8.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Apples" ), new uno.Any( (Double) 7.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Apples" ), new uno.Any( (Double) 1.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 9.0 ) }, new uno.Any [] { new uno.Any( "Bob" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 3.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Oranges" ), new uno.Any( (Double) 4.0 ) }, new uno.Any [] { new uno.Any( "Alice" ), new uno.Any( "Apples" ), new uno.Any( (Double) 9.0 ) }
};
xData.setDataArray( aValues );
// --- Get cell range address. ---
unoidl.com.sun.star.sheet.XCellRangeAddressable xRangeAddr =
(unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
aRangeAddress = xRangeAddr.getRangeAddress();
// --- Sheet operation. --- // uses the range filled with XCellRangeData
unoidl.com.sun.star.sheet.XSheetOperation xSheetOp =
(unoidl.com.sun.star.sheet.XSheetOperation) xData; double fResult = xSheetOp.computeFunction(
unoidl.com.sun.star.sheet.GeneralFunction.AVERAGE );
/** Returns the XCellSeries interface of a cell range. @paramxSheetThespreadsheetcontainingthecellrange. @paramaRangeTheaddressofthecellrange.
@return The XCellSeries interface. */ private unoidl.com.sun.star.sheet.XCellSeries getCellSeries(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
{ return (unoidl.com.sun.star.sheet.XCellSeries)
xSheet.getCellRangeByName( aRange );
}
}
/** This is a helper class for the spreadsheet and table samples. Itconnectstoarunningofficeandcreatesaspreadsheetdocument. Additionallyitcontainsvarioushelperfunctions.
*/ class SpreadsheetDocHelper : System.IDisposable
{
// __ private members ___________________________________________
public SpreadsheetDocHelper()
{ // System.Diagnostics.Debugger.Launch(); // Connect to a running office and get the service manager
mxMSFactory = connect(); // Create a new spreadsheet document
mxDocument = initDocument();
}
/** Returns the service manager.
@return XMultiServiceFactory interface of the service manager. */ public unoidl.com.sun.star.lang.XMultiServiceFactory getServiceManager()
{ return mxMSFactory;
}
/** Returns the whole spreadsheet document.
@return XSpreadsheetDocument interface of the document. */ public unoidl.com.sun.star.sheet.XSpreadsheetDocument getDocument()
{ return mxDocument;
}
/** Returns the spreadsheet with the specified index (0-based). @paramnIndexTheindexofthesheet.
@return XSpreadsheet interface of the sheet. */ public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex )
{ // Collection of sheets
unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
mxDocument.getSheets();
/** Inserts a new empty spreadsheet with the specified name. @paramaNameThenameofthenewsheet. @paramnIndexTheinsertionindex.
@return The XSpreadsheet interface of the new sheet. */ public unoidl.com.sun.star.sheet.XSpreadsheet insertSpreadsheet( String aName, short nIndex )
{ // Collection of sheets
unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
mxDocument.getSheets();
/** Writes a double value into a spreadsheet. @paramxSheetTheXSpreadsheetinterfaceofthespreadsheet. @paramaCellNameTheaddressofthecell(oranamedrange).
@param fValue The value to write into the cell. */ publicvoid setValue(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, double fValue )
{
xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ).setValue( fValue );
}
/** Writes a formula into a spreadsheet. @paramxSheetTheXSpreadsheetinterfaceofthespreadsheet. @paramaCellNameTheaddressofthecell(oranamedrange).
@param aFormula The formula to write into the cell. */ publicvoid setFormula(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, String aFormula )
{
xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ).setFormula( aFormula );
}
/** Writes a date with standard date format into a spreadsheet. @paramxSheetTheXSpreadsheetinterfaceofthespreadsheet. @paramaCellNameTheaddressofthecell(oranamedrange). @paramnDayThedayofthedate. @paramnMonthThemonthofthedate.
@param nYear The year of the date. */ publicvoid setDate(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCellName, int nDay, int nMonth, int nYear )
{ // Set the date value.
unoidl.com.sun.star.table.XCell xCell =
xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ); String aDateStr = nMonth + "/" + nDay + "/" + nYear;
xCell.setFormula( aDateStr );
// Set standard date format.
unoidl.com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier =
(unoidl.com.sun.star.util.XNumberFormatsSupplier) getDocument();
unoidl.com.sun.star.util.XNumberFormatTypes xFormatTypes =
(unoidl.com.sun.star.util.XNumberFormatTypes)
xFormatsSupplier.getNumberFormats(); int nFormat = xFormatTypes.getStandardFormat(
unoidl.com.sun.star.util.NumberFormat.DATE, new unoidl.com.sun.star.lang.Locale() );
/** Draws a colored border around the range and writes the headline inthefirstcell. @paramxSheetTheXSpreadsheetinterfaceofthespreadsheet. @paramaRangeTheaddressofthecellrange(oranamedrange).
@param aHeadline The headline text. */ publicvoid prepareRange(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange, String aHeadline )
{
unoidl.com.sun.star.beans.XPropertySet xPropSet = null;
unoidl.com.sun.star.table.XCellRange xCellRange = null;
// Methods to create cell addresses and range addresses.
/** Creates a unoidl.com.sun.star.table.CellAddress and initializes it withthegivenrange. @paramxSheetTheXSpreadsheetinterfaceofthespreadsheet.
@param aCell The address of the cell (or a named cell). */ public unoidl.com.sun.star.table.CellAddress createCellAddress(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aCell )
{
unoidl.com.sun.star.sheet.XCellAddressable xAddr =
(unoidl.com.sun.star.sheet.XCellAddressable)
xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 ); return xAddr.getCellAddress();
}
/** Creates a unoidl.com.sun.star.table.CellRangeAddress and initializes itwiththegivenrange. @paramxSheetTheXSpreadsheetinterfaceofthespreadsheet.
@param aRange The address of the cell range (or a named range). */ public unoidl.com.sun.star.table.CellRangeAddress createCellRangeAddress(
unoidl.com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
{
unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
(unoidl.com.sun.star.sheet.XCellRangeAddressable)
xSheet.getCellRangeByName( aRange ); return xAddr.getRangeAddress();
}
// Methods to convert cell addresses and range addresses to strings.
/** Returns the text address of the cell. @paramnColumnThecolumnindex. @paramnRowTherowindex.
@return A string containing the cell address. */ publicString getCellAddressString( int nColumn, int nRow )
{ String aStr = ""; if (nColumn > 25)
aStr += (char) ('A' + nColumn / 26 - 1);
aStr += (char) ('A' + nColumn % 26);
aStr += (nRow + 1); return aStr;
}
/** Returns the text address of the cell range. @paramaCellRangeThecellrangeaddress.
@return A string containing the cell range address. */ publicString getCellRangeAddressString(
unoidl.com.sun.star.table.CellRangeAddress aCellRange )
{ return
getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow )
+ ":"
+ getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow );
}
/** Returns the text address of the cell range. @paramxCellRangeTheXSheetCellRangeinterfaceofthecellrange. @parambWithSheettrue=Includesheetname.
@return A string containing the cell range address. */ publicString getCellRangeAddressString(
unoidl.com.sun.star.sheet.XSheetCellRange xCellRange, bool bWithSheet )
{ String aStr = ""; if (bWithSheet)
{
unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
xCellRange.getSpreadsheet();
unoidl.com.sun.star.container.XNamed xNamed =
(unoidl.com.sun.star.container.XNamed) xSheet;
aStr += xNamed.getName() + ".";
}
unoidl.com.sun.star.sheet.XCellRangeAddressable xAddr =
(unoidl.com.sun.star.sheet.XCellRangeAddressable) xCellRange;
aStr += getCellRangeAddressString( xAddr.getRangeAddress() ); return aStr;
}
/** Returns a list of addresses of all cell ranges contained in the collection. @paramxRangesIATheXIndexAccessinterfaceofthecollection.
@return A string containing the cell range address list. */ publicString getCellRangeListString(
unoidl.com.sun.star.container.XIndexAccess xRangesIA )
{ String aStr = ""; int nCount = xRangesIA.getCount(); for (int nIndex = 0; nIndex < nCount; ++nIndex)
{ if (nIndex > 0)
aStr += " ";
uno.Any aRangeObj = xRangesIA.getByIndex( nIndex );
unoidl.com.sun.star.sheet.XSheetCellRange xCellRange =
(unoidl.com.sun.star.sheet.XSheetCellRange) aRangeObj.Value;
aStr += getCellRangeAddressString( xCellRange, false );
} return aStr;
}
/** Connect to a running office that is accepting connections.
@return The ServiceManager to instantiate office components. */ private XMultiServiceFactory connect()
{
publicvoid terminate()
{
XModifiable xMod = (XModifiable) mxDocument; if (xMod != null)
xMod.setModified(false);
XDesktop aDesktop = (XDesktop)
mxMSFactory.createInstance( "com.sun.star.frame.Desktop" ); if (aDesktop != null)
{ try
{
aDesktop.terminate();
} catch (DisposedException d)
{ //This exception may be thrown because shutting down OOo using //XDesktop terminate does not really work. In the case of the //Exception OOo will still terminate.
}
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet am 2026-09-28)
¤
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.