public static void ExportExcelLO(string FileName, DataSet ds) { try { string unoPath = @"C:\Program Files (x86)\LibreOffice 5\program"; Environment.SetEnvironmentVariable("UNO_PATH", unoPath, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + @";" + unoPath, EnvironmentVariableTarget.Process); XComponentContext oStrap = uno.util.Bootstrap.bootstrap(); XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager(); XComponentLoader desktop = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop"); string url = @"private:factory/scalc"; PropertyValue[] loadProps = new PropertyValue[3]; loadProps[0] = new PropertyValue() { Name = "Hidden", Value = new uno.Any(true) }; loadProps[1] = new PropertyValue() { Name = "FilterName", Value = new uno.Any("MS Excel 97") }; loadProps[2] = new PropertyValue() { Name = "ReadOnly", Value = new uno.Any(false) }; XComponent document = desktop.loadComponentFromURL(url, "_blank", 0, loadProps); XSpreadsheets oSheets = ((XSpreadsheetDocument)document).getSheets(); XIndexAccess oSheetsIA = (XIndexAccess)oSheets; XSpreadsheet sheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value; int ii = 0; XCell celija = null; foreach (DataColumn kol in ds.Tables[0].Columns) { celija = sheet.getCellByPosition(ii, 0); ((XText)celija).setString(kol.ColumnName); //((XPropertySet)celija).setPropertyValue("CellBackColor", new uno.Any(654321)); //((XPropertySet)celija).setPropertyValue("CharColor", new uno.Any(333444)); ii++; } ds.Tables[0].AcceptChanges(); ii = 0; foreach (DataRow red in ds.Tables[0].Rows) { int jj = 0; ii++; foreach (object ob in red.ItemArray) { celija = sheet.getCellByPosition(jj, ii); ((XText)celija).setString(ob.ToString()); //((XPropertySet)celija).setPropertyValue("CellBackColor", new uno.Any(888777)); jj++; } } ((XStorable)document).storeToURL("file:///" + FileName.Replace(@"\", "/"), loadProps); System.Diagnostics.Process.Start(FileName); } catch (System.Exception ex) { throw ex; } }
static void Main(string[] args) { InitOpenOfficeEnvironment(); XMultiServiceFactory multiServiceFactory = Connect(); XComponent xComponent = null; string docFileName = @"C:\test3.doc"; try { XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory .createInstance("com.sun.star.frame.Desktop"); //set the property PropertyValue[] propertyValue = new PropertyValue[1]; PropertyValue aProperty = new PropertyValue(); aProperty.Name = "Hidden"; aProperty.Value = new uno.Any(false); propertyValue[0] = aProperty; xComponent = componentLoader .loadComponentFromURL(PathConverter(docFileName), "_blank", 0, propertyValue); XTextDocument xTextDocument = ((XTextDocument)xComponent); XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)xTextDocument.getText(); XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration(); while (xParagraphEnumeration.hasMoreElements()) { uno.Any element = xParagraphEnumeration.nextElement(); XServiceInfo xinfo = (XServiceInfo)element.Value; if (xinfo.supportsService("com.sun.star.text.TextTable")) { Console.WriteLine("Found Table!"); XTextTable xTextTable = (XTextTable)element.Value; String[] cellNames = xTextTable.getCellNames(); for (int i = 0; i < cellNames.Length; i++) { XCell xCell = xTextTable.getCellByName(cellNames[i]); XText xTextCell = (XText)xCell; String strText = xTextCell.getString(); Console.WriteLine(strText); } } else { XTextContent xTextElement = (XTextContent)element.Value; XEnumerationAccess xParaEnumerationAccess = (XEnumerationAccess)xTextElement; // create another enumeration to get all text portions of //the paragraph XEnumeration xTextPortionEnum = xParaEnumerationAccess.createEnumeration(); //step 3 Through the Text portions Enumeration, get interface //to each individual text portion while (xTextPortionEnum.hasMoreElements()) { XTextRange xTextPortion = (XTextRange)xTextPortionEnum.nextElement().Value; Console.Write(xTextPortion.getString()); } } } } catch (unoidl.com.sun.star.uno.Exception exp1) { Console.WriteLine(exp1.Message); Console.WriteLine(exp1.StackTrace); } catch (System.Exception exp2) { Console.WriteLine(exp2.Message); Console.WriteLine(exp2.StackTrace); } finally { xComponent.dispose(); xComponent = null; } Console.WriteLine("Done."); Console.ReadLine(); }