POI,POI 简介,POI 导出

来源:互联网 发布:第九交响曲 知乎 编辑:程序博客网 时间:2024/05/24 01:38

1. POI 简介 

     POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft

Office 办公套件如 Excel,Word,Powerpoint 等文件的 API.

     微软的Office 办公软件在企业的日常办公中占据着重要的地位,人们已经非常熟悉

Office 的使用。在我们开发的应用系统中,常常需要将数据导出到 Excel 文件中,或者

Word 文件中进行打印。比如移动的话费查询系统中就提供了将话费清单导入到 excel 表

格中的功能。这样在web 应用中,我们在浏览器中看到的数据可以被导出到 Excel 中了。

下面主要介绍如何操作Excel。

2. 下载 POI


     到apache 官方网站下载POI 的jar 包

3. Excel 文件的结构

         一个Excel 文档称为工作簿(worksheet),一个工作簿包含多个工作表(sheet),

每个工作表看起来像一张二维表格,由很多行(row)组成,每行由多个单元格组成(cell).

下面是POI HSSF API 中的类与Excel 结构的对应关系:


4.导出


4.1   jsp页面

function doExportExcel(){
        window.location.href="<%=request.getContextPath()%>/exportExcel.action";

}


<input type="button" value="导出" onclick="doExportExcel();"/>


4.2controller层


public void exportExcel(HttpServletResponse response){
        try {
            //查找用户列表
            List<Movie> userlist = service.listExcel();
            
            //导出
            response.setContentType("application/x-execl");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户列表.xls".getBytes(),"ISO-8859-1"));
            ServletOutputStream outputStream = response.getOutputStream();
            service.exportExcel((List<Movie>) userlist,outputStream);
            if(outputStream != null){
                outputStream.close();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


4.3 service层

    public void exportExcel(List<Movie> userlist, ServletOutputStream outputStream) {

         //调用exportExcel工具类

        Excelutil.exportExcel(userlist, outputStream);
    }


4.4exportExcel工具类


public static void exportExcel(List<Movie> userlist, ServletOutputStream outputStream) {
        try{
            //创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建合并单元格对象
            //起始行号,结束行号,起始列号,结束列号
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0,4);
            
            //头标题样式
            HSSFCellStyle style1 = createCellStyle(workbook,(short) 16);
            //列表题样式
            HSSFCellStyle style2 = createCellStyle(workbook,(short) 13);
            //创建工作表
            HSSFSheet sheet = workbook.createSheet("用户列表");
            //加载合并单元格对象
            sheet.addMergedRegion(cellRangeAddress);
            //设置默认列宽
            sheet.setDefaultColumnWidth(20);
            
            //创建行
            //创建头标提行;并且设置头标题
            HSSFRow row1 = sheet.createRow(0);
            HSSFCell cell1 = row1.createCell(0);
            //加载单元格样式
            cell1.setCellStyle(style1);
            cell1.setCellValue("用户列表");
            
            
            //创建头标提行;并且设置头标题
            HSSFRow row2 = sheet.createRow(1);
            String[] titles = {"电影名","票价","类型","影院","票房","上映时间","地区"};
            
            for (int i = 0; i < titles.length; i++) {
                
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(style2);
                cell2.setCellValue(titles[i]);
                
            }
            
            //操作单元格,将用户列表写入excel
            if(userlist != null){
                
                    for(int j = 0; j < userlist.size();j++){
                       HSSFRow row = sheet.createRow(j+2);
                        //电影名
                        HSSFCell cel0 = row.createCell(0);
                        cel0.setCellValue(userlist.get(j).getName());
                        //票价
                        HSSFCell cell = row.createCell(1);
                        cell.setCellValue(userlist.get(j).getPrice());
                        //类型
                        HSSFCell cel2 = row.createCell(2);
                        cel2.setCellValue(userlist.get(j).getTypename());
                        //影院
                        HSSFCell cel3 = row.createCell(3);
                        cel3.setCellValue(userlist.get(j).getNname());
                        //票房
                        HSSFCell cel4 = row.createCell(4);
                        cel4.setCellValue(userlist.get(j).getTotalprice());
                        //上映时间
                        HSSFCell cel5 = row.createCell(5);
                        cel5.setCellValue(userlist.get(j).getPublicdate());
                        //地区
                        HSSFCell cel6 = row.createCell(6);
                        cel6.setCellValue(userlist.get(j).getCname())
                    }
                }
            
            //输出
            workbook.write(outputStream);
            workbook.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        
    }
    
    /*
     * 创建单元格
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) {
        HSSFCellStyle style = workbook.createCellStyle();
        //水平居中
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //垂直居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        
        //创建字体
        HSSFFont font = workbook.createFont();
        //加粗字体
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints((short) 16);
        //加载字体
        style.setFont(font);
        
        return style;
    }


1 0
原创粉丝点击