POI 导出文件
1.前台ajax
方法1:
//导出表格
function exportExcel(){
var parms = new Object();
parms["id"] = id;
$.ajax({
cache: true,
type: "POST",
url: ctx+'/export/exportExcel.do',
data: parms,
async: false,
success:function(data){
},
error:function(){
}
});
}
方法2:
// 导出报表
var exportExcel = function() {
if (0 ==dateLength) {
layer.msg("没有可导出的数据,请重新查询。");
return;
}
var url = ctx + '/export/exportExcel.do';
$("#id").val(id);
$("#downForm").attr('action',url);
$("#formType").val(type);
$("#downForm").submit();
}
2.后端
@RequestMapping("/exportExcel")
public void exportExcel(
@RequestParam(value="sort",required=false) String id,
HttpServletRequest request,HttpServletResponse response){
List<Person> list = personService.getAll(id);
//模板路径
String tempPath = getClass().getResource("/").getPath()
+"../../public/dist/file/exportExcel.xls";
//1.创建一个webbook,对应一个Excel模板文件
HSSFWorkbook wb = null;
FileInputStream fis;
try {
fis = new FileInputStream(tempPath);
wb = new HSSFWorkbook(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.在webbook中读取sheet
HSSFSheet sheet = wb.getSheetAt(0);
//导出文件名
String fileName =list.get(0).get("name").toString()+ "统计";
//获取第一行
HSSFRow firstRow = sheet.getRow(0);
// 设置第一行标题内容
String title = list.get(0).get("name").toString();
title += "统计";
firstRow.getCell(1).setCellValue(title);
//统计行
HSSFRow totalRow = sheet.getRow(2);
totalRow.getCell(3).setCellValue(name);
totalRow.getCell(5).setCellValue(sex);
totalRow.getCell(7).setCellValue(order);
//3.获取列头行
HSSFRow titleRow = sheet.getRow(5);
//获取标题所在行数
int titleRowNum = titleRow.getRowNum();
//3.在sheet中添加行
HSSFRow row = sheet.createRow(6);
//单元格样式
HSSFCellStyle cellStyle = wb.createCellStyle();
int i =0;
for(Map<String, Object> dataMap : list){
// 创建单元格,并设置值
row = sheet.createRow(titleRowNum+1);
row.createCell(0).setCellValue(i+1);
row.getCell(0).setCellStyle(cellStyle);
row.createCell(1).setCellValue(dataMap.get("name").toString());
row.getCell(1).setCellStyle(cellStyle);
row.createCell(2).setCellValue(dataMap.get("sex").toString());
row.getCell(2).setCellStyle(cellStyle);
row.createCell(3).setCellValue(dataMap.get("color").toString());
row.getCell(3).setCellStyle(cellStyle);
titleRowNum ++;
i++;
}
// 预下载的文件的路径+文件名
String filePath = FtpConf.getProperty(FtpConf.LOCAL_BASE_PATH)+"/"+list.get(0).get("name").toString()+"报表.xls";
FileOutputStream fos;
try {
fos = new FileOutputStream(filePath);
wb.write(fos);
fos.close();
File file = new File(filePath);
// 创建一个输入流
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
response.reset();
// 设置相应头
// response.addHeader("Content-Disposition", "attachment;filename=\""
// + new String(fileName.getBytes("GB2312"),"iso8859-1") + "\"");
response.addHeader("Content-Length", "" + file.length());
// 创建一个输出流
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
os.write(buffer);
os.flush();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.创建一个excel模板