3
votes

I'm entering data from a CSV file into a OpenOffice spreadsheet.

This code gets the a new sheet in a spreadsheet:

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;
}

I then have method that enters a list into a cell range one cell at a time. I want to be able to automatically set the column size for these cells. which is something like

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; 
  } 
}

After googling i have found the function:

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

but im unsure on how to use this. Could anyone explain this further or think of another way to have the cell autofit?

2

2 Answers

5
votes

I understand the comments in the code are in Spanish I think, but the code is in English. I ran the comments through Google translate so now they are in English. I copied it from 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)); 
}

What this does it this: First it gets the range name and then gets the first column. The real code, though, is XpropertySet being used, which is explained REALLY well here.

0
votes
   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));
            }
        }