java导入导出excel(支持复杂标题的导出和大量数据的导入)

直奔主题,[源码下载地址](https://gitee.com/renjiale/excelUtils)
最近做项目太多的导出excel,每个业务层的代码都几乎一样,对与像我这样有代码洁癖的人是不可忍受的,于是就动手自己封装了一个基于POI3.17的简单的小框架。
在gitee上我有附上word使用教程,在这里简单展示一下测试效果。
导出:
  1. 在实体类中给要导出的属性加注解:
    

在这里插入图片描述
在这里插入图片描述
这里注意两点,hasCildren=true的属性不能为null,子级内的导出属性顺序要连贯。
2. 在导出的方法中调用API:

/**
 	* 导出合同关键字匹配统计列表
 	* @param response
 	* @param vo
 	* @return
 	*/
	@GetMapping("/export")
	@ResponseBody
	public void export(HttpServletResponse response, StatisticsVO vo) throws Exception {
    
    
    		//查询数据
			List<StatisticsVO> list = costKeywordMatchRecordService.
				statisticsCostKeywordMatchRecord(vo);
           ExcelUtils.exportExcel(StatisticsVO.class, list, "测试", "匹配统计.xlsx",response, new CustomizeStyles() {
    
    
             @Override
             protected CellStyle createTitleStyles(Workbook wb) {
    
    
            	CellStyle style = wb.createCellStyle();
            	//设置单元格水平对齐方式
            	style.setAlignment(HorizontalAlignment.CENTER);
            	//设置单元格垂直对齐方式
            	style.setVerticalAlignment(VerticalAlignment.CENTER);
            	//设置右边框
            	style.setBorderRight(BorderStyle.MEDIUM);
            	//设置右边框颜色
            	style.setRightBorderColor(IndexedColors.BLACK.getIndex());
            	//设置左边框
            	style.setBorderLeft(BorderStyle.MEDIUM);
            	//设置左边框颜色
            	style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            	//设置上边框
            	style.setBorderTop(BorderStyle.MEDIUM);
           	 	//设置上边框颜色
            	style.setTopBorderColor(IndexedColors.BLACK.getIndex());
            	//设置下边框
            	style.setBorderBottom(BorderStyle.MEDIUM);
            	//设置下边框颜色
            	style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            	//设置前景颜色
            	style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
            	//设置前景的风格样式
            	style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            	//设置字体样式
            	Font headerFont = wb.createFont();
            	headerFont.setFontName("Arial");
            	headerFont.setFontHeightInPoints((short) 10);
            	headerFont.setBold(true);
            	headerFont.setColor(IndexedColors.BLACK.getIndex());
            	style.setFont(headerFont);
            	return style;
        	 }
            @Override
            protected CellStyle createDataStyles(Workbook wb) {
    
    
            	CellStyle style = wb.createCellStyle();
            	//设置单元格水平对齐方式
            	style.setAlignment(HorizontalAlignment.CENTER);
            	//设置单元格垂直对齐方式
            	style.setVerticalAlignment(VerticalAlignment.CENTER);
            	//设置右边框
            	style.setBorderRight(BorderStyle.THIN);
            	//设置右边框颜色
            	style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置左边框
            	style.setBorderLeft(BorderStyle.THIN);
            	//设置左边框颜色
            	style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置上边框
            	style.setBorderTop(BorderStyle.THIN);
            	//设置上边框颜色
            	style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置下边框
            	style.setBorderBottom(BorderStyle.THIN);
            	//设置下边框颜色
            	style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置字体样式
            	Font dataFont = wb.createFont();
            	dataFont.setFontName("Arial");
            	dataFont.setFontHeightInPoints((short) 10);
            	style.setFont(dataFont);
            	return style;
        	 }
        	 @Override
        	 protected CellStyle createTotalStyles(Workbook wb) {
    
    
            	CellStyle style = wb.createCellStyle();
            	//设置单元格水平对齐方式
            	style.setAlignment(HorizontalAlignment.RIGHT);
            	//设置单元格垂直对齐方式
            	style.setVerticalAlignment(VerticalAlignment.CENTER);
            	//设置右边框
            	style.setBorderRight(BorderStyle.THIN);
            	//设置右边框颜色
            	style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置左边框
            	style.setBorderLeft(BorderStyle.THIN);
            	//设置左边框颜色
           	 	style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置上边框
            	style.setBorderTop(BorderStyle.THIN);
            	//设置上边框颜色
            	style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置下边框
            	style.setBorderBottom(BorderStyle.THIN);
            	//设置下边框颜色
            	style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
            	//设置字体样式
            	Font totalFont = wb.createFont();
            	totalFont.setFontName("Arial");
            	totalFont.setFontHeightInPoints((short) 10);
            	style.setFont(totalFont);
            	return style;
        	 }
    	   });
	}

这里最后一个参数是一个对象,是设置导出表格的样式的,可以不传这个参数。或者为了你项目的导出风格一致,可以创建一个子类设置样式。这里为了演示所以写了这么多,实际开发中不会有这么多行代码。
最后看一下导出的样子:
在这里插入图片描述
导入

  1. 要导入的文档
    在这里插入图片描述 2. 调用导入API(这里只是在main方法测试了一下)
public static void main(String[] args) throws IOException,InvalidFormatException {
    
    
        File f = new File("C:\\Users\\Administrator\\Desktop\\匹配库.xlsx");
        InputStream is = new FileInputStream(f);
        ImportResult result =ExcelUtils.importExcel(is,CostKeywordMatchLibrary.class, 1, new ExcelImportHandler<CostKeywordMatchLibrary>() {
    
    
            @Override
            public boolean eachColumnCallBack(Sheet sheet, int rowNum, Row row, int column,
 				Cell cell, CostKeywordMatchLibrary vo, Object value) {
    
    
                System.out.println("执行每列的回调,当前是第" + (rowNum + 1) + "行" + (column + 1) + "列,得到值为:"+value);
                switch (column){
    
    
                    case 0:
                        String type = (String)value;
                        switch (type){
    
    
                            case "合同编号": type = "1";break;
                            case "合同名称": type = "2";break;
                            case "合同金额": type = "3";break;
                            case "合同开始时间": type = "4";break;
                            case "合同结束时间": type = "5";break;
                            default: return false;
                        }
                        vo.setKeywordType(type);break;
                    case 1:vo.setKeywordCode((String)value);break;
                    case 2:vo.setKeywordName((String)value);break;
                    case 3:
                        String enabledState = (String)value;
                        switch (enabledState){
    
    
                            case "未启用": enabledState = "0";break;
                            case "已启用": enabledState = "1";break;
                            default: return false;
                        }
                        vo.setEnabledState(enabledState);break;
                    case 4:vo.setUpdateUser((String)value);break;
                    default:return false;
                }
                return true;
            }
            @Override
            public CostKeywordMatchLibrary eachRowCallBack(Sheet sheet, int rowNum, Row row, 			CostKeywordMatchLibrary vo) {
    
    
                vo.setCreateDate(new Date());
                vo.setCreateUser("窝嫩叠");
                vo.setUpdateDate(new Date());
                vo.setOrgCode("组织机构代码");
                vo.setModeName("模块名称");
                vo.setModeId("模块id");
                vo.setLibraryNote("这里是备注");
                System.out.println("执行每行的回调,当前是第" + (rowNum + 1) + "行,处理对象:" + vo.toString());
                return vo;
            }
            @Override
            public boolean batchSaveCallBack(List<CostKeywordMatchLibrary> cacheList) {
    
    
                System.out.println("执行用户保存的回调,在这里模拟保存数据,集合中的数据为:");
                for (CostKeywordMatchLibrary library : cacheList) {
    
    
                    System.out.println(library.toString());
                }
                return true;
            }
        });
        System.out.println(result.toString());
    }

执行结果:
在这里插入图片描述
最后,此框架使用的日志框架为log4j,默认的日志级别为info,想看更细节的日志可以在你的配置文件中这样配置:

logging.level.org.kangjia=debug
哦!对了,我源码里面几乎每一步都有详细的注释,有兴趣的同学可以研究研究。可以多提提意见。

猜你喜欢

转载自blog.csdn.net/weixin_53514362/article/details/120159128