//ファイルを開く public void OpenFile(string filePath) { //parameterファイルを開く string path = filePath; // pathをURIに変換 Uri.TryCreate(path, UriKind.Absolute, out uriCalcFile); // OpenOfficeファイルを開くためのおまじない // コンポーネントコンテキストの取得 localContext = Bootstrap.bootstrap(); // サービスマネージャーの取得 multiServiceFactory = (XMultiServiceFactory)localContext.getServiceManager(); // コンポーネントローダーの取得 loader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop"); // 非表示で実行するためのプロパティ指定 calcProp = new PropertyValue[1]; calcProp[0] = new PropertyValue(); calcProp[0].Name = "Hidden"; calcProp[0].Value = new uno.Any(true); // Calcファイルを開く doc = (XSpreadsheetDocument)loader.loadComponentFromURL(uriCalcFile.ToString(), "_blank", 0, calcProp); // シート群を取得 sheets = doc.getSheets(); }
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; } }