//inserisce il testo nel bookmark, se va a buon fine ritorna true private bool InsertTxtBookmark(unoidl.com.sun.star.container.XNameAccess ElencoBookmark, string NomeBookmark, string Testo) { if (ElencoBookmark.hasByName(NomeBookmark))//verifica che esista il bookmark { XTextContent xFoundBookmark = (XTextContent)ElencoBookmark.getByName(NomeBookmark).Value; // work with bookmark XTextRange xFound = xFoundBookmark.getAnchor(); xFound.setString(Testo); return(true); } else { return(false); } }
private void CreaReport(object sender, DoWorkEventArgs e) { string PathTemplate = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "template.odt"; if (File.Exists(PathTemplate)) { //The C# application starts OpenOffice.org by using it's bootstrap method: XComponentContext oStrap = uno.util.Bootstrap.bootstrap(); //This will start a new instance of OpenOffice.org if it is not running, //or it will obtain an existing instance if it is already open. //The next step is to create a new OpenOffice.org service manager: XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager(); XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop"); //string url = @"private:factory/swriter"; PropertyValue[] propVals = new PropertyValue[0]; XComponent oDoc = oDesk.loadComponentFromURL(PathConverter(PathTemplate), "_blank", 0, propVals); //string docText = "This will be my first paragraph.\n\r"; //docText += "This will be my second paragraph.\n\r"; //And then this is written to the body of the document: //((XTextDocument)oDoc).getText().setString(docText); XBookmarksSupplier xBS = (XBookmarksSupplier)oDoc; unoidl.com.sun.star.container.XNameAccess xNamedBookmarks = xBS.getBookmarks(); //inserimento dati nei bookmark //grassi InsertTxtBookmark(xNamedBookmarks, "bPalma", oPalmaRaf.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmaAfr", oPalmaAfr.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmaOle60", oPalmaOle60.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmaOle62", oPalmaOle62.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmaOle64", oPalmaOle64.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmaSte48", oPalmaSt48.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmaSte53", oPalmaSt53.ToString()); InsertTxtBookmark(xNamedBookmarks, "bCocco", oCoccoRaf.ToString()); InsertTxtBookmark(xNamedBookmarks, "bCoccoIdr", oCoccoIdr.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmisto", oPalmistoR.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmistoIdr", oPalmistoI.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmistoSte", oPalmistoSt.ToString()); InsertTxtBookmark(xNamedBookmarks, "bPalmistoFra", oPalmistoFraz.ToString()); InsertTxtBookmark(xNamedBookmarks, "bSoia", oSoiaRaf.ToString()); InsertTxtBookmark(xNamedBookmarks, "bColza", oColzaRaf.ToString()); InsertTxtBookmark(xNamedBookmarks, "bArachide", oArachideR.ToString()); InsertTxtBookmark(xNamedBookmarks, "bVinacciolo", oVinacciolo.ToString()); InsertTxtBookmark(xNamedBookmarks, "bMais", oMaisRaf.ToString()); InsertTxtBookmark(xNamedBookmarks, "bGirasoleAO", oGirasoleALino.ToString()); InsertTxtBookmark(xNamedBookmarks, "bGirasoleAL", oGirasoleAOle.ToString()); InsertTxtBookmark(xNamedBookmarks, "bSesamo", oSesamoRaff.ToString()); InsertTxtBookmark(xNamedBookmarks, "bNocciola", oNocciola.ToString()); InsertTxtBookmark(xNamedBookmarks, "bOliva", oOliva.ToString()); InsertTxtBookmark(xNamedBookmarks, "bBurroCacao", oBurroCacao.ToString()); InsertTxtBookmark(xNamedBookmarks, "bBabassu", oBabassu.ToString()); InsertTxtBookmark(xNamedBookmarks, "bKarite", oKarite.ToString()); InsertTxtBookmark(xNamedBookmarks, "bBurro", oBurro.ToString()); InsertTxtBookmark(xNamedBookmarks, "bStrutto", oStrutto.ToString()); InsertTxtBookmark(xNamedBookmarks, "bSego", oSegoRaf.ToString()); InsertTxtBookmark(xNamedBookmarks, "bX", oX.ToString()); InsertTxtBookmark(xNamedBookmarks, "bY", oY.ToString()); InsertTxtBookmark(xNamedBookmarks, "bZ", oZ.ToString()); //acidi grassi calcolati InsertTxtBookmark(xNamedBookmarks, "bC4-0", AcidiGrassiOtt[0].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC6-0", AcidiGrassiOtt[1].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC8-0", AcidiGrassiOtt[2].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC10-0", AcidiGrassiOtt[3].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC12-0", AcidiGrassiOtt[4].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC14-0", AcidiGrassiOtt[5].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC14-1", AcidiGrassiOtt[6].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC15-0", AcidiGrassiOtt[7].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC16-ISO", AcidiGrassiOtt[8].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC16-0", AcidiGrassiOtt[9].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC16-1", AcidiGrassiOtt[10].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC17-ISO", AcidiGrassiOtt[11].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC17-0", AcidiGrassiOtt[12].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC17-1", AcidiGrassiOtt[13].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC18-0", AcidiGrassiOtt[14].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC18-ISO", AcidiGrassiOtt[15].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC18-1", AcidiGrassiOtt[16].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC18-2", AcidiGrassiOtt[17].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC18-3", AcidiGrassiOtt[18].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC18-CON", AcidiGrassiOtt[19].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC20-0", AcidiGrassiOtt[20].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC20-1", AcidiGrassiOtt[21].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC22-0", AcidiGrassiOtt[22].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bC22-1", AcidiGrassiOtt[23].ToString("F2")); //acidi grassi inseriti a mano (forniti dal gascromatografo) InsertTxtBookmark(xNamedBookmarks, "bGC4-0", AcidiGrassiRic[0].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC6-0", AcidiGrassiRic[1].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC8-0", AcidiGrassiRic[2].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC10-0", AcidiGrassiRic[3].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC12-0", AcidiGrassiRic[4].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC14-0", AcidiGrassiRic[5].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC14-1", AcidiGrassiRic[6].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC15-0", AcidiGrassiRic[7].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC16-ISO", AcidiGrassiRic[8].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC16-0", AcidiGrassiRic[9].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC16-1", AcidiGrassiRic[10].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC17-ISO", AcidiGrassiRic[11].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC17-0", AcidiGrassiRic[12].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC17-1", AcidiGrassiRic[13].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC18-0", AcidiGrassiRic[14].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC18-ISO", AcidiGrassiRic[15].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC18-1", AcidiGrassiRic[16].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC18-2", AcidiGrassiRic[17].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC18-3", AcidiGrassiRic[18].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC18-CON", AcidiGrassiRic[19].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC20-0", AcidiGrassiRic[20].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC20-1", AcidiGrassiRic[21].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC22-0", AcidiGrassiRic[22].ToString("F2")); InsertTxtBookmark(xNamedBookmarks, "bGC22-1", AcidiGrassiRic[23].ToString("F2")); //numero di iodio InsertTxtBookmark(xNamedBookmarks, "bnIodio", oNiodio.ToString("F2")); //And then the file is saved to disk: //((XStorable)oDoc).storeAsURL(PathConverter(@"C:\test.odt"), propVals); //((XStorable)oDoc).storeAsURL(PathConverter(SaveTo), propVals); PropertyValue[] propValsOut = new PropertyValue[1]; propValsOut[0] = new PropertyValue(); propValsOut[0].Name = "FilterName"; switch (Estensione) { case ".odt": break; case ".doc": propValsOut[0].Value = new uno.Any("MS Word 97"); break; case ".pdf": propValsOut[0].Value = new uno.Any("writer_pdf_Export"); break; default: break; } ((XStorable)oDoc).storeToURL(PathConverter(SaveTo), propValsOut); //chiude ((XComponent)oDoc).dispose(); //And then any memory that's been used can be freed up: oDoc = null; } else { MessageBox.Show("Template non trovato:\n" + PathTemplate); } }
// ________________________________________________________________ /// This sample function modifies cells and cell ranges. public void doSampleFunction() { // for common usage unoidl.com.sun.star.sheet.XSpreadsheet xSheet = getSpreadsheet(0); unoidl.com.sun.star.beans.XPropertySet xPropSet = null; unoidl.com.sun.star.table.XCell xCell = null; unoidl.com.sun.star.table.XCellRange xCellRange = null; // *** Access and modify a VALUE CELL *** Console.WriteLine("*** Sample for service table.Cell ***"); xCell = xSheet.getCellByPosition(0, 0); // Set cell value. xCell.setValue(1234); // Get cell value. double nDblValue = xCell.getValue() * 2; xSheet.getCellByPosition(0, 1).setValue(nDblValue); // *** Create a FORMULA CELL and query error type *** xCell = xSheet.getCellByPosition(0, 2); // Set formula string. xCell.setFormula("=1/0"); // Get error type. bool bValid = (xCell.getError() == 0); // Get formula string. String aText = "The formula " + xCell.getFormula() + " is "; aText += bValid ? "valid." : "erroneous."; // *** Insert a TEXT CELL using the XText interface *** xCell = xSheet.getCellByPosition(0, 3); unoidl.com.sun.star.text.XText xCellText = (unoidl.com.sun.star.text.XText)xCell; unoidl.com.sun.star.text.XTextCursor xTextCursor = xCellText.createTextCursor(); xCellText.insertString(xTextCursor, aText, false); // *** Change cell properties *** int nValue = bValid ? 0x00FF00 : 0xFF4040; xPropSet = (unoidl.com.sun.star.beans.XPropertySet)xCell; xPropSet.setPropertyValue( "CellBackColor", new uno.Any((Int32)nValue)); // *** Accessing a CELL RANGE *** Console.WriteLine("*** Sample for service table.CellRange ***"); // Accessing a cell range over its position. xCellRange = xSheet.getCellRangeByPosition(2, 0, 3, 1); // Change properties of the range. xPropSet = (unoidl.com.sun.star.beans.XPropertySet)xCellRange; xPropSet.setPropertyValue( "CellBackColor", new uno.Any((Int32)0x8080FF)); // Accessing a cell range over its name. xCellRange = xSheet.getCellRangeByName("C4:D5"); // Change properties of the range. xPropSet = (unoidl.com.sun.star.beans.XPropertySet)xCellRange; xPropSet.setPropertyValue( "CellBackColor", new uno.Any((Int32)0xFFFF80)); // *** Using the CELL CURSOR to add some data below of // the filled area *** Console.WriteLine("*** Sample for service table.CellCursor ***"); // Create a cursor using the XSpreadsheet method createCursorByRange() xCellRange = xSheet.getCellRangeByName("A1"); unoidl.com.sun.star.sheet.XSheetCellRange xSheetCellRange = (unoidl.com.sun.star.sheet.XSheetCellRange)xCellRange; unoidl.com.sun.star.sheet.XSheetCellCursor xSheetCellCursor = xSheet.createCursorByRange(xSheetCellRange); unoidl.com.sun.star.table.XCellCursor xCursor = (unoidl.com.sun.star.table.XCellCursor)xSheetCellCursor; // Move to the last filled cell. xCursor.gotoEnd(); // Move one row down. xCursor.gotoOffset(0, 1); xCursor.getCellByPosition(0, 0).setFormula( "Beyond of the last filled cell."); // *** Modifying COLUMNS and ROWS *** Console.WriteLine("*** Sample for services table.TableRows and " + "table.TableColumns ***"); unoidl.com.sun.star.table.XColumnRowRange xCRRange = (unoidl.com.sun.star.table.XColumnRowRange)xSheet; unoidl.com.sun.star.table.XTableColumns xColumns = xCRRange.getColumns(); unoidl.com.sun.star.table.XTableRows xRows = xCRRange.getRows(); // Get column C by index (interface XIndexAccess). uno.Any aColumnObj = xColumns.getByIndex(2); xPropSet = (unoidl.com.sun.star.beans.XPropertySet)aColumnObj.Value; xPropSet.setPropertyValue("Width", new uno.Any((Int32)5000)); // Get the name of the column. unoidl.com.sun.star.container.XNamed xNamed = (unoidl.com.sun.star.container.XNamed)aColumnObj.Value; aText = "The name of this column is " + xNamed.getName() + "."; xSheet.getCellByPosition(2, 2).setFormula(aText); // Get column D by name (interface XNameAccess). unoidl.com.sun.star.container.XNameAccess xColumnsName = (unoidl.com.sun.star.container.XNameAccess)xColumns; aColumnObj = xColumnsName.getByName("D"); xPropSet = (unoidl.com.sun.star.beans.XPropertySet)aColumnObj.Value; xPropSet.setPropertyValue( "IsVisible", new uno.Any((Boolean)false)); // Get row 7 by index (interface XIndexAccess) uno.Any aRowObj = xRows.getByIndex(6); xPropSet = (unoidl.com.sun.star.beans.XPropertySet)aRowObj.Value; xPropSet.setPropertyValue("Height", new uno.Any((Int32)5000)); xSheet.getCellByPosition(2, 6).setFormula("What a big cell."); // Create a cell series with the values 1 ... 7. for (int nRow = 8; nRow < 15; ++nRow) { xSheet.getCellByPosition(0, nRow).setValue(nRow - 7); } // Insert a row between 1 and 2 xRows.insertByIndex(9, 1); // Delete the rows with the values 3 and 4. xRows.removeByIndex(11, 2); // *** Inserting CHARTS *** Console.WriteLine("*** Sample for service table.TableCharts ***"); unoidl.com.sun.star.table.XTableChartsSupplier xChartsSupp = (unoidl.com.sun.star.table.XTableChartsSupplier)xSheet; unoidl.com.sun.star.table.XTableCharts xCharts = xChartsSupp.getCharts(); // The chart will base on the last cell series, initializing all values. String aName = "newChart"; unoidl.com.sun.star.awt.Rectangle aRect = new unoidl.com.sun.star.awt.Rectangle(); aRect.X = 10000; aRect.Y = 3000; aRect.Width = aRect.Height = 5000; unoidl.com.sun.star.table.CellRangeAddress[] aRanges = new unoidl.com.sun.star.table.CellRangeAddress[1]; aRanges[0] = createCellRangeAddress(xSheet, "A9:A14"); // Create the chart. xCharts.addNewByName(aName, aRect, aRanges, false, false); // Get the chart by name. uno.Any aChartObj = xCharts.getByName(aName); unoidl.com.sun.star.table.XTableChart xChart = (unoidl.com.sun.star.table.XTableChart)aChartObj.Value; // Query the state of row and column headers. aText = "Chart has column headers: "; aText += xChart.getHasColumnHeaders() ? "yes" : "no"; xSheet.getCellByPosition(2, 8).setFormula(aText); aText = "Chart has row headers: "; aText += xChart.getHasRowHeaders() ? "yes" : "no"; xSheet.getCellByPosition(2, 9).setFormula(aText); }