2012-12-20 125 views
3

我将数据从CSV文件输入到OpenOffice电子表格中。最佳列宽OpenOffice Calc

这段代码获得在电子表格中的一个新的工作表:

Public Spreadsheet getSpreadsheet(int sheetIndex, XComponent xComp) 
{ 
    XSpreadsheet xSheets = ((XSpreadsheetDocument)xComp).getSheets(); 
    XIndexAccess xSheetIA = (XIndexAccess)xSheets; 
    XSpreadsheet XSheet = (XSpreadsheet)xSheetsA.getByIndex(sheetIndex).Value; 
    return XSheet; 
} 

我然后有进入列表成在时间的小区范围中的一个细胞的方法。我希望能够自动设置这些单元格的列大小。这有点像

string final DataCell; 
Xspreadsheet newSheet = getSpreadsheet(sheetIndex, xComp); 
int numberOfRecords = (int numberOfColumns * int numberOfRows); 
for(cellNumber = 0; cellNumber < numberOfrecords; cellNumber++) 
{ 
    XCell tableData = newSheet.getCellbyPosition(columnValue, rowValue); 
    ((XText)tableData).setString(finalDataCell); 
    column Value++; 
    if(columnValue > = numberOfColumns) 
    { 
    rowVal++ column = 0; 
    } 
} 

谷歌上搜索我已经找到了功能后:

columns.OptimalWidth = Truehttp://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=31292

但即时如何使用这个不确定。任何人都可以进一步解释这一点,或想想另一种方法来让细胞自动适应?

回答

5

我知道代码中的评论是用西班牙文,我认为,但代码是英文的。我通过Google翻译发表了评论,现在他们都是英文的。我复制它从here

//Auto Enlarge col width 
private void largeurAuto(string NomCol) 
{ 
XCellRange Range = null; 
Range = Sheet.getCellRangeByName(NomCol + "1"); //Recover the range, a cell is 

XColumnRowRange RCol = (XColumnRowRange)Range; //Creates a collar ranks 
XTableColumns LCol = RCol.getColumns(); // Retrieves the list of passes 
uno.Any Col = LCol.getByIndex(0); //Extract the first Col 

XPropertySet xPropSet = (XPropertySet)Col.Value; 
xPropSet.setPropertyValue("OptimalWidth", new one.Any((bool)true)); 
} 

这做什么它这样的:首先它获得的区域名称,然后得到的第一列。但是,真正的代码是使用XpropertySet,这真的很好解释here

+0

谢谢,我设法让这个代码稍有不同的版本工作,但这真的很有帮助。我会把我的版本包装下来,它可以帮助别人。再次感谢。 –

0
public void optimalWidth(XSpreadsheet newSheet) 
     { 
     // gets the used range of the sheet 
      XSheetCellCursor XCursor = newSheet.createCursor(); 
      XUsedAreaCursor xUsedCursor = (XUsedAreaCursor)XCursor; 
      xUsedCursor.gotoStartOfUsedArea(true); 
      xUsedCursor.gotoEndOfUsedArea(true); 

      XCellRangeAddressable nomCol = (XCellRangeAddressable)xUsedCursor; 


      XColumnRowRange RCol = (XColumnRowRange)nomCol; 
      XTableColumns LCol = RCol.getColumns(); 
     // loops round all of the columns 
      for (int i = 0; i < nomCol.getRangeAddress().EndColumn;i++) 
      { 
      XPropertySet xPropSet = (XPropertySet)LCol.getByIndex(i).Value; 
      xPropSet.setPropertyValue("OptimalWidth", new uno.Any(true)); 
      } 
     } 
相关问题