0
点赞
收藏
分享

微信扫一扫

java导出Excle表格

月白色的大狒 2022-02-15 阅读 63

世间万物,始于心,终于行

想要了解本章内容,你得先了解自定义注解.
目录

java中元注解
背景
所用依赖
逻辑代码块
测试

java中元注解

接下来就是自定义注解

package com.yuan.testExcel;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 *  excel 表格 列名注解
 *
 * @author yuanzixiang
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelRow {

    /**
     *  Excel 对应列名
     * @return
     */
    String name();

    /**
     *  excel 列名备注
     * @return
     */
    String note() default "";
}

ExcelRow 该注解使用在属性上.

背景
最近在面试的时候被问到有没有用过java操作Excel,直接就是当场尬住,所以这几天狠狠研究了一下这个东西。

所用依赖
如果版本有不同有冲突,请自行去maven仓库拉取

  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.1.1</version>
  </dependency>

逻辑代码块

/**
     *  workbook
     * @param titleList
     * @return
     */
    public static HSSFWorkbook getWorkBook(List<String> titleList){
        //第一步,创建一个workbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 一个sheet
        HSSFSheet sheet = wb.createSheet("工作表名");

       // 第一行 标题
        HSSFRow rowTitle = sheet.createRow(0);

        // 设置标题
        for (int i = 0; i < titleList.size(); i++) {
            HSSFCell cell = rowTitle.createCell(i);
            cell.setCellValue(titleList.get(i));
        }
        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
        /*sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));
        sheet.addMergedRegion(new CellRangeAddress(titleList.size()-1,titleList.size()-1,titleList.size()-1,titleList.size()+1));*/
        return wb;
    }
public static <T> HSSFWorkbook getWorkBook(List<String> titleList , List<T> dataList) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        if (CollectionUtils.isNotEmpty(dataList)) {
            T t1 = dataList.get(0);
            Class<?> t1Class = t1.getClass();
            EnableExcel enableExcel = t1Class.getAnnotation(EnableExcel.class);
            if (enableExcel == null) {
                throw new IllegalArgumentException("EnableExcel 注解没有在实体类启用");
            }
            Field[] fields = t1Class.getDeclaredFields();
            if (fields != null && fields.length > 0) {
                Map<String , Integer> titleMap = new HashMap<>(titleList.size()); // 存放属性名称对应的下标

                int fieldExcelSize = 0; // 类中ExcelRow 注解的数量
                for (Field field : fields) {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    ExcelRow excelRow = field.getAnnotation(ExcelRow.class);
                    if (excelRow != null) {
                        String name = excelRow.name();
                        if (StringUtils.isEmpty(name)) {
                            throw new IllegalArgumentException("ExcelRow 注解name属性不能为空");
                        }

                        int index = titleList.indexOf(name.trim());
                        if (index != -1) {
                            fieldExcelSize++;
                            titleMap.put(fieldName , index);
                        }
                    }
                }

                if (!(titleList.size() == fieldExcelSize)) {
                    throw new IllegalArgumentException("ExcelRow 注解name属性对应的列数不对");
                }

                HSSFWorkbook workBook = getWorkBook(titleList);
                HSSFSheet sheet = workBook.getSheetAt(0);

                for (T t : dataList) {
                    int lastRowNum = sheet.getLastRowNum();
                    HSSFRow row = sheet.createRow(lastRowNum + 1);
                    BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
                    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                        String fieldName = propertyDescriptor.getName();
                        if (titleMap.containsKey(fieldName)) {
                            Method readMethod = propertyDescriptor.getReadMethod();
                            if (readMethod != null) {
                                Class<?> returnType = readMethod.getReturnType();
                                String simpleName = returnType.getSimpleName();

                                Object invoke = readMethod.invoke(t);
                                String value = "";
                                // 可以根据不同的类型返回不同的数据
                                if ("date".equalsIgnoreCase(simpleName)) {
                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    if (invoke != null) {
                                        value = simpleDateFormat.format(invoke);
                                    }
                                }
                                if (invoke != null && "".equals(value)) {
                                    value = invoke.toString();
                                }
                                row.createCell(titleMap.get(fieldName)).setCellValue(value);
                            }
                        }
                    }
                }
                return workBook;
            }
        }
        return null;
    }

测试
/**
* 测试Excel表格导出
* @param args
*/
public static void main(String[] args) throws IllegalAccessException, IntrospectionException, InvocationTargetException, IOException {

            List<String> titleList = new ArrayList<>();
            titleList.add("哈");
            titleList.add("哈哈");
            titleList.add("Date");

            List<ExcelEntity> userEntities = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                ExcelEntity excelEntity = new ExcelEntity();
                excelEntity.setUsername("username"+i);
                excelEntity.setPassword("password"+i);
                excelEntity.setCreateDate(new Date());
                userEntities.add(excelEntity);
            }

            HSSFWorkbook workBook = ExcelUtils.getWorkBook(titleList, userEntities);
            if (workBook != null) {
                File file = new File("D:\\1.xlsx");
                workBook.write(file);
            }
            System.out.println("导出成功");

        }
举报

相关推荐

0 条评论