java - Spring MVC, Excel file download, corrupts file -
i'am working on excel export functionality in 1 of webapps. set little test case , got download working, xlsx file corrupted , don't know else try. if write excel file opens without problem, error must occure when downloading.
the setup:
spring-mvc 3.2.7 poi 3.10.1 tomcat 8.0
controller method:
@requestmapping(value = "/download", method = requestmethod.get) public modelandview downloadexcel() { // create sample data list<book> listbooks = new arraylist<book>(); listbooks.add(new book("effective java", "joshua bloch", "0321356683", "may 28, 2008", 38.11f)); listbooks.add(new book("head first java", "kathy sierra & bert bates", "0596009208", "february 9, 2005", 30.80f)); listbooks.add(new book("java generics , collections", "philip wadler", "0596527756", "oct 24, 2006", 29.52f)); listbooks.add(new book("thinking in java", "bruce eckel", "0596527756", "february 20, 2006", 43.97f)); listbooks.add(new book("spring in action", "craig walls", "1935182358", "june 29, 2011", 31.98f)); // return view resolved excel view resolver return new modelandview(new excelbuilder(listbooks)); }
abstract custom view:
public abstract class abstractpoiexcelview extends abstractview { private static final string content_type_xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; public abstractpoiexcelview() { } @override protected boolean generatesdownloadcontent() { return true; } @override protected final void rendermergedoutputmodel(map<string, object> model, httpservletrequest request, httpservletresponse response) throws exception { xssfworkbook workbook = new xssfworkbook(); buildexceldocument(model, workbook, request, response); bytearrayoutputstream baos = createtemporaryoutputstream(); response.setheader("content-disposition", "attachment;filename=filename.xlsx"); response.setcontenttype(content_type_xlsx); workbook.write(baos); writetoresponse(response, baos); } protected abstract void buildexceldocument(map<string, object> model, xssfworkbook workbook, httpservletrequest request, httpservletresponse response) throws exception; }
excelbuilder:
public class excelbuilder extends abstractpoiexcelview { private list<book> listbooks; public excelbuilder(list<book> books) { this.listbooks = books; } @override protected void buildexceldocument(map<string, object> model, xssfworkbook workbook, httpservletrequest request, httpservletresponse response) throws exception { sheet sheet = workbook.createsheet("java books"); sheet.setdefaultcolumnwidth(30); row header = sheet.createrow(0); header.createcell(0).setcellvalue("book title"); header.createcell(1).setcellvalue("author"); header.createcell(2).setcellvalue("isbn"); header.createcell(3).setcellvalue("published date"); header.createcell(4).setcellvalue("price"); // create data rows int rowcount = 1; (book abook : listbooks) { row arow = sheet.createrow(rowcount++); arow.createcell(0).setcellvalue(abook.gettitle()); arow.createcell(1).setcellvalue(abook.getauthor()); arow.createcell(2).setcellvalue(abook.getisbn()); arow.createcell(3).setcellvalue(abook.getpublisheddate()); arow.createcell(4).setcellvalue(abook.getprice()); } } }
response header:
cache-control:private, must-revalidate content-disposition:attachment;filename="filename.xlsx" content-language:de-de content-length:3778 content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=iso-8859-1 date:wed, 19 nov 2014 12:52:05 gmt expires:thu, 01 jan 1970 00:00:00 gmt pragma:private set-cookie:jsessionid=07f50ff2b63d4003311de222782c4e89; path=/abc/; httponly x-content-type-options:nosniff x-frame-options:deny x-xss-protection:1; mode=block
it confuses me charset set, when binary data. problem?
don't return modelandview write excel file response's outputstream
@requestmapping(value = "/download", method = requestmethod.get) @responsebody public object downloadexcel(httpservletresponse response) { response.setcontenttype("application/vnd.ms-excel"); response.setheader("content-disposition", "attachment; filename=" + thefilenamestring + ".xls"); try { generateexcel(response.getoutputstream()); } catch (ioexception e) { system.out.println("error: " + e); } return null; }
check streams flushed/closed
Comments
Post a Comment