ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自定义注解实现Execl的导入导出

2021-01-28 18:35:42  阅读:198  来源: 互联网

标签:Execl 自定义 int clazz rowIndex 导入 new public row


自定义注解实现Execl导入导出

悲伤,我是Java开发程序员,竟然被说是做运维的了。我是开发开发开发!!!!

写个笔记,记录一下以前的execl导入导出功能代码

pom坐标

此处使用常规版本3.9,未使用4.0版本,原因部分API已删除或过时

	<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
	</dependency>

自定义注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelAttribute {
    /** 对应的列名称 */
    String name() default "";

    /** 列序号 */
    int sort();

    /** 字段类型对应的格式 */
    String format() default "";

}

导入工具类

public class ExcelImportUtil<T> {
	//泛型实体clzz
    private Class clazz;
    private Field fields[];
	
    public ExcelImportUtil(Class clazz) {
        this.clazz = clazz;
        fields = clazz.getDeclaredFields();
    }

    /**
     * 基于注解读取excel
     *rowIndex 开始行号
     *cellIndex开始单元格
     */
    public List<T> readExcel(InputStream is, int rowIndex, int cellIndex) {
        List<T> list = new ArrayList<T>();
        T entity = null;
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            Sheet sheet = workbook.getSheetAt(0);
            // 不准确
            int rowLength = sheet.getPhysicalNumberOfRows();
//            System.out.println(sheet.getLastRowNum());
            for (int rowNum = rowIndex; rowNum <= rowLength; rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (row!=null){

                    entity = (T) clazz.newInstance();
                    for (int j = cellIndex; j < row.getPhysicalNumberOfCells(); j++) {
                        Cell cell = row.getCell(j);
                        for (Field field : fields) {
                            if (field.isAnnotationPresent(ExcelAttribute.class)) {
                                field.setAccessible(true);
                                ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);
                                if (j == ea.sort()) {
                                    field.set(entity, covertAttrType(field, cell));
                                }
                            }
                        }
                    }
                    list.add(entity);


                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 类型转换 将cell 单元格格式转为 字段类型
     */
    private Object covertAttrType(Field field, Cell cell) throws Exception {
        String fieldType = field.getType().getSimpleName();
        if ("String".equals(fieldType)) {
            return getValue(cell);
        } else if ("Date".equals(fieldType)) {
            return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(getValue(cell));
        } else if ("int".equals(fieldType) || "Integer".equals(fieldType)) {
            return Integer.parseInt(getValue(cell));
        } else if ("double".equals(fieldType) || "Double".equals(fieldType)) {
            return Double.parseDouble(getValue(cell));
        } else {
            return null;
        }
    }


    /**
     * 格式转为String
     *
     * @param cell
     * @return
     */
    public String getValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                return cell.getRichStringCellValue().getString().trim();
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    Date dt = DateUtil.getJavaDate(cell.getNumericCellValue());
                    return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt);
                } else {
                    // 防止数值变成科学计数法
                    String strCell = "";
                    Double num = cell.getNumericCellValue();
                    BigDecimal bd = new BigDecimal(num.toString());
                    if (bd != null) {
                        strCell = bd.toPlainString();
                    }
                    // 去除 浮点型 自动加的 .0
                    if (strCell.endsWith(".0")) {
                        strCell = strCell.substring(0, strCell.indexOf("."));
                    }
                    return strCell;
                }
            case Cell.CELL_TYPE_BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            default:
                return "";
        }
    }
}

导出工具类

public class ExcelExportUtil<T> {
	 // 开始行号
    private int rowIndex;
    //风格索引,使用哪一行最为模板风格
    private int styleIndex;
    //泛型 实体clzz
    private Class clazz;
    private Field fields[];

    public ExcelExportUtil(Class clazz, int rowIndex, int styleIndex) {
        this.clazz = clazz;
        this.rowIndex = rowIndex;
        this.styleIndex = styleIndex;
        fields = clazz.getDeclaredFields();
    }

    /**
     * 基于注解导出
     *is execl模板
     *objs 数据
     *fileName 文件名
     */
    public void export(HttpServletResponse response, InputStream is, List<T> objs, String fileName) throws Exception {

        XSSFWorkbook workbook = new XSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);

        CellStyle[] styles = getTemplateStyles(sheet.getRow(styleIndex));

        AtomicInteger datasAi = new AtomicInteger(rowIndex);
        for (T t : objs) {
            Row row = sheet.createRow(datasAi.getAndIncrement());
            for (int i = 0; i < styles.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellStyle(styles[i]);
                for (Field field : fields) {
                    if (field.isAnnotationPresent(ExcelAttribute.class)) {
                        field.setAccessible(true);
                        ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);
                        if (i == ea.sort()) {
                            if (field.get(t)==null){
                                continue;
                            }
                            cell.setCellValue(field.get(t).toString());
                        }
                    }
                }
            }
        }
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }
      public CellStyle[] getTemplateStyles(Row row) {
        CellStyle[] styles = new CellStyle[row.getLastCellNum()];
        for (int i = 0; i < row.getLastCellNum(); i++) {
            styles[i] = row.getCell(i).getCellStyle();
        }
        return styles;
    }
}

使用方法

//导出方法
List<User> data=new ArrayList<User>();   
ExcelExportUtil<User> excelExportUtil = new ExcelExportUtil<>(User.class, 1, 0);
        ClassPathResource classPathResource = new ClassPathResource("templates/user.xlsx");
        try ( InputStream inputStream =classPathResource.getInputStream()) {
            excelExportUtil.export(response, data, "导出结果.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
        }

//导入方法
 ExcelImportUtil<User> excelUtil = new ExcelImportUtil<User>(User.class);
 List<User> userList = excelUtil.readExcel(file.getInputStream(), 1, 0);

标签:Execl,自定义,int,clazz,rowIndex,导入,new,public,row
来源: https://www.cnblogs.com/chaoba/p/14341394.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有