几种简单的导出
1.直接返回文件路径方式:
1.1struts配置:
<action name="exportReport1" class="xx.xx.action.exportReportAction" method="exportReport1">
<result name="success" type="json">
<param name="root">resultPath</param>
</result>
</action>
1.2具体代码:
private String resultPath = "";
public String getResultPath() {
return resultPath;
}
public void setResultPath(String resultPath) {
this.resultPath = resultPath;
}
public String exportReport1() {
try {
Map<String,Object> params = new HashMap<String,Object>();
params.put("id", request.getParameter("projectId"));
resultPath = RestfulUtil.restfulInvoke("http://127.0.0.1:8080/xx/xx", "POST", params);
} catch (Exception e) {
log.error("访问服务器失败:",e);
}
return SUCCESS;
}
2.返回流的方式:
2.1struts配置:
<action name="exportReport2" class="xx.xx.action.exportReportAction" method="exportReport2">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel;charset=utf-8</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${excelFileName}.xlsx"</param>
<param name="bufferSize">4096</param>
</result>
</action>
2.2具体代码:
public String exportReport2(){
ByteArrayOutputStream byteArrayOutputStream = null;
try {
File file = FileUtils.getFile("xxxx.xlsx".replace("/", File.separator));
byteArrayOutputStream = new ByteArrayOutputStream();
Workbook workbook = new XSSFWorkbook(FileUtils.openInputStream(file));
workbook.write(byteArrayOutputStream);
byteArrayOutputStream.flush();
inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
excelFileName = "xxx";
result = "success";
} catch (IOException e) {
LogManager.recordLog(LogType.error,"导出excel失败");
result = "error";
} finally {
IOUtils.closeQuietly(byteArrayOutputStream);
}
return SUCCESS;
}