问题形成:
Ajax结合配置的excel模板,导出excel时,无任何效果?
过程描述:
$.ajax({
type:"post",
url:basePath + '/manage/exports',
data:JSON.stringify(params),
contentType: "application/json;charset=utf-8",
success:function(data){
console.log("-----1230------",data)
}
});
excel配置:
//导出方法
public void export(Map param, HttpServletResponse response,HttpServletRequest request) throws FileNotFoundException {
// 文件根路径
String fileRootPath = request.getSession().getServletContext().getRealPath("/excelTemplate");
// 模板文件名称
String tempFileName = "xxxxxx.xls";
// 内容
Map bean = new HashMap();
bean.put("row", param);
String fileName = "故障停电.xls";
// 设置sheet页名称
String sheetName = "停电";
downFile(response, bean, fileRootPath, tempFileName, fileName, sheetName);
}
// 文件下载
public void downFile(final HttpServletResponse response, Map bean, String fileRootPath,String tempFileName, String fileName, String sheetName)
throws FileNotFoundException {
if (bean != null) {
XLSTransformer transformer = new XLSTransformer();
InputStream in = new FileInputStream(new File(fileRootPath + File.separator + fileName));
HSSFWorkbook workbook;
try {
workbook = (HSSFWorkbook) transformer.transformXLS(in, bean);
Sheet sheet = workbook.getSheetAt(0);
// 设置sheet页名称
workbook.setSheetName(0, sheetName);
try {
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(),"ISO8859-1"));
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParsePropertyException e1) {
e1.printStackTrace();
} catch (InvalidFormatException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
结果:
1.前台发送请求,后台使用poi生成excel文件时,过程中不报错,但浏览器无任何反应,也无下载界面。
2.console.log("-----1230------",data),可以看到控制台,输出了一段乱码文字,整个页面无任何异常错误提示。
3.导出excel时,应该是把文件下载下来,因此后台就需要往前台(浏览器)输出文件流。
4.Ajax请求获取的数据是字符串,无法解析后台返回的文件。
附:不使用模板导出,在程序中写死表格的代码。
//导出方法
public void exportBookstable(Map param,HttpServletResponse response) {
// 获取数据
List
getHSSFWorkbook()方法:
public class ExportFaultTable {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String title0,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
//行宽
sheet.setDefaultColumnWidth(15);
sheet.setDefaultRowHeight((short) 50);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row0 = sheet.createRow(0);
HSSFRow row1 = sheet.createRow(1);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
HSSFCellStyle style1 = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
// 设置字体
HSSFFont hssfFont = wb.createFont();
hssfFont.setFontName("DengXian");
hssfFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(hssfFont);
// 主标题样式
HSSFFont hssfFont1 = wb.createFont();
hssfFont1.setFontName("DengXian");
hssfFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
hssfFont1.setFontHeightInPoints((short) 20);//设置字体大小
style1.setFont(hssfFont1);
// 设置前景色
//style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
// 设置背景颜色
//style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
// 这里设置了底边边框,左边框、右边框和顶边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
//声明列对象
HSSFCell cell1 = null;
HSSFCell cell0 = null;
//创建标题
for(int i=0;i
前台使用 location.href ,可以正常导出文件。
var url = "manage/exportbookstable?param="+ "参数" ;
location.href = basePath+url;