excel - Spring MVC / AbstractExcelView / Apache POI : File Error -
i'm using apache poi , extending springs abstractexcelview 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 loop through 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 (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, shouldnt need }
i can create sheet when have following code after 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 examples online people include writing out outputstream or setting contenttype, seems abstractexcelview handles that.
when comment out 6 lines above, tries create file (i can see in console logs parsing data correctly), when go open file, excel fails open , displays following:
"file error: data may have been lost."
i know code works is, i'm trying right way , follow else doing online. don't think have code in outputstream or contenttype stuff, works if do.
any appreciated!
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 wordsheet hssfsheet sheet = workbook.createsheet("title"); hssfrow header = sheet.createrow(0); header.createcell(0).setcellvalue("whatever"); header.createcell(1).setcellvalue("whatever"); int rownum = 1; (...) { // create row data hssfrow row = sheet.createrow(rownum++); row.createcell(0).setcellvalue("whatever"); row.createcell(1).setcellvalue("whatever"); } } }
Comments
Post a Comment