我现在要做的是用servlet运用POI生成Word, 流程就是:当你点击原件下载(一个按钮)就会生成Word 我在jsp中有一个onclick事件 一点击就跳到servlet里面 运行时报空 而且我目前不知道servlet如何写
javascript点击时间跳入后台,切记勿用ajax: location.href="/goods/returns.xhtml";
java代码:@RequestMapping("returns.xhtml")
public String returns(HttpServletRequest req,HttpServletResponse rep){
HSSFWorkbook wb=export(userDao.getGoods(l));
rep.setContentType("application/msexcle");
rep.setHeader("Content-disposition", "attachment;filename=goods.xls");
try {
OutputStream os=rep.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Goods";
}
public HSSFWorkbook export(List<GoodsEntity> myl){
String[] excelHeader={"Gid","Gname","Gcontext","Gmoney","Gcolor"};
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet("商品信息");
HSSFRow row=sheet.createRow(0);
HSSFCellStyle style=wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i <excelHeader.length; i++) {
HSSFCell cell=row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
sheet.autoSizeColumn(i);
}
for (int i = 0; i < myl.size(); i++) {
row=sheet.createRow(i+1);
GoodsEntity goods=myl.get(i);
row.createCell(0).setCellValue(goods.getGid());
row.createCell(1).setCellValue(goods.getGname());
row.createCell(2).setCellValue(goods.getGcontext());
row.createCell(3).setCellValue(goods.getGmoney());
row.createCell(4).setCellValue(goods.getGcolor());
}
return wb;
}
这里用的是springmvc,return "Goods";这里是点击下载后返回的界面 我是跳回去,
首先创建一个WORD文件,然后设置数据,然后用IO流写出。