使用Stream导出EXCEL文件
网上用STruts2自带的功能导出EXCEL,但是没有成功,返回前台的时候,总不能弹出下载对话框?
?
?
private InputStream excelStream; // 需要生成getter和setter
? public String execute() throws Exception
? {
????????
???????? return SUCCESS;
? }
?
? public void setExcelStream(InputStream s)
? {
?? this.excelStream = s;
? }
? public InputStream getExcelStream()
? {
??
?? StringBuffer excelBuf = new StringBuffer();
???????? excelBuf.append("BookName").append("\t").append("Year").append("\t").append("author").append("\n");
???????? excelBuf.append("Thinking in Java").append("\t").append("2001").append("\t").append("Eckel").append("\n");
???????? excelBuf.append("Spring in action").append("\t").append("2005").append("\t").append("Rod").append("\n");
???????? String excelString = excelBuf.toString();
????????
???????? excelStream = new ByteArrayInputStream(excelString.getBytes(), 0, excelString.length());
?? return this.excelStream;
? }
?
action配置
????? <action name="excel"? type="stream">
??????????????????? <param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>
??????????????????? <param name="inputName">excelStream</param>?????????
??????????????????? <param name="contentDisposition">attachment;filename="standard.xls"</param>
??????????????????? <param name="bufferSize">1024</param>
??????????????? </result>
??????? </action>
?
开始以为是前台使用EXTJS的原故,或是ACTION所在的包继承了json-default的原故,但经分析都不是,现在未解决
?
?
问题解决了,原因是前台发送请求的方式错误,造成返回的数据被EXTJS解析成字符串了
?
这种方式可以生成简单的EXCEL文件,但不能对样式进行控制和进行复杂的操作,使用POI的SS可以生成优美的EXCEL。
?
?