【java工具类】POI导出excel

POI的maven依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>

EasyExcel.java的代码:
public static void POIExcel(List<Map<String,Object>> AllDataList, String excelPath) throws IOException{

        OutputStream out = new FileOutputStream(excelPath);

        String sheetName = "";
        List<List<String>> titles = new ArrayList<List<String>>();
        List<List<String>> sheetData = new ArrayList<List<String>>();
        int size = AllDataList.size();
        Map<String,Object> map = new HashMap<>();

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();

        //超链接样式
        XSSFCellStyle hlink_style = xssfWorkbook.createCellStyle();
        XSSFFont hlink_font = xssfWorkbook.createFont();
        hlink_font.setUnderline(HSSFFont.U_SINGLE);
        hlink_font.setColor(HSSFColor.BLUE.index);
        hlink_style.setFont(hlink_font);

        //循环sheet
        for(int i=0 ; i<size ; i++ ){
            map = AllDataList.get(i);
            sheetName = StringUtils.nvlString(map.get("sheetName"));
            titles = (List<List<String>>) map.get("tabHeader");
            sheetData = (List<List<String>>) map.get("sheetData");

            XSSFSheet sheet = xssfWorkbook.createSheet(sheetName);
            //标题行
            XSSFRow titlerRow = sheet.createRow(0);
            //循环标题
            int t=0;
            for(List<String> ls : titles){
                titlerRow.createCell(t).setCellValue(ls.get(0));
                t++;
            }
            //循环行
            int row = 1;
            for(List<String> ls : sheetData){
                XSSFRow dataRow = sheet.createRow(row);
                //循环列
                for(int col=0;col<ls.size();col++){
                    dataRow.createCell(col).setCellValue(ls.get(col));
                    if(col==0){
                        File tempFile =new File(ls.get(col));
                        String fileName = tempFile.getName();
                        XSSFCell cell = dataRow.getCell(0);
                        cell.setCellFormula("HYPERLINK(\"./"+fileName+"\",\""+fileName+"\")");
                        cell.setCellStyle(hlink_style);
                    }
                }
                row++;
            }
        }

        xssfWorkbook.write(out);
        xssfWorkbook.close();
    }

调用:

//excel生成路径
            String path = "D:\\exportTest\\" + System.currentTimeMillis() + ".xlsx";
            File xlsFile = new File(path);
            //文件夹不存在就创建
            if  (!xlsFile.getParentFile().exists()  && !xlsFile.getParentFile().isDirectory())
            {
                xlsFile.getParentFile().mkdir();
            }
            //生成excel
            EasyExcel.POIExcel(AllDataList,path);

其中,参数AllDataList的传入格式为List<Map<String,Object>>:

格式较复杂,List中每条数据就是一个sheet页内容,主要是sheet页名称(String)、sheet页表头(List<List<String>>)、sheet页数据(List<List<String>>)

 

记录一下,以后用到的话可根据需要的格式酌情修改。

posted @ 2019-08-06 15:50  推土机27  阅读(459)  评论(0编辑  收藏  举报