<_Name>SpreadsheetDocHelper
md-text-file-icon
<_Category>OpenOffice Samples
C#
<_Description>Spreadsheet helper class for the OpenOffice samples.
.
using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.util;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.beans;
namespace OpenOffice.Samples
{
///
/// This is a helper class for the spreadsheet and table samples.
/// It connects to a running office and creates a spreadsheet document.
/// Additionally it contains various helper functions.
///
public class SpreadsheetDocHelper : IDisposable
{
XComponentContext componentContext;
XMultiServiceFactory multiServiceFactory;
XSpreadsheetDocument document;
public SpreadsheetDocHelper (string[] args)
{
// Connect to a running office and get the service manager
multiServiceFactory = Connect (args);
// Create a new spreadsheet document
document = InitDocument ();
}
#region Helper methods
/// Returns the service manager.
/// The interface of the service manager.
public XMultiServiceFactory ServiceManager {
get { return multiServiceFactory; }
}
/// Returns the whole spreadsheet document.
/// The interface of the document.
public XSpreadsheetDocument Document {
get { return document; }
}
/// Returns the spreadsheet with the specified index.
/// The index of the sheet (0-based).
/// The interface of the sheet.
public XSpreadsheet GetSpreadsheet (int index)
{
XSpreadsheets sheets = document.getSheets ();
XIndexAccess sheetsIA = (XIndexAccess) sheets;
return (XSpreadsheet) sheetsIA.getByIndex (index).Value;
}
/// Inserts a new empty spreadsheet with the specified name.
/// The name of the new sheet.
/// The insertion index.
/// The interface of the new sheet.
public XSpreadsheet InsertSpreadsheet (string name, short index)
{
XSpreadsheets sheets = document.getSheets ();
sheets.insertNewByName (name, index);
return (XSpreadsheet) sheets.getByName (name).Value;
}
#endregion
#region Methods to fill values into cells.
/// Writes a double value into a spreadsheet.
/// The interface of the spreadsheet.
/// The address of the cell (or a named range).
/// The value to write into the cell.
public void SetCellValue (XSpreadsheet sheet, string cellName, double cellValue)
{
sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setValue (cellValue);
}
/// Writes a formula into a spreadsheet.
/// The interface of the spreadsheet.
/// The address of the cell (or a named range).
/// The formula to write into the cell.
public void SetCellFormula (XSpreadsheet sheet, string cellName, string formula)
{
sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setFormula (formula);
}
/// Writes a date with standard date format into a spreadsheet.
/// The interface of the spreadsheet.
/// The address of the cell (or a named range).
/// The day of the date.
/// The month of the date.
/// The year of the date.
public void SetCellDate (XSpreadsheet sheet, string cellName, int day, int month, int year)
{
// Set the date value.
XCell cell = sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
cell.setFormula (month + "/" + day + "/" + year);
// Set standard date format.
XNumberFormatsSupplier formatsSupplier = (XNumberFormatsSupplier) Document;
XNumberFormatTypes formatTypes = (XNumberFormatTypes) formatsSupplier.getNumberFormats ();
int numberFormat = formatTypes.getStandardFormat (NumberFormat.DATE, new Locale ());
XPropertySet propSet = (unoidl.com.sun.star.beans.XPropertySet) cell;
propSet.setPropertyValue ("NumberFormat", new uno.Any (numberFormat));
}
/// Draws a colored border around the range and writes the headline in the first cell.
/// The interface of the spreadsheet.
/// The address of the cell range (or a named range).
/// The headline text.
public void PrepareRange (XSpreadsheet sheet, string range, string headline)
{
XPropertySet propSet = null;
XCellRange cellRange = null;
// draw border
cellRange = sheet.getCellRangeByName (range);
propSet = (XPropertySet) cellRange;
BorderLine aLine = new BorderLine ();
aLine.Color = 0x99CCFF;
aLine.InnerLineWidth = aLine.LineDistance = 0;
aLine.OuterLineWidth = 100;
TableBorder border = new TableBorder ();
border.TopLine = border.BottomLine = border.LeftLine = border.RightLine = aLine;
border.IsTopLineValid = border.IsBottomLineValid = true;
border.IsLeftLineValid = border.IsRightLineValid = true;
propSet.setPropertyValue ("TableBorder", new uno.Any (typeof (TableBorder), border));
// draw headline
XCellRangeAddressable xAddr = (XCellRangeAddressable) cellRange;
CellRangeAddress aAddr = xAddr.getRangeAddress ();
cellRange = sheet.getCellRangeByPosition (
aAddr.StartColumn,
aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow);
propSet = (XPropertySet) cellRange;
propSet.setPropertyValue ("CellBackColor", new uno.Any ((int) 0x99CCFF));
// write headline
XCell cell = cellRange.getCellByPosition (0, 0);
cell.setFormula (headline);
propSet = (XPropertySet) cell;
propSet.setPropertyValue ("CharColor", new uno.Any ((int) 0x003399));
propSet.setPropertyValue ("CharWeight",
new uno.Any ((Single) unoidl.com.sun.star.awt.FontWeight.BOLD));
}
#endregion
#region Methods to create cell addresses and range addresses.
/// Creates a CellAddress and initializes it with the given range
/// The interface of the spreadsheet.
/// The address of the cell (or a named cell).
public CellAddress CreateCellAddress (XSpreadsheet sheet, string cellName)
{
XCellAddressable addr = (XCellAddressable) sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
return addr.getCellAddress ();
}
/// Creates a CellRangeAddress and initializes it with the given range.
/// The interface of the spreadsheet.
/// The address of the cell range (or a named range).
public CellRangeAddress CreateCellRangeAddress (XSpreadsheet sheet, string range)
{
XCellRangeAddressable addr = (XCellRangeAddressable) sheet.getCellRangeByName (range);
return addr.getRangeAddress ();
}
#endregion
#region Methods to convert cell addresses and range addresses to strings.
/// Returns the text address of the cell.
/// The column index.
/// The row index.
/// A string containing the cell address.
public string GetCellAddressString (int column, int row)
{
return ((column > 25)? new string ((char) ('A' + column / 26 - 1), 1) : string.Empty)
+ new string ((char) ('A' + column % 26), 1)
+ (row + 1);
}
/// Returns the text address of the cell range.
/// The cell range address.
/// A string containing the cell range address.
public string GetCellRangeAddressString (CellRangeAddress cellRange)
{
return GetCellAddressString (cellRange.StartColumn, cellRange.StartRow)
+ ":"
+ GetCellAddressString (cellRange.EndColumn, cellRange.EndRow);
}
/// Returns the text address of the cell range.
/// The XSheetCellRange interface of the cell range.
/// Whether to include the sheet name.
/// A string containing the cell range address.
public string GetCellRangeAddressString (XSheetCellRange cellRange, bool withSheetName)
{
XCellRangeAddressable addr = (XCellRangeAddressable) cellRange;
string str = GetCellRangeAddressString (addr.getRangeAddress ());
if (withSheetName) {
XSpreadsheet sheet = cellRange.getSpreadsheet ();
XNamed xNamed = (XNamed) sheet;
return xNamed.getName () + "." + str;
} else {
return str;
}
}
/// Returns a list of addresses of all cell ranges contained in the collection.
/// The XIndexAccess interface of the collection.
/// A string containing the cell range address list.
public string GetCellRangeListString (XIndexAccess rangesIA)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
int count = rangesIA.getCount ();
for (int i = 0; i < count; ++i)
{
if (i > 0)
sb.Append (" ");
uno.Any rangeObj = rangesIA.getByIndex (i);
XSheetCellRange cellRange =
(XSheetCellRange) rangeObj.Value;
sb.Append (GetCellRangeAddressString (cellRange, false));
}
return sb.ToString ();
}
#endregion
/// Connect to a running office that is accepting connections.
/// The ServiceManager to instantiate office components.
XMultiServiceFactory Connect (string [] args)
{
componentContext = uno.util.Bootstrap.bootstrap ();
return (XMultiServiceFactory) componentContext.getServiceManager ();
}
public void Dispose ()
{
}
/// Creates an empty spreadsheet document.
/// The interface of the document.
XSpreadsheetDocument InitDocument ()
{
XComponentLoader loader
= (XComponentLoader) multiServiceFactory.createInstance ("com.sun.star.frame.Desktop");
return (XSpreadsheetDocument) loader.loadComponentFromURL
("private:factory/scalc", "_blank", 0, new PropertyValue[0]);
}
}
}]]>