Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using Apache POI and extending Springs AbstractExcelView to create Excel sheets.

public class ExcelSpreadsheetView extends AbstractExcelView {

protected void buildExcelDocument(Map<String, Object> model,
            HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

//GET POSITIONS TO LOOP THROUGH FROM MODEL
@SuppressWarnings("unchecked")
List<Position> positions = (List<Position>) model.get("positions");

int lastRow = 0;

mySheet = wb.createSheet("SHEET1");

myRow = mySheet.createRow(lastRow);
myCell = myRow.createCell(0);

//loop through positions
for (int p = 0; p < positions.size(); p++) {
     myRow = mySheet.createRow(lastRow);
     myCell = myRow.createCell(0);
     myCell.setCellValue(new HSSFRichTextString(positions.get(p).getPositionName()));
     lastRow++;
}

//response stuff goes here, but I shouldnt need it

}

I can successfully create a sheet when I have the following code after all the POI code:

response.setHeader("Content-Disposition", "inline;filename=\"spreadsheet.xls\"");
response.setContentType("APPLICATION/OCTET-STREAM");
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();

Thing is, in none of the examples online do people include the writing out to the OutputStream or setting the ContentType, it seems like the AbstractExcelView handles that.

When I comment out the 6 lines above, it tries to create the file (I can see in console logs that its parsing all the data correctly), but when I go to open the file, Excel fails to open it and displays the following:

"File Error: data may have been lost."

I know my code works as is, I'm just trying to do it the right way and follow what everyone else is doing online. I don't think I have code in the OutputStream or ContentType stuff, but it only works if I do.

Any help much appreciated!

share|improve this question

1 Answer 1

Show me more code please.

You use HSSFSheet ?

public class ExcelReportView extends AbstractExcelView {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // create a wordsheet
        HSSFSheet sheet = workbook.createSheet("Title");

        HSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("whatever");
        header.createCell(1).setCellValue("whatever");

        int rowNum = 1;
        for (...) {
            // create the row data
            HSSFRow row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue("whatever");
            row.createCell(1).setCellValue("whatever");
        }
    }
}
share|improve this answer
    
added more code –  javabeer May 8 '13 at 19:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.