• [开发笔记] servlet 基于poi 生成xls 并向页面输出下载



           List list = this.getDateList(); //获取数据list。 之后转换成xls String voucherno
    = ""; if(list.size()<=0){ } HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); sheet.setColumnWidth((short) 0, (short) (256*15)); sheet.setColumnWidth((short) 1, (short) (256*15)); sheet.setColumnWidth((short) 2, (short) (256*15));//标题栏 HSSFRow row = sheet.createRow(( short ) 0 ); String [] arr = {"单据号","财务凭证号","金额"}; for(int i=0;i<arr.length;i++){ setCell(row,i,arr[i]); } //内容 for(int i =0;i<list.size();i++){ AccInputTaxTurnOutHisDto schema = (AccInputTaxTurnOutHisDto)list.get(i); row = sheet.createRow(( short ) i+1 ); int index = 0; voucherno = schema.getVoucherNo(); setCell(row,index++,schema.getCertiNo()); setCell(row,index++,schema.getRemark1()); setCellDouble(row,index++,schema.getTurnOutFee()); } try { response.setContentType("application/OCTET-STREAM;charset=UTF-8"); String filename = URLEncoder.encode("转出凭证号" + voucherno+"进项转出清单.xls", "UTF-8"); //确保文件名不会中文乱码,这个很关键 response.setHeader("Content-disposition", "attachment;filename="+filename); OutputStream fos = response.getOutputStream(); wb.write(fos); fos.flush(); //通过respose 向页面输出,页面会得到下载的提示 fos.close(); } catch (Exception e) { System.err.println("生成xls文件出错"+e); }





        private void setCell(HSSFRow row,int place,String value){
    HSSFCell cell = row.createCell(( short ) place );
    cell.setEncoding(HSSFCell.ENCODING_UTF_16); //确保单元格支持中文
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue(value);
    }

    private void setCellDouble(HSSFRow row,int place,double value){
    HSSFCell cell = row.createCell(( short ) place );
    cell.setEncoding(HSSFCell.ENCODING_UTF_16); //确保单元格支持中文
    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    cell.setCellValue(value);
    }

  • 相关阅读:
    初学mongodb笔记
    git学习【转载】
    json序列化与反序列化
    ES6函数比对ES5函数
    前端js脚本与防止js脚本
    js函数整合队列顺序执行插件
    padding-使用必记
    css小技巧
    三分钟教会你开密码箱
    百度地图Marker优化方案
  • 原文地址:https://www.cnblogs.com/heiniao/p/6510456.html
Copyright © 2020-2023  润新知