<_Name>Spreadsheet Table Sample
<_Category>C#/OpenOffice Samples
md-project
C#
<_Description>Creates a new C# OpenOffice integration sample project.
${ProjectName}
.
using System;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.beans;
namespace OpenOffice.Samples
{
///
/// Creates a spreadsheet document and provides access to table contents.
///
public class GeneralTableSample : SpreadsheetDocHelper
{
public static void Main (string[] args)
{
try {
//get a spreadsheet handle and run through the samples
using (GeneralTableSample sample = new GeneralTableSample (args)) {
XSpreadsheet sheet = sample.GetSpreadsheet (0);
sample.SetCellValues (sheet);
sample.WriteFormulaWithValidityMessage (sheet);
sample.AlterCellRanges (sheet);
sample.WriteUsingCellCursor (sheet);
sample.ModifyColumnsAndRows (sheet);
sample.InsertChart (sheet);
}
Console.WriteLine ("Samples done.");
} catch (Exception ex) {
Console.WriteLine ("Sample caught exception! " + ex);
}
}
public GeneralTableSample (string[] args) : base (args)
{
}
public void SetCellValues (XSpreadsheet sheet)
{
Console.WriteLine ("Setting cell values.");
// Get cell and set cell value.
XCell cell = sheet.getCellByPosition (0, 0);
cell.setValue (1234);
// Get cell value, modify, and write to another cell
double cellValue = cell.getValue () * 2;
sheet.getCellByPosition (0, 1).setValue (cellValue);
}
public void WriteFormulaWithValidityMessage (XSpreadsheet sheet)
{
Console.WriteLine ("Writing cell formula and validity message.");
// Get cell and set formula string.
XCell cell = sheet.getCellByPosition (0, 2);
cell.setFormula ("=1/0");
//Create a message saying if it's valid
bool valid = (cell.getError () == 0);
string text = string.Format ("The formula {0} is {1}",
cell.getFormula(), valid ? "valid." : "erroneous.");
// Insert the message into a text cell using the XText interface
cell = sheet.getCellByPosition (0, 3);
unoidl.com.sun.star.text.XText cellText = (unoidl.com.sun.star.text.XText) cell;
unoidl.com.sun.star.text.XTextCursor textCursor = cellText.createTextCursor();
cellText.insertString (textCursor, text, false);
// Change cell properties, so that the color depends on the formula's validity
int color = valid ? 0x00FF00 : 0xFF4040;
XPropertySet propSet = (XPropertySet) cell;
propSet.setPropertyValue ("CellBackColor", new uno.Any (color));
}
///
/// Sample for service table.CellRange.
///
public void AlterCellRanges (XSpreadsheet sheet)
{
Console.WriteLine("Altering cell ranges.");
// Accessing a cell range over its position.
XCellRange cellRange = sheet.getCellRangeByPosition (2, 0, 3, 1);
// Change properties of the range.
XPropertySet propSet = (XPropertySet) cellRange;
propSet.setPropertyValue ("CellBackColor", new uno.Any ((int)0x8080FF));
// Accessing a cell range over its name.
cellRange = sheet.getCellRangeByName ("C4:D5");
// Change properties of the range.
propSet = (XPropertySet) cellRange;
propSet.setPropertyValue ("CellBackColor", new uno.Any ((int)0xFFFF80));
}
///
/// Sample for service table.CellCursor
///
public void WriteUsingCellCursor (XSpreadsheet sheet)
{
Console.WriteLine ("Writing cell value using a cell cursor.");
// Create a cursor using the XSpreadsheet method createCursorByRange()
XCellRange cellRange = sheet.getCellRangeByName ("A1");
XSheetCellRange sheetCellRange = (XSheetCellRange) cellRange;
XSheetCellCursor sheetCellCursor = sheet.createCursorByRange (sheetCellRange);
XCellCursor cursor = (XCellCursor) sheetCellCursor;
// Move to the last filled cell.
cursor.gotoEnd ();
// Move one row down.
cursor.gotoOffset (0, 1);
cursor.getCellByPosition (0, 0).setFormula ("Beyond of the last filled cell.");
}
///
/// Sample for services table.TableRows and table.TableColumns
///
public void ModifyColumnsAndRows (XSpreadsheet sheet)
{
Console.WriteLine ("Modifying Columns and Rows");
XColumnRowRange colRowRange = (XColumnRowRange) sheet;
XTableColumns columns = colRowRange.getColumns();
XTableRows rows = colRowRange.getRows();
// Get column C by index (interface XIndexAccess).
uno.Any columnObj = columns.getByIndex (2);
XPropertySet propSet = (XPropertySet) columnObj.Value;
propSet.setPropertyValue ("Width", new uno.Any (5000));
// Get the name of the column.
XNamed xNamed = (XNamed) columnObj.Value;
string text = "The name of this column is " + xNamed.getName() + ".";
sheet.getCellByPosition (2, 2).setFormula (text);
// Get column D by name (interface XNameAccess).
XNameAccess columnsName = (XNameAccess) columns;
columnObj = columnsName.getByName ("D");
propSet = (XPropertySet) columnObj.Value;
propSet.setPropertyValue ("IsVisible", new uno.Any (false));
// Get row 7 by index (interface XIndexAccess)
uno.Any aRowObj = rows.getByIndex (6);
propSet = (XPropertySet) aRowObj.Value;
propSet.setPropertyValue ("Height", new uno.Any (5000));
sheet.getCellByPosition (2, 6).setFormula ("What a big cell.");
// Create a cell series with the values 1 ... 7.
for (int row = 8; row < 15; ++row)
sheet.getCellByPosition (0, row).setValue (row - 7);
// Insert a row between 1 and 2
rows.insertByIndex (9, 1);
// Delete the rows with the values 3 and 4.
rows.removeByIndex (11, 2);
}
///
/// Sample for service table.TableCharts
///
public void InsertChart (XSpreadsheet sheet)
{
Console.WriteLine ("Inserting chart.");
XTableChartsSupplier chartsSupp = (XTableChartsSupplier) sheet;
XTableCharts charts = chartsSupp.getCharts();
// The chart will base on the last cell series, initializing all values.
string name = "newChart";
unoidl.com.sun.star.awt.Rectangle rect = new unoidl.com.sun.star.awt.Rectangle();
rect.X = 10000;
rect.Y = 3000;
rect.Width = rect.Height = 5000;
CellRangeAddress[] ranges = new CellRangeAddress[1];
ranges[0] = CreateCellRangeAddress (sheet, "A9:A14");
// Create the chart.
charts.addNewByName (name, rect, ranges, false, false);
// Get the chart by name.
uno.Any chartObj = charts.getByName (name);
XTableChart chart = (XTableChart) chartObj.Value;
// Query the state of row and column headers.
sheet.getCellByPosition (2, 8).setFormula (
"Chart has column headers: " + (
chart.getHasColumnHeaders () ? "yes" : "no"));
sheet.getCellByPosition (2, 9).setFormula (
"Chart has row headers: " + (
chart.getHasRowHeaders () ? "yes" : "no"));
}
}
}]]>