m0_37468810 2017-05-03 08:13 采纳率: 0%
浏览 790
已结题

用servlet运用POI下载Word文档

我现在要做的是用servlet运用POI生成Word, 流程就是:当你点击原件下载(一个按钮)就会生成Word 我在jsp中有一个onclick事件 一点击就跳到servlet里面 运行时报空 而且我目前不知道servlet如何写

  • 写回答

2条回答

  • csFirend 2017-05-03 09:10
    关注

    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";这里是点击下载后返回的界面 我是跳回去,
    
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗