|
本帖最后由 QlikView 于 2018-11-6 18:23 编辑
目前使用weX5 制作纯PC端 web 应用,涉及导出文件功能。
求教weX5 如何实现导出Excel并弹出下载框,另存文件到客户端。使用如下方法,浏览器不会弹出下载框。
前台调用方法如下:
//导出Excel
Model.prototype.exportClick = function(event){
justep.Baas.sendRequest({
"url" : "/test/exportTest",
"action" : "exportExcel",
"params" : {
},
"success" : function(data) {
}
});
};
后台使用JAVA POI导出Excel方法,后台代码如下:
//导出Excel
public static JSONObject exportExcel(JSONObject params, ActionContext context) throws SQLException, NamingException, ClassNotFoundException, java.sql.SQLException, UnsupportedEncodingException {
System.out.println("开始导出");
HttpServletResponse res = (HttpServletResponse)(context.get(ActionContext.RESPONSE));
//文件名(可根据传递来的参数设置)
Connection conn = context.getConnection(DATASOURCE_Novartis);
/*连存储过程:包名.包体名*/
CallableStatement proc = conn.prepareCall("{call sp_exportTest}");
/*执行*/
ResultSet rs = proc.executeQuery();
String fileName = "test";
ExcelUtil util = new ExcelUtil();
try {
util.downLoad(res, fileName, rs);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* try {
util.downLoad(res,fileName, rs);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}*/
return null;
}
/*下载Excel文件*/
public void downLoad(HttpServletResponse response,String fileName,ResultSet rs) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
this.createExcel(fileName, rs).write(os);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response响应头弹出下载框
response.reset();
response.setContentType("application/x-execl;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
/*创建Excel文件*/
public SXSSFWorkbook createExcel(String fileName,ResultSet rs){
// 创建一个Excel文件 每1000条写一个临时文件
SXSSFWorkbook workbook = new SXSSFWorkbook (1000);
// 创建一个工作表
Sheet sheet = workbook.createSheet("test");
// 添加表头行
Row row = sheet.createRow(0);
try {
ResultSetMetaData m = rs.getMetaData();
int columns=m.getColumnCount();
// 遍历添加表头内容
for(int i=1;i<=columns;i++){
Cell cell = row.createCell(i-1);
cell.setCellValue(m.getColumnName(i));
}
int j = 1;
// 遍历添加数据内容
while(rs.next()){
row = sheet.createRow(j);
j++;
for(int i=1;i<=columns;i++){
Cell cell = row.createCell(i-1);
cell.setCellValue(rs.getString(i));
}
}
/* FileOutputStream os = new FileOutputStream(path+fileName+".xlsx");
workbook.write(os);
os.close();*/
System.out.println("写入Excel完成");
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return workbook;
}
|
|