java接口文档自动实体类生成表格功能

做程序员很难,难在哪?我感觉首先起名字(各种变量实体类的名字);其次是写文档(重复着Ctrl+C Ctrl+V,还要绞尽脑汁搜刮出各种修饰描述),简直不胜其烦
这不,项目完结又开始写接口文档了,看到对于实体类的变量注释我就头大,网上有一些工具不适合在内网使用,终于在用原始方法写了几天后我决定写个方法提高一下效率

目的

将实体类文件转换为想要的excel文件格式

编码过程

目标文件格式:

    /**
     * id
     */
    private Integer id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 用户密码
     */
    private String pwd;

预期实现样式:

参数名称 类型 参数描述
id Integer id
username String 用户名
pwd String 用户密码

配置的依赖:

 <dependency>
     <groupId>org.apache.poigroupId>
      <artifactId>poiartifactId>
      <version>3.14version>
  dependency>
  <dependency>
      <groupId>org.apache.poigroupId>
      <artifactId>poi-ooxmlartifactId>
      <version>3.14version>
  dependency>

实现代码:

    /**
     * java实体类文件转为excel
     * @param inPath 目标文件路径
     * @param outPath 输出文件路径
     * @param fileName 文件名称(此处指输出文件名)
     * @throws IOException 使用时记得捕获异常
     */
    public void javaToExcel(String inPath,String outPath,String fileName)throws IOException {
        //内存中构造Workbook对象
        Workbook wb = new XSSFWorkbook();
        //添加Sheet
        Sheet sheet = wb.createSheet("注释");
        //属性注释
        String anno = new String();
        //属性计数
        Integer num = 0;

        File infile = new File(inPath);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(infile));
        BufferedReader br = new BufferedReader(reader);
        //读取的每行信息
        String line = "";
        line = br.readLine();
        //循环读取每条属性
        while(line != null) {
            line = line.trim();
            if(line.contains("/**")||line.contains("*/")){
            }else if(line.startsWith("*")){
                anno=line;
            }else if(line.startsWith("private")){
                //将当前代码拆分成数组
                String[] obj = line.replaceFirst(";","").split(" ");
                System.out.println(line);
                if(obj.length==3){
                    //3.添加行
                    Row row = sheet.createRow(num);
                    row.createCell(0).setCellValue(obj[2]);
                    row.createCell(1).setCellValue(obj[1]);
                    row.createCell(2).setCellValue(anno.substring(1));
                    num++;
                }
            }
            line = br.readLine();
        }
        //写出到硬盘
        OutputStream os = new FileOutputStream(new File(outPath+"\\"+fileName+".xlsx"));
        //输出和关流
        wb.write(os);
        wb.close();
    }

该代码基本就是Io流读取文件和poi写入表格,主要就是处理思路

你可能感兴趣的:(文档,java)