2 <Template Originator = "Michael Hutchinson"
5 LastModified = "2008/05/30">
7 <TemplateConfiguration>
8 <_Name>SpreadsheetDocHelper</_Name>
9 <Icon>md-text-file-icon</Icon>
10 <_Category>OpenOffice Samples</_Category>
11 <LanguageName>C#</LanguageName>
12 <_Description>Spreadsheet helper class for the OpenOffice samples.</_Description>
13 </TemplateConfiguration>
16 <ParentProject PermittedCreationPaths="" ExcludedFiles="/SpreadsheetDocHelper.cs" />
20 <File Name="SpreadsheetDocHelper.cs" AddStandardHeader="False"><![CDATA[//
21 // This library is free software; you can redistribute it and/or
22 // modify it under the terms of the GNU Lesser General Public
23 // License as published by the Free Software Foundation; either
24 // version 2.1 of the License, or (at your option) any later version.
26 // This library is distributed in the hope that it will be useful,
27 // but WITHOUT ANY WARRANTY; without even the implied warranty of
28 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 // Lesser General Public License for more details.
31 // You should have received a copy of the GNU Lesser General Public
32 // License along with this library. If not, see <http://www.gnu.org/licenses/>.
36 using unoidl.com.sun.star.lang;
37 using unoidl.com.sun.star.uno;
38 using unoidl.com.sun.star.bridge;
39 using unoidl.com.sun.star.frame;
40 using unoidl.com.sun.star.sheet;
41 using unoidl.com.sun.star.container;
42 using unoidl.com.sun.star.util;
43 using unoidl.com.sun.star.table;
44 using unoidl.com.sun.star.beans;
46 namespace OpenOffice.Samples
50 /// This is a helper class for the spreadsheet and table samples.
51 /// It connects to a running office and creates a spreadsheet document.
52 /// Additionally it contains various helper functions.
54 public class SpreadsheetDocHelper : IDisposable
56 XComponentContext componentContext;
57 XMultiServiceFactory multiServiceFactory;
58 XSpreadsheetDocument document;
60 public SpreadsheetDocHelper (string[] args)
62 // Connect to a running office and get the service manager
63 multiServiceFactory = Connect (args);
64 // Create a new spreadsheet document
65 document = InitDocument ();
68 #region Helper methods
70 /// <summary>Returns the service manager.</summary>
71 /// <returns> The <see cref="XMultiServiceFactory"/> interface of the service manager.</returns>
72 public XMultiServiceFactory ServiceManager {
73 get { return multiServiceFactory; }
76 /// <summary>Returns the whole spreadsheet document.</summary>
77 /// <returns> The <see cref="XSpreadsheetDocument"/> interface of the document.</returns>
78 public XSpreadsheetDocument Document {
79 get { return document; }
82 /// <summary> Returns the spreadsheet with the specified index. </summary>
83 /// <param name="index">The index of the sheet (0-based). </param>
84 /// <returns> The <see cref="XSpreadsheet"/> interface of the sheet. </returns>
85 public XSpreadsheet GetSpreadsheet (int index)
87 XSpreadsheets sheets = document.getSheets ();
88 XIndexAccess sheetsIA = (XIndexAccess) sheets;
89 return (XSpreadsheet) sheetsIA.getByIndex (index).Value;
92 /// <summary> Inserts a new empty spreadsheet with the specified name. </summary>
93 /// <param name="name"> The name of the new sheet. </param>
94 /// <param name="index"> The insertion index. </param>
95 /// <returns> The <see cref="XSpreadsheet"/> interface of the new sheet. </returns>
96 public XSpreadsheet InsertSpreadsheet (string name, short index)
98 XSpreadsheets sheets = document.getSheets ();
99 sheets.insertNewByName (name, index);
100 return (XSpreadsheet) sheets.getByName (name).Value;
105 #region Methods to fill values into cells.
107 /// <summary> Writes a double value into a spreadsheet. </summary>
108 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
109 /// <param name="cellName"> The address of the cell (or a named range). </param>
110 /// <param name="cellValue"> The value to write into the cell.</param>
111 public void SetCellValue (XSpreadsheet sheet, string cellName, double cellValue)
113 sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setValue (cellValue);
116 /// <summary> Writes a formula into a spreadsheet. </summary>
117 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
118 /// <param name="cellName"> The address of the cell (or a named range). </param>
119 /// <param name="formula"> The formula to write into the cell. </param>
120 public void SetCellFormula (XSpreadsheet sheet, string cellName, string formula)
122 sheet.getCellRangeByName (cellName).getCellByPosition (0, 0).setFormula (formula);
125 /// <summary> Writes a date with standard date format into a spreadsheet. </summary>
126 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
127 /// <param name="cellName"> The address of the cell (or a named range). </param>
128 /// <param name="day"> The day of the date. </param>
129 /// <param name="month"> The month of the date. </param>
130 /// <param name="year">The year of the date. </param>
131 public void SetCellDate (XSpreadsheet sheet, string cellName, int day, int month, int year)
133 // Set the date value.
134 XCell cell = sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
135 cell.setFormula (month + "/" + day + "/" + year);
137 // Set standard date format.
138 XNumberFormatsSupplier formatsSupplier = (XNumberFormatsSupplier) Document;
139 XNumberFormatTypes formatTypes = (XNumberFormatTypes) formatsSupplier.getNumberFormats ();
140 int numberFormat = formatTypes.getStandardFormat (NumberFormat.DATE, new Locale ());
141 XPropertySet propSet = (unoidl.com.sun.star.beans.XPropertySet) cell;
142 propSet.setPropertyValue ("NumberFormat", new uno.Any (numberFormat));
145 /// <summary>Draws a colored border around the range and writes the headline in the first cell. </summary>
146 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
147 /// <param name="range"> The address of the cell range (or a named range). </param>
148 /// <param name="headline"> The headline text. </param>
149 public void PrepareRange (XSpreadsheet sheet, string range, string headline)
151 XPropertySet propSet = null;
152 XCellRange cellRange = null;
155 cellRange = sheet.getCellRangeByName (range);
156 propSet = (XPropertySet) cellRange;
157 BorderLine aLine = new BorderLine ();
158 aLine.Color = 0x99CCFF;
159 aLine.InnerLineWidth = aLine.LineDistance = 0;
160 aLine.OuterLineWidth = 100;
161 TableBorder border = new TableBorder ();
162 border.TopLine = border.BottomLine = border.LeftLine = border.RightLine = aLine;
163 border.IsTopLineValid = border.IsBottomLineValid = true;
164 border.IsLeftLineValid = border.IsRightLineValid = true;
165 propSet.setPropertyValue ("TableBorder", new uno.Any (typeof (TableBorder), border));
168 XCellRangeAddressable xAddr = (XCellRangeAddressable) cellRange;
169 CellRangeAddress aAddr = xAddr.getRangeAddress ();
171 cellRange = sheet.getCellRangeByPosition (
173 aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow);
175 propSet = (XPropertySet) cellRange;
176 propSet.setPropertyValue ("CellBackColor", new uno.Any ((int) 0x99CCFF));
179 XCell cell = cellRange.getCellByPosition (0, 0);
180 cell.setFormula (headline);
181 propSet = (XPropertySet) cell;
182 propSet.setPropertyValue ("CharColor", new uno.Any ((int) 0x003399));
183 propSet.setPropertyValue ("CharWeight",
184 new uno.Any ((Single) unoidl.com.sun.star.awt.FontWeight.BOLD));
189 #region Methods to create cell addresses and range addresses.
191 /// <summary> Creates a CellAddress and initializes it with the given range </summary>
192 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
193 /// <param name="cellName"> The address of the cell (or a named cell). </param>
194 public CellAddress CreateCellAddress (XSpreadsheet sheet, string cellName)
196 XCellAddressable addr = (XCellAddressable) sheet.getCellRangeByName (cellName).getCellByPosition (0, 0);
197 return addr.getCellAddress ();
200 /// <summary> Creates a CellRangeAddress and initializes it with the given range. </summary>
201 /// <param name="sheet"> The <see cref="XSpreadsheet"/> interface of the spreadsheet. </param>
202 /// <param name="range"> The address of the cell range (or a named range). </param>
203 public CellRangeAddress CreateCellRangeAddress (XSpreadsheet sheet, string range)
205 XCellRangeAddressable addr = (XCellRangeAddressable) sheet.getCellRangeByName (range);
206 return addr.getRangeAddress ();
211 #region Methods to convert cell addresses and range addresses to strings.
213 /// <summary> Returns the text address of the cell. </summary>
214 /// <param name="column"> The column index. </param>
215 /// <param name="row"> The row index. </param>
216 /// <returns> A string containing the cell address. </returns>
217 public string GetCellAddressString (int column, int row)
219 return ((column > 25)? new string ((char) ('A' + column / 26 - 1), 1) : string.Empty)
220 + new string ((char) ('A' + column % 26), 1)
224 /// <summary> Returns the text address of the cell range. </summary>
225 /// <param name="cellRange"> The cell range address. </param>
226 /// <returns> A string containing the cell range address. </returns>
227 public string GetCellRangeAddressString (CellRangeAddress cellRange)
229 return GetCellAddressString (cellRange.StartColumn, cellRange.StartRow)
231 + GetCellAddressString (cellRange.EndColumn, cellRange.EndRow);
234 /// <summary> Returns the text address of the cell range. </summary>
235 /// <param name="cellRange"> The XSheetCellRange interface of the cell range. </param>
236 /// <param name="withSheetName"> Whether to include the sheet name. </param>
237 /// <returns> A string containing the cell range address. </returns>
238 public string GetCellRangeAddressString (XSheetCellRange cellRange, bool withSheetName)
240 XCellRangeAddressable addr = (XCellRangeAddressable) cellRange;
241 string str = GetCellRangeAddressString (addr.getRangeAddress ());
244 XSpreadsheet sheet = cellRange.getSpreadsheet ();
245 XNamed xNamed = (XNamed) sheet;
246 return xNamed.getName () + "." + str;
253 /// <summary>Returns a list of addresses of all cell ranges contained in the collection. </summary>
254 /// <param name="rangesIA"> The <see cref="XIndexAccess"/> XIndexAccess interface of the collection. </param>
255 /// <returns> A string containing the cell range address list. </returns>
256 public string GetCellRangeListString (XIndexAccess rangesIA)
258 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
259 int count = rangesIA.getCount ();
260 for (int i = 0; i < count; ++i)
264 uno.Any rangeObj = rangesIA.getByIndex (i);
265 XSheetCellRange cellRange =
266 (XSheetCellRange) rangeObj.Value;
267 sb.Append (GetCellRangeAddressString (cellRange, false));
269 return sb.ToString ();
274 /// <summary> Connect to a running office that is accepting connections. </summary>
275 /// <returns> The ServiceManager to instantiate office components. </returns>
276 XMultiServiceFactory Connect (string [] args)
278 componentContext = uno.util.Bootstrap.bootstrap ();
279 return (XMultiServiceFactory) componentContext.getServiceManager ();
282 public void Dispose ()
286 /// <summary> Creates an empty spreadsheet document. </summary>
287 /// <returns>The <see cref="XSpreadsheetDocument"/> interface of the document. </returns>
288 XSpreadsheetDocument InitDocument ()
290 XComponentLoader loader
291 = (XComponentLoader) multiServiceFactory.createInstance ("com.sun.star.frame.Desktop");
292 return (XSpreadsheetDocument) loader.loadComponentFromURL
293 ("private:factory/scalc", "_blank", 0, new PropertyValue[0]);