Java Web利用POI导出Excel简单例子

public void exportCF() {
        Session session = SessionHelper.currentSession();
        System.out.println(keycode);
        try {
            // 获取当前业务号查封信息
            Query query = session.createQuery("from Cfk c where c.keycode='" + keycode + "'");
            List cfks = query.list();
            int num = cfks.size();
            HttpServletResponse reponse = ServletActionContext.getResponse();
            HttpServletRequest request=ServletActionContext.getRequest();
            HSSFWorkbook wb = exportwb(cfks);
            String name = keycode + "查封记录.xls";
            //name= encodeFilename(name, request);    
            reponse.setContentType("application/vnd.ms-excel");
            reponse.setHeader("Content-disposition", "attachment;filename="+name);
            OutputStream ops = reponse.getOutputStream();
            wb.write(ops);
            ops.flush();
            ops.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
private HSSFWorkbook exportwb(List cfks) {
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sf3 = new SimpleDateFormat("yyyy年MM月dd日  hh时mm分");
        String[] colNames = { "查封机关", "查封原因", "查封文件", "查封文号", "查封开始时间", "查封结束时间", "解除查封文件", "解除查封文号", "查封登记时间", "解封登簿人", "查封备注", "解封备注" };
        // 单元格列宽
        int[] excelHeaderWidth = { 1000, 1500, 6500, 6500, 6500, 6500,  1000, 1500, 6500, 1200, 6500, 6500 };
        HSSFWorkbook wbk = new HSSFWorkbook();
        HSSFSheet sheet = wbk.createSheet("查封信息");

        HSSFCellStyle style = wbk.createCellStyle();
        // 设置居中样式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
        // 设置合计样式
        HSSFCellStyle style1 = wbk.createCellStyle();
        HSSFFont font = wbk.createFont();
        font.setColor(HSSFColor.BLUE.index);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 粗体
        style1.setFont(font);
        style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
        // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
        HSSFRow biaoti = sheet.createRow(0);
        // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
        HSSFCell cell1 = biaoti.createCell(0);
        cell1.setCellValue("查封登记信息");
        cell1.setCellStyle(style);
        // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 12));
        // 设置列宽度(像素)
        for (int i = 0; i < excelHeaderWidth.length; i++) {
            sheet.setColumnWidth(i,  excelHeaderWidth[i]);
        }
        // 在sheet里创建第二行
        HSSFRow row2 = sheet.createRow(1);
        for (int j = 0; j < colNames.length; j++) {
            HSSFCell cell = row2.createCell(j);
            cell.setCellValue(colNames[j]);
            cell.setCellStyle(style1);
            sheet.autoSizeColumn(j);
        }
        for (int i = 0; i < cfks.size(); i++) {
            Cfk cfk = cfks.get(i);
            HSSFRow row = sheet.createRow(i + 2);
            row.createCell(0).setCellValue(cfk.getUnitname());
            row.createCell(1).setCellValue(cfk.getCffor());
            row.createCell(2).setCellValue(cfk.getCfwj());
            row.createCell(3).setCellValue(cfk.getFwh());
            if (cfk.getCfqsrq() != null) {
                row.createCell(4).setCellValue(sFormat.format(cfk.getCfqsrq()));
            } else {
                row.createCell(4).setCellValue("无");
            }
            if (cfk.getJfdate() != null) {
                row.createCell(5).setCellValue(sFormat.format(cfk.getJfdate()));
            } else {
                row.createCell(5).setCellValue("无");
            }
            row.createCell(6).setCellValue(cfk.getJfwj());
            row.createCell(7).setCellValue(cfk.getJffwh());
            if (cfk.getCfdate() != null) {
                row.createCell(8).setCellValue(sf3.format(cfk.getCfdate()));
            } else {
                row.createCell(9).setCellValue("无");
            }
            row.createCell(10).setCellValue(cfk.getJfdjr());
            row.createCell(11).setCellValue(cfk.getBz());
            row.createCell(12).setCellValue(cfk.getJfbz());
        }

        return wbk;
    }

struts2配置文件


        
        

jsp页面信息

 

你可能感兴趣的:(Java Web利用POI导出Excel简单例子)