Struts poi 导出

来源:互联网 发布:人工智能柴玉梅版答案 编辑:程序博客网 时间:2024/05/17 06:53

1.配置struts.xml

<action name="getOrderBillExcel" class="ordersAction" method="getOrderBillExcel"><result name="success" type="stream"><param name="contentType">application/vnd.ms-excel</param><!--contentType指定下载的文件类型--->  <param name="contentDisposition">attachment;filename="${excelFileName}"</param><!-----文件名------><param name="bufferSize">1024</param><param name="inputName">excelStream</param><!--返回流excelStream为action中的流变量名称--->    </result></action>
2.poi 的maven配置如下

<dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi</artifactId>     <version>3.9</version></dependency>
3.Action类如下

    private InputStream excelStream;  //输出流变量      private String excelFileName; //下载文件名      public InputStream getExcelStream() {        return excelStream;    }    public void setExcelStream(InputStream excelStream) {        this.excelStream = excelStream;    }    public String getExcelFileName() {        return excelFileName;    }    public void setExcelFileName(String excelFileName) {        this.excelFileName = excelFileName;    }    /**     * 导出     * @return     */    public String getOrderBillExcel(){        String key = this.request.getParameter("key");        String productType = this.request.getParameter("productType");        String status = this.request.getParameter("status");        String hql = "from Orders where deleted=0";        if (StringUtils.isNotEmpty(key)) {            hql = hql + " and (name='" + key + "' or no='" + key + "' or productName='" + key + "')";        }        if(StringUtils.isNotEmpty(productType)){            hql = hql + "and (productType='" + productType + "')";        }        if(StringUtils.isNotEmpty(status)){            hql = hql + "and (status='" + status + "')";        }        hql = hql + " order by id desc";        try {           Map<String, List<Orders>> model = new HashMap<String, List<Orders>>();            List<Orders> ordersList = this.ordersService.list(hql);            model.put("findList", ordersList);            HSSFWorkbook workbook = ordersExcelAction.buildExcelDocument(model);            ByteArrayOutputStream output = new ByteArrayOutputStream();            workbook.write(output);            byte[] fileContent = output.toByteArray();            ByteArrayInputStream isInfo = new ByteArrayInputStream(fileContent);            excelStream = isInfo;             //文件流              String infoName = "订单信息.xls"; //设置下载的文件名             excelFileName=new String(infoName.getBytes("gb2312"), "iso8859-1");        } catch (Exception e) {            e.printStackTrace();        }        return "success";    }
4.导出模板

 public HSSFWorkbook buildExcelDocument(Map<String, List<Orders>> model)throws Exception{        HSSFWorkbook workbook = new HSSFWorkbook();        List<Orders> list = (List<Orders>) model.get("findList");        //create a wordsheet        HSSFSheet sheet = workbook.createSheet("*****数据");        HSSFRow header = sheet.createRow(0);        header.createCell(0).setCellValue("ID");        header.createCell(1).setCellValue("姓名");        header.createCell(2).setCellValue("性别");        header.createCell(3).setCellValue("年龄");        header.createCell(4).setCellValue("价格");                int rowNum = 1;        for (int i=0; i<list.size(); i++)         {            //create the row data            HSSFRow row = sheet.createRow(rowNum++);            row.createCell(0).setCellValue(list.get(i).getId()==null?"":list.get(i).getId().toString());            row.createCell(1).setCellValue(list.get(i).getNo()==null?"":list.get(i).getNo().toString());            row.createCell(2).setCellValue(list.get(i).getName()==null?"":list.get(i).getName().toString());            row.createCell(3).setCellValue(list.get(i).getProductName()==null?"":list.get(i).getProductName().toString());            row.createCell(4).setCellValue(Double.parseDouble(list.get(i).getProductMoney()==null?"":list.get(i).getProductMoney()+""));        }        return workbook;    }        /**     * @function       * @param      * @return     */    private String getProductType(String productType){        if(productType!=null&&!"".equals(productType.trim())){            if (productType.trim().equals("0")){                return "";            }else if(productType.trim().equals("1")){                return "";            }else if(productType.trim().equals("2")){                return "";            }else if(productType.trim().equals("3")){                return "";            }else if(productType.trim().equals("4")){                return "";            }else{                return "";            }        }        return "";    }    /**     * @function      * @     * @return     */    public String getOrderStatus(int i){        switch(i){            case 0:return "";            case 1:return "";            case 2:return "";            case 3:return "";            default: return "";         }    }