jjemphoung 0 Light Poster

I am currently doing some writings to an excel file (.xls) through JExcel API. My problem is about the autoSize method of CellView.

My code.

public void write() throws IOException, WriteException {
        File file = new File(inputFile);
	WorkbookSettings wbSettings = new WorkbookSettings();

        wbSettings.setLocale(new Locale("en", "EN"));

        WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
        workbook.createSheet(getSheetTitle(), 0);
        WritableSheet excelSheet = workbook.getSheet(0);
        createHeaders(excelSheet);   //write headers, works fine
        createContents(excelSheet);  //write contents, works fine

        //problem starts here
        CellView cv = new CellView();

        for(int x = 0; x < getHeaders().size(); x++){   //getHeaders().size returns the no. of columns
            cv = excelSheet.getColumnView(x);
            cv.setAutosize(true);
            excelSheet.setColumnView(x, cv);
        }

        workbook.write();
        workbook.close();
    }

My problem is when I open the .xls file, some of the columns are not auto sized, though some are auto sized. For ex., a row of my .xls file contains text like

| BSIT4G[newline here]G1 | 950.00 | Nov 6, 2011, 11:30AM | abcdefg |

(where | = cell borders)

As you can see, the first column is not auto sized. When I tried to switch the positions of columns, the column the contain that data is still not autosized. Any ideas? Replies are greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.