POI实现报表的导出

走远了吗. 提交于 2019-12-18 03:07:11

/**

*文件导出方法

*@param resource List<String[ ]> 集合类型,要导出的具体数据结合。

*@param outputStream 输出流,输出的excel文件保存的具体位置。

public void exportExcel(List<String[ ]> resource,OutputStream outputStream){

       //创建一个内存的excel对象

       HSSFWorkbook workbook =new HSSFWorkbook();

       //创建一个表格

       HSSFSheet sheet =new HSSFSheet(“sheet1”);

       //创建表头

      //获取表头内容

       String[ ]  headerStr =resource.get(0);

       HSSFRow headerRow =sheet.createRow(0);

       //设置列宽

       for(int i=0;i <headerStr.length;i++){

              sheet.setColumWidth(i,5000);

       }

       //设置头单元格样式

        HSSFCellStype headserStyle =work.createCellStype();

        headerStyle.setAlignment(HSSFCellStype.ALIGN_CENTER);  //水平居中

        //设置字体

         HSSFont headerFont = workbook.createFont();

         headerFont.steColor(HSSFColor.VIOLET.index);    //VIOLET 是优雅的紫罗兰色

         headerFont.setFontName("宋体");

         headerStyle.setFont(headerFont);

 

          //定义表头内容

         for(int i < =0; i<headerStr.length; i++){

                //创建一个单元格

               HSSFCell  headerCell =headerRow.createCell(i);

               headerCell.setCellStyle(headerStyle);

                headerCell.setCellValue(headerStr[i]);

         }

         //表体内容

         //样式

        HSSFCellStype bodyStyle=work.createCellStype();

        bodyStyle.setAlignment(HSSFCellStype.ALIGN_CENTER);  //水平居中

        //设置字体

         HSSFont bodyFont= workbook.createFont();

         bodyFont.steColor(HSSFColor.BLUE.index);

         bodyFont.setFontName("微软雅黑");

         bodyFont.setFont(headerFont);

         for(int row =1; row <resource.size(); row++){

                 //输出行数据

                 String[ ] temp =resource.get(row);

                 //创建行

                 HSSFRow bodyRow =sheet.createRow(row);

                 //循环创建列

                 for(int cell =0; cell< temp.length; cell++){

                      HSSFCell bodyCell =bodyRow.createCell(cell);

                       bodyCell.setCellStyle(bodyStyle);

                       body.setCellValue(temp[cell]);

                 }

         }

         //将内存创建的excel对象,输出到文件中

         workbook.write(outputStream);

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!