15
687
169
中流砥柱
using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Text.RegularExpressions; //require references to all "cli_*.dll's files from: //C:/Program Files/OpenOffice 3.0/openofficeorg1.cab/ // set Copy Local = false //set Specific Version = false using unoidl.com.sun.star.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.text; using unoidl.com.sun.star.beans; using unoidl.com.sun.star.sheet; using unoidl.com.sun.star.container; using unoidl.com.sun.star.table; //See original article from there: //http://c-programming.suite101.com/article.cfm/creating_an_openoffice_calc_document_with_c namespace OOfficeExm { public class OOfficeTools { /// /// * Write data into the existing Calc document * /// public static void WriteToExistingCalc() { //file name is with back slashes in the OOffice format only: string fileName = @"file:///C:/test.ods"; //The first step is to use //the bootstrap method to start OpenOffice.org (or to access any existing instances): XComponentContext oStrap = uno.util.Bootstrap.bootstrap(); //The next step is to use OpenOffice.org's service manager to create a desktop: XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager(); XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop"); PropertyValue[] propVals = new PropertyValue[0]; //And then an existing Calc document is added to the desktop: XComponent oDoc = oDesk.loadComponentFromURL(fileName, "_private:stream", 0, propVals);//OK XSpreadsheets oSheets = ((XSpreadsheetDocument)oDoc).getSheets(); XIndexAccess oSheetsIA = (XIndexAccess)oSheets; XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value; for (int i = 0; i /// * Read data from existing Calc document * /// public static void ReadExistingCalcRange() { //file name is with back slashes in the OOffice format only: string fileName = @"file:///C:/test.ods"; string target = "A1:D999"; //The first step is to use //the bootstrap method to start OpenOffice.org (or to access any existing instances): XComponentContext oStrap = uno.util.Bootstrap.bootstrap(); //The next step is to use OpenOffice.org's service manager to create a desktop: XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager(); //Create loader component XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop"); PropertyValue[] propVals = new PropertyValue[0]; //And then an existing Calc document is added to the desktop: XComponent xDoc = oDesk.loadComponentFromURL(fileName, "_private:stream", 0, propVals); // Then use the service manager for current document XMultiServiceFactory xDocFactory = (XMultiServiceFactory)xDoc; // Get document sheets XSpreadsheets oSheets = ((XSpreadsheetDocument)xDoc).getSheets(); // Create indexer XIndexAccess oSheetsIA = (XIndexAccess)oSheets; // Get first sheet XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value; // Get desired range XCellRange oRange = (XCellRange)oSheet.getCellRangeByName(target); // After this line there are few ways for the iteration through cells // see help docs here: //http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/ //I'm pretty sure there is an easier way to loop through cells //but here is my way to retrive rows and columns from address of range: int[] columns = GetColumnsFromString(target); int[] rows = GetRowsFromString(target); // (row and column indexes are starting from zero) int startcol = columns[0] - 1; int endcol = columns[1] - 1; int startrow = rows[0] - 1; int endrow = rows[1] - 1; ArrayList arr = new ArrayList(); for (int r = startrow; r /// * Get first and last column from range address * /// /// /// public static int[] GetColumnsFromString(string address) { int[] columns = new int[2]; string sep = ":"; string[] target = address.Split(sep.ToCharArray()); string head = target[0]; string tail = target[1]; columns[0] = Col_AToI(CutNumeric(head)); columns[1] = Col_AToI(CutNumeric(tail)); return columns; } /// /// * Get first and last row from range address * /// /// /// public static int[] GetRowsFromString(string address) { int[] rows = new int[2]; string sep = ":"; string[] target = address.Split(sep.ToCharArray()); string head = target[0]; string tail = target[1]; rows[0] = int.Parse(CutAlpha(head)); rows[1] = int.Parse(CutAlpha(tail)); return rows;