2017-08-24 9 views
0

J'ai créé une table en utilisant open office api dans java. Maintenant, je veux formater le contenu de la cellule du tableau comme lui donner la couleur de fond, changer la police, la taille, l'alignement. Mon code estFormatage de la table OpenOffice java

XMultiServiceFactory xMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xdocument); 

     // Creating a table with 13 rows and 2 columns 
     XTextTable xTextTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xMSF.createInstance("com.sun.star.text.TextTable")); 
     xTextTable.initialize(2, 2); // rows, cols 

     // insert table in the xText 

     xText.insertTextContent(xText.getEnd(), xTextTable, false); 
     XPropertySet xPS1 = (XPropertySet) UnoRuntime.queryInterface(
     XPropertySet.class, xTextTable); 

     // Get table Width and TableColumnRelativeSum properties values 
     int iWidth = (Integer) xPS1.getPropertyValue("Width"); 
     short sTableColumnRelativeSum = (Short) xPS1.getPropertyValue("TableColumnRelativeSum"); 

     // Calculate conversion ration 
     double dRatio = (double) sTableColumnRelativeSum/(double) iWidth; 

     // Convert our 20 mm (2000) to unknown (relative) units 
     double dRelativeWidth = (double) 25000 * dRatio; 

     // Get table column separators 
     Object xObj = xPS1.getPropertyValue("TableColumnSeparators"); 

     TableColumnSeparator[] xSeparators = (TableColumnSeparator[])UnoRuntime.queryInterface(
     TableColumnSeparator[].class, xObj); 

     // Last table column separator position 
     double dPosition = sTableColumnRelativeSum - dRelativeWidth; 

     // Set set new position for all column separators   
     for (int i = xSeparators.length - 1 ; i >= 0 ; i--) 
     { 
     xSeparators[i].Position = (short) Math.ceil(dPosition); 
     dPosition -= dRelativeWidth; 
     } 

     // Do not forget to set TableColumnSeparators back! Otherwise, it doesn't work. 
     xPS1.setPropertyValue("TableColumnSeparators", xSeparators); 


     XCellRange xCellRangeHeader = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, xTextTable); 
     XCell xCellHeader = null; 
     XText xHeaderText = null; 



     xCellHeader = xCellRangeHeader.getCellByPosition(0,0); // cols, rows 
     xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader); 
     xHeaderText.setString("Records Center Total Capacity"); 

     xCellHeader = xCellRangeHeader.getCellByPosition(1,0); // cols, rows 
     xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader); 
     xHeaderText.setString(""+RecordCentrecapacity); 

     xCellHeader = xCellRangeHeader.getCellByPosition(0,1); // cols, rows 
     xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader); 
     xHeaderText.setString("Current Inventory For Week Ending"); 

     xCellHeader = xCellRangeHeader.getCellByPosition(1,1); // cols, rows 
     xHeaderText = (XText) UnoRuntime.queryInterface(XText.class, xCellHeader); 
     xHeaderText.setString(""+currentTotalInventory); 

Pouvez-vous me aider comme je suis nouveau dans ce sujet et je veux formater la table en fonction de mes besoins.

Répondre

0

De l'exemple swriter à https://api.libreoffice.org/examples/examples.html#Java_examples:

//create instance of a text table 
com.sun.star.text.XTextTable xTT = null; 

// get the property set of the text table 
com.sun.star.beans.XPropertySet xTTPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTT); 

// Change the BackColor 
try { 
    xTTPS.setPropertyValue("BackTransparent", Boolean.FALSE); 
    xTTPS.setPropertyValue("BackColor",Integer.valueOf(13421823)); 
    xTTRowPS.setPropertyValue("BackTransparent", Boolean.FALSE); 
    xTTRowPS.setPropertyValue("BackColor",Integer.valueOf(6710932)); 

} catch (Exception e) { 
    System.err.println("Couldn't change the color " + e); 
    e.printStackTrace(System.err); 
} 

// get the property set of the cursor 
com.sun.star.beans.XPropertySet xTCPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, 
          xTCursor); 

// Change the CharColor and add a Shadow 
try { 
    xTCPS.setPropertyValue("CharColor",Integer.valueOf(255)); 
    xTCPS.setPropertyValue("CharShadowed", Boolean.TRUE); 
} catch (Exception e) { 
    System.err.println("Couldn't change the color " + e); 
    e.printStackTrace(System.err); 
} 

Et de https://wiki.openoffice.org/wiki/API/Samples/Java/Writer/TextTable#Horizontal_alignmentL

XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface(
    XPropertySet.class, xTextTable); 

xPS.setPropertyValue("HoriOrient", com.sun.star.text.HoriOrientation.NONE); 

int iLeftMargin, iRightMargin; // I assume you know how do you want to align your table 

xPS.setPropertyValue("LeftMargin", iLeftMargin); 
xPS.setPropertyValue("RightMargin", iRightMargin); 

Voir aussi l'exemple C d'au https://wiki.openoffice.org/wiki/Writer/API/Tables#Changing_Appearance.

EDIT:

Voici un exemple Python qui définit la couleur et l'alignement vertical d'une seule cellule. XrayTool montre que les cellules TextTable supportent com.sun.star.text.CellProperties.

cell = table.getCellByName("A1") 
cell.BackColor = int("FF00FF", 16) 
cell.setPropertyValue("VertOrient", 2) 
+0

Ceci est pour toute la table mais comment ajouter de la couleur ou aligner le texte d'une seule cellule de la table et pas d'autres cellules –