java - Using XSSFWorkbook and AbstractExcelView -


it understanding abstractexcelview class function buildexceldocument not support xssfworkbook (https://jira.spring.io/browse/spr-6898).

i trying work around implementing buildexceldocument function follows:

workbook workbook = null; protected void buildexceldocument(map model,      hssfworkbook wbook,      httpservletrequest request,      httpservletresponse response) throws exception {        if(request.getrequesturl().tostring().contains("xlsx")){           workbook = new xssfworkbook();           workbook.createsheet();           excelversion = "xlsx"; //used determine response        }else{           workbook = wbook;        }        buildbothexceldocument(model,workbook,request,response); } 

here, buildbothexceldocument function use apache ss usermodel generate , create both excel versions, hssfworkbook , xssfworkbook. once workbook created create header prompt user save or open excel file:

if(excelversion.equals("xlsx")){    response.setheader("pragma", "public");    response.setheader("cache-control", "max-age=0");    response.setcontenttype("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");    response.setheader("content-disposition", "attachment; filename=test.xlsx"); }else{    response.setheader("pragma", "public");    response.setheader("cache-control", "max-age=0");    response.setcontenttype("application/vnd.ms-excel");    response.setheader("content-disposition", "attachment; filename=\"test.xls\""); } 

all of aforementioned code works when using hssfworkbook. workbook's data gets created correctly, can downloaded, , open correctly using either microsoft excel 2003 or microsoft excel 2007.

when attempt create .xlsx file, error when opening stating "excel cannot open file 'test.xlsx' becuase file format or file extension not valid. verify file has not been corrupted , file extension matches format of file". leads me believe somewhere along spring corrupted file. questions are:

1 - response contenttype , header correct? (source: what correct content-type excel files?)

2 - there way use spring , abstractexcelview create xlsx excel file?

3 - spring support xssfworkbook's @ all?

some additional notes:

apache poi version - v3.9

i have tried following when creating header:

    response.setheader("content-disposition", "attachment; filename=\"test.xlsx\""); 

and

    response.setheader("content-disposition", "attachment; filename='test.xlsx'"); 

all yield same failed results.

i used following link convert old hssf code use ss usermodel, http://poi.apache.org/spreadsheet/converting.html. tested conversion process without interference of spring , able create both xls , xlsx files. help.

i know .net implementation of poi has method write stream. if write workbook interface stream, , return stream byte array parametric content , headers, should work -- had similar issue in .net mvc controllers not serving response correctly.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -