POI 导出Excel 实例记录

    public static void  main(String[] asge){
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("rowKey1","rowKey1");
        map.put("rowKey2","rowKey2");
        map.put("rowKey3","rowKey3");
        map.put("rowKey4","rowKey4");

        list.add(map);
        downUserList(list);

    }

    public static XSSFWorkbook createUserListExcel(List<Map<String,Object>> listresult){
        // 1.创建HSSFWorkbook,一个HSSFWorkbook对应一个Excel文件
        XSSFWorkbook wb = new XSSFWorkbook();
        // 2.在workbook中添加一个sheet,对应Excel文件中的sheet
        XSSFSheet sheet = wb.createSheet("sheet1");
        
        //添加单元格 备注信息逻辑 业务不需要可以忽略 str;
        //获取poi的一个工具类
        XSSFCreationHelper creation = wb.getCreationHelper();
        //设置一个锚点 也就是单元格右上角红色三角
        XSSFClientAnchor clientAnchor = creation.createClientAnchor();
        //设置一个画布 就是用来显示注释信息的画布
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
        //设置一个单元格注释
        XSSFComment cellComment = drawing.createCellComment(clientAnchor);
        //声明一个富文本格式 用来设置备注信息字体;
        XSSFRichTextString test = creation.createRichTextString("备注信息");
        //也可设置字体样式
        cellComment.setString(test);
        
        //end

        // 3.设置表头,即每个列的列名
        String[] titel = {"rowName1","rowName2","rowName3","rowName4"};
        // 3.1创建第一行
        XSSFRow row = sheet.createRow(0);
        // 此处创建一个序号列
        row.createCell(0).setCellValue("序号");
        // 将列名写入

        Font redFont =  wb.createFont();
        redFont.setColor(Font.COLOR_RED);// 红色
        int j=0;

        // 写入正式数据
        for (int i = 0; i < listresult.size(); i++) {
            // 创建行
            row = sheet.createRow(i+1);
            // 序号
            row.createCell(0).setCellValue(i+1);
            // 银行名称
            row.createCell(1).setCellValue(listresult.get(i).get("rowKey1").toString());
            sheet.autoSizeColumn(1, true);
            // 业务类型
            row.createCell(2).setCellValue(listresult.get(i).get("rowKey2").toString());
            // 异常信息
            row.createCell(3).setCellValue(listresult.get(i).get("rowKey3").toString());
            // 数量
            XSSFCell cell = row.createCell(4);
            //设置单元格富文本样式;
            XSSFRichTextString richString = new XSSFRichTextString( "Hello*" );
            //richString.applyFont( 0, 6,  blueFont );
            //设置字体富文本字体样式 可以根据下标设置多个 字体样式 hello word 可以让hello 显示红色 其他显示蓝色 根据字符下标;
            richString.applyFont( 4, 6, redFont );

            cell.setCellValue(richString);
            //设置单元格注释信息
            cell.setCellComment(cellComment);
        }
        /**
         * 上面的操作已经是生成一个完整的文件了,只需要将生成的流转换成文件即可;
         * 下面的设置宽度可有可无,对整体影响不大
         */
        // 设置单元格宽度
        int curColWidth = 0;
        for (int i = 0; i <= titel.length; i++) {
            // 列自适应宽度,对于中文半角不友好,如果列内包含中文需要对包含中文的重新设置。
            sheet.autoSizeColumn(i, true);
            // 为每一列设置一个最小值,方便中文显示
            curColWidth = sheet.getColumnWidth(i);
            if(curColWidth<2500){
                sheet.setColumnWidth(i, 2500);
            }
            // 第3列文字较多,设置较大点。
            sheet.setColumnWidth(3, 8000);
        }
        return wb;
    }


//下载excel 文件
    private static String downUserList(List<Map<String,Object>> listresult){
        // getTime()是一个返回当前时间的字符串,用于做文件名称
        String name ="ExcelTest";
        //  csvFile是我的一个路径,自行设置就行
        String filePath  = "D:/GitRepository/P2M.PURE/com.sysware.p2m.core/com/sysware/p2m/core/office/excel/excelTemplate/excelTemplate166.xls";
        // 1.生成Excel
        XSSFWorkbook userListExcel = createUserListExcel(listresult);
        try{
            // 输出成文件
            File file = new File(filePath);
            if(file.exists() || !file.isDirectory()) {
                file.mkdirs();
            }
            // TODO 生成的wb对象传输
            FileOutputStream outputStream = new FileOutputStream(new File(s));
            userListExcel.write(outputStream);
            outputStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return name;
    }
原文地址:https://www.cnblogs.com/wangfl/p/10953941.html