当前位置: 代码迷 >> Web前端 >> poi导出到本地 抉择本地路径 保存到本地
  详细解决方案

poi导出到本地 抉择本地路径 保存到本地

热度:661   发布时间:2013-12-22 15:06:55.0
poi导出到本地 选择本地路径 保存到本地
//poi导出excel 保存到本地

public void exportExcel(HttpServletRequest req, HttpServletResponse resp) throws IOException{

HSSFWorkbook wb = new HSSFWorkbook();
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/x-download");

String filedisplay = "未匹配的产品清单.xls";
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
resp.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);
HSSFSheet sheet = wb.createSheet("未匹配产品");
sheet.setColumnWidth(0, 50*160);
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);

HSSFRow row = sheet.createRow((int) 0);

HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFCell cell = row.createCell(0);
cell.setCellValue("产品名称");

Map<String, Object> params = new HashMap<String, Object>();
params.put("userId", this.getMember().getId());
params.put("matchabled", 0);
List<Match> matchs = this.getBeanManager().listBean(Match.class, params);

for (int i = 0; i < matchs.size(); i++) {
row = sheet.createRow((int) i + 1);
Match match = (Match) matchs.get(i);
row.createCell(0).setCellValue(match.getName());
}
try {
OutputStream out = resp.getOutputStream();
wb.write(out);
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
  相关解决方案