用博客记录点滴……

POI导出excel并下载(以流的形式在客户端下载,不保存文件在服务器上)

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;

//使用poi生成excel表格
public void ExportExcel(ArrayList arrList,OutputStream out){
        PublicFunction fun = new PublicFunction();
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet("Sheet1");
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth(20);
        
        // 生成表格单元样式
        HSSFCellStyle style = workbook.createCellStyle();
        // 设置表格单元样式
        style.setFillForegroundColor(HSSFColor.WHITE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成表格单元字体
        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        // 把字体应用到当前的样式
        style.setFont(font);
        
        // 生成标题样式
        HSSFCellStyle styletitle = workbook.createCellStyle();
        styletitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styletitle.setFillBackgroundColor(HSSFColor.LIME.index);
        HSSFFont fonttitle = workbook.createFont();
        fonttitle.setColor(HSSFColor.BLACK.index);
        fonttitle.setFontHeightInPoints((short) 14);
        fonttitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字体应用到标题样式
        styletitle.setFont(fonttitle);
        
        // 生成表头样式
        HSSFCellStyle stylehead = workbook.createCellStyle();
        stylehead.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        stylehead.setFillBackgroundColor(HSSFColor.AQUA.index);
        HSSFFont fonthead = workbook.createFont();
        fonthead.setColor(HSSFColor.BLACK.index);
        fonthead.setFontHeightInPoints((short) 13);
        fonthead.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字体应用到表头样式
        stylehead.setFont(fonthead);
        
        
        //创建空白表格
        for(int i=0;i<arrList.size()+2;i++){//行数(包括标题)
            HSSFRow row = sheet.createRow(i);
            if(i == 0){
                row.setHeight((short)(15.625*28));
                for(int j=0;j<6;j++){//列数
                    HSSFCell cell = row.createCell(j);
                    cell.setCellStyle(styletitle);
                }
            }else if(i == 1){
                row.setHeight((short)(15.625*26));
                for(int j=0;j<6;j++){//列数
                    HSSFCell cell = row.createCell(j);
                    cell.setCellStyle(stylehead);
                }
            }else{
                for(int j=0;j<6;j++){//列数
                    HSSFCell cell = row.createCell(j);
                    cell.setCellStyle(style);
                }
            }        
        }
        sheet.setColumnWidth(1, (short)(35.7*230));
        sheet.setColumnWidth(2, (short)(35.7*265));
        //填充数据
        sheet.addMergedRegion(new Region(0,(short)0,0,(short)5)); 
        HSSFCell celltitle = sheet.getRow(0).getCell(0);
        //设置标题
        celltitle.setCellValue("矿权到期检查报表");
        //设置表头
        sheet.getRow(1).getCell(0).setCellValue("序号");//
        sheet.getRow(1).getCell(1).setCellValue("许可证号");//
        sheet.getRow(1).getCell(2).setCellValue("申请人");//
        sheet.getRow(1).getCell(3).setCellValue("有效期止");//
        sheet.getRow(1).getCell(4).setCellValue("状态");//
        sheet.getRow(1).getCell(5).setCellValue("天数");//
        for(int i=2;i<arrList.size()+2;i++){//行数
            KQDQJC_Bean bean = new KQDQJC_Bean();
            bean = (KQDQJC_Bean)arrList.get(i-2);
            for(int j=0;j<6;j++){//列数
                switch(j){
                    case 0:
                        sheet.getRow(i).getCell(j).setCellValue(String.valueOf(i-1));//
                        break;
                    case 1:
                        sheet.getRow(i).getCell(j).setCellValue(bean.getXKZH());//
                        break;
                    case 2:
                        sheet.getRow(i).getCell(j).setCellValue(bean.getSQR());//
                        break;
                    case 3:
                        sheet.getRow(i).getCell(j).setCellValue(fun.setTimeFormat(bean.getYXQZ()));//
                        break;
                    case 4:
                        sheet.getRow(i).getCell(j).setCellValue(bean.getZT());//
                        break;
                    case 5:
                        sheet.getRow(i).getCell(j).setCellValue(bean.getTS());//
                        break;
                }    
            }
        }
        try {
            workbook.write(out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
public Boolean dqjc_QueryForTz(String jcrq,String jcts,OutputStream out){
        Boolean isSuccess = false;
        String PagesExl = "";
        Connection dbConn = null;
        int iJcts = Integer.parseInt(jcts);//到期提示天数
        try{
            dbConn = ServiceContext.getLandDbConnection();
            KYQGL_Query query = new KYQGL_Query();
            ArrayList arrList = query.kqdqjc_GetQueryItemAll(dbConn,jcrq,iJcts);
            query.ExportExcel(arrList,out);
            isSuccess = true;
        }catch(Exception e){ 
            logger.error("[导出矿权到期检查报表失败:]" + e);
        }finally{
            ServiceContext.releaseLandDbConnection(dbConn);
        }
        return isSuccess;
}

上面的是java后台方法。

下面是jsp前端页面调用代码(dqjc_QueryResultForTZ.jsp):

<%@ page language="java" pageEncoding="GBK" %>
<%@page import="com.zhongzhi.gis.clientapp.kyqgl.kyqgl.query.KYQGL_DataQuery"%>
<%@page import="java.io.OutputStream"%>
<%
    String jcrq = "";//检查日期 
    jcrq = request.getParameter("jcrq");
    jcrq = java.net.URLDecoder.decode(jcrq,"UTF-8");
    KYQGL_DataQuery query = new KYQGL_DataQuery();
    String fileName = "矿权到期检查.xls";
    OutputStream os = null;
    try{    
        //设置文件头
        response.setContentType("application/msexcel");
        response.setHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("GB2312"),"ISO-8859-1") + ";");
        out.clear();
        out=pageContext.pushBody();
        os = response.getOutputStream(); 
        query.dqjc_QueryForTz(jcrq,"30",os);//设置到期提醒天数为30天
        os.flush();
    }catch(Exception e){
        System.out.println(e);
    }
%>

触发导出excel时不能使用ajax,可以使用下面方法代替之:

function query_dqjc_dataForTz(){
    var jcrq = $("#dcjcrq").val();
    var url =  "pages/kyqgl/kyqgl/util/dqjc_QueryResultForTZ.jsp?jcrq="+jcrq;
    window.location.href = url;
}

 

posted @ 2015-07-08 10:12  aegisada  阅读(1972)  评论(0编辑  收藏  举报