Question

I am unable to use autosizecolumn(). Actually I am not getting where to write this autosizecoloumn() method though I got all data printed on xls file and now my only requirement is to set autosizecolumn.

I have used addmergeredregion on top-left and on top-right company logo and table data below both.

For Column header

                   short mainrow=10;


                        row=sheet.createRow(mainrow);
                        cell= row.createCell((short)0);
                        cell.setCellValue(new HSSFRichTextString("Sr.No."));
                        cell.setCellStyle(header);

                        sheet.autoSizeColumn((short)0);                 

                        cell= row.createCell((short)1);
                        cell.setCellValue(new HSSFRichTextString("Town_Code"));
                        cell.setCellStyle(header);

                        cell= row.createCell((short)2);
                        cell.setCellValue(new HSSFRichTextString("Town_Name"));
                        cell.setCellStyle(header);

                        cell= row.createCell((short)3);
                        cell.setCellValue(new HSSFRichTextString("State_Name"));
                        cell.setCellStyle(header);

                        cell= row.createCell((short)4);
                        cell.setCellValue(new HSSFRichTextString("Country_Name"));
                        cell.setCellStyle(header);

Fill data into XLS

                        short cellnum;
                        Integer srno=1;
                        for(Town town : townList){
                        row=sheet.createRow(++mainrow);
                        cellnum=0;
                        cell= row.createCell(cellnum++);
                        cell.setCellValue(srno++);
                        cell.setCellStyle(dataformat);

                        cell= row.createCell(cellnum++);
                        cell.setCellValue(new HSSFRichTextString(town.getTownCode()));
                        cell.setCellStyle(dataformat);
                        cell= row.createCell(cellnum++);
                        cell.setCellValue(new HSSFRichTextString(town.getName()));
                        cell.setCellStyle(dataformat);
                        cell= row.createCell(cellnum++);
                        cell.setCellValue(new HSSFRichTextString(town.getStateName()));
                        cell.setCellStyle(dataformat);
                        cell= row.createCell(cellnum++);
                        cell.setCellValue(new HSSFRichTextString(town.getCountryName()));
                        cell.setCellStyle(dataformat);

                        }

                        workbook.write(reportOutBuffer);
                        reportOutBuffer.close();
                        printNewReport(request, resp, reportOutBuffer.toByteArray(), "application/xls" ,"Town_Report_2013.");
Was it helpful?

Solution

It is important to call the autoSizeColumn(); after data has been written to the sheet. I would include it right before the FileOutputStream out = ... call. Like previous answers, auto size will take up lots of buffer space and should not be used on large sheets or SQL calls to data. It is better to define the columnSize manually. For example:

sheet.setColumnSize(0, 10*256);

The first number is the column number, first column is Zero. Column width is calculated in 1/256ths of character width.

OTHER TIPS

It would be better if you write autosizecoloumn() at very end, so that the cell will not get updated after then. If not clear, just write it before workbook.write method. It should be like.

    sheet.autoSizeColumn(colNum);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top