接口文件转化实体脚本

脚本代码

import javafx.scene.control.Cell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;

public class APITransformer {
    private static final String path ="C:\\Users\\tony\\Desktop\\study\\excreice\\springcloud\\rebbion\\src\\main\\java\\theBean";
    private static StringBuilder sb = new StringBuilder();
    public static void main(String args[]) throws IOException {
        String url;
        //C:\Users\tony\Desktop\study\excreice\springcloud\rebbion\src\main\resources\templates\STapplication.xls
        File file =new File("C:\\Users\\tony\\Desktop\\study\\excreice\\springcloud\\rebbion\\src\\main\\resources\\templates\\STapplication.xls");
        InputStream is = new FileInputStream(file);
        Workbook workBook = new HSSFWorkbook(is);
        Sheet sheet = workBook.getSheetAt(0);
        StringBuilder getSb = null;
        Row row;
        Cell cell;
        int sheetCount =1;
        int startRow=0;
        while(true){
            row = sheet.getRow(1);
            startRow =3;
            if (row!=null&& row.getCell(0).getStringCellValue()!=null) {
                String entityName = row.getCell(0).getStringCellValue();
                if(entityName==null||"".equals(entityName))break;
                File classFile = new File(path+entityName+".java");
                FileOutputStream in = new FileOutputStream(classFile);
                System.out.println(entityName);
                append("package com.augurit.aplanmis.integration.guangdong.shuguang.rest.domain.STworkitem;");
                append("import lombok.Data;");
                append("import javax.persistence.*;");
                append("import java.math.BigDecimal;");
                append("/**\n" +
                        " * <author>          <time>          <version>          <desc>\n" +
                        " * Toshar           20191001           1.0              描述\n" +
                        " */");
                append("@Data");
                sb.append("public class ");
                append(entityName+"{");
                getSb = new StringBuilder();
                while(true){
                    if(sheet.getRow(startRow)==null){
                        break;
                    }
                    String desc = sheet.getRow(startRow).getCell(0).getStringCellValue();
                    String OriginalType = sheet.getRow(startRow).getCell(1).getStringCellValue();
                    String type = typeChange(OriginalType);
                    String field = sheet.getRow(startRow).getCell(2).getStringCellValue();
                    append("\r\n\tprivate "+type+" "+field+";  //"+desc);
                    getSb.append("\r\n\tpublic "+type+" get"+field+"(){\r\n");
                    getSb.append("\t\treturn "+field+";\r\n");
                    getSb.append("\t}\r\n");
                    getSb.append("\r\n\tpublic void set"+field+"("+type+" "+field+"){\r\n");
                    getSb.append("\t\tthis."+field+"="+field+";\r\n");
                    getSb.append("\t}\r\n");
                    startRow++;
                }
                append(getSb.toString());
                append("}");
                in.write(sb.toString().getBytes());
                sb =new StringBuilder();
            }else{
                break;
            }
            sheet = workBook.getSheetAt(sheetCount);
            sheetCount++;
        }
    }
    public static String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    private static void append (String str){
        sb.append(str).append("\r\n");
    }
    private static String typeChange(String OriginalType){
        String type=null;
        switch (OriginalType){
            case "字符型":
                type="String";
                break;
            case "日期型":
                type="Date";
                break;
            case "数字型":
                type="Integer";
                break;
        }
        return type;
    }
    }

使用依赖

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

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

转换模板

在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值