使用SpringMvc调用POI jar导出excel的源码

@RequestMapping(value = "/result/export")
public String export(ResultIn in,HttpServletRequest request,
HttpServletResponse response) throws Exception {
ResultXls xls=resultService.getResultXls(in);
HSSFWorkbook wb = resultService.toExcel(xls);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
//response.setHeader("Content-disposition", "attachment;filename="+ "query" + ".xls");
response.setHeader("Content-Disposition", "attachment;filename=export.xls");
//response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
return null;
}

//提取excel文件需要的数据
@Transactional
public ResultXls getResultXls(ResultIn in) {
String json= this.find(in);
List<Map> dataList=GsonUtils.getList(json);
ResultXls o=new ResultXls();
o.setDataList(dataList);
List<SequencesPO> sequenceList = GsonUtils.gson.fromJson(in.getSequences(),
new TypeToken<List<SequencesPO>>() {
}.getType());
o.setSequencesList(sequenceList);
return o;
}

//转换为excel文件
@Transactional
public HSSFWorkbook toExcel(ResultXls xls ) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Campaign");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
List<Map> dataList=xls.getDataList();
List<SequencesPO> sequencesList=xls.getSequencesList();
//循环列标题
String colname="";
row.createCell(0).setCellValue("指标数据");
for (int i = 1; i <= sequencesList.size(); i++) {
colname=sequencesList.get(i).getDisplayName();//title中文
row.createCell(i).setCellValue(colname);
}
//循环行
String val="";
for (int i=0;i<dataList.size();i++){
Map map=dataList.get(i);
row = sheet.createRow(i + 1);
val=(String)map.get("sj");//第一列
row.createCell(0).setCellValue(val);
//循环列
for (int j=1;j<=sequencesList.size();j++){
val=(String)map.get("c"+j);
row.createCell(j).setCellValue(val);
}
}
return wb;
}

上一篇:install kali on my x200


下一篇:对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)