【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>>)

 

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

转载于:https://www.cnblogs.com/tuituji27/p/11309467.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值