0
我在java中創建了一個使用open office api的表格。現在我想格式化表格單元格內容,如給它背景顏色,更改字體,大小和對齊方式。 我的代碼是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);
你能幫我,我這個話題在新的,我想根據我的需要格式化的表格。
這是整個表格,但如何添加顏色或對齊表單單元格的文本而不是其他單元格 –