java 将接口文档.xlsx文件中的字段转换成驼峰式的变量

案例
/**
变量中文名
/
private String c
I*A;

需要poi包

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

代码

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Configuration
public class test {

    /**
     * 列名所在行
     */
    public static int colHeadRow=2;

    public static final String colEnName="字段名";
    public static int colEn=-1;

    public static final String colCnName="字段中文名";
    public static int colCn=-1;

    public static final String colBackName="备注";
    public static int colBack=-1;

    public static final String varHead="private String ";

    public static final String append1="/**\n";

    public static final String append2=" *";
    public static final String append3=" */\n";

    public static void main(String[] args) throws IOException {
        String filePath="你的.xlsx";
        bufferedWriterMethod(filePath);
    }

    /**
     * 拼接字符串
     */
    public static String appendVar(Map map){
        StringBuffer var=new StringBuffer(append1);
        var.append(append2+map.get("cn")+"\n");
        var.append(append3);
        var.append(varHead+map.get("en")+";\n");
        return var.toString();
    }
    /**
     * 拼接字符串
     */
    public static String appendVar(String str){
        StringBuffer var=new StringBuffer("\n"+append1);
        var.append(append2+str+"\n");
        var.append(append3+"\n");
        return var.toString();
    }

    /**
     * 字符串转换,将数据字段格式换成驼峰格式
     */
    public static String transferString(String old){
        StringBuffer sb=new StringBuffer();
        old=old.toLowerCase(Locale.ROOT);
        for (int i = 0; i < old.length(); i++) {
            char ch =old.charAt(i);
            if (ch=='_'){
                sb.append(Character.toUpperCase(old.charAt(++i)));
                continue;
            }
            sb.append(ch);
        }
        return sb.toString();
    }
    /**
     *
     * check字符是否为A-Z
     */
    public static boolean checkChar(char ch){
        return ch>='A'&&ch<='Z';
    }

    /**
     * 将拼接字符串写入文件
     * @param filePath xslx文件路径
     * @throws IOException
     */
    public static void bufferedWriterMethod(String filePath) throws IOException {
        int index=filePath.lastIndexOf('.');
        String outFilePath=filePath.substring(0,index)+".txt";
        System.out.println(outFilePath);
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outFilePath))) {
            bufferedWriter.write(readXlsxAppend(filePath));
        }
    }

    /**
     * 读取xlsx文件,并将字段和所属拼接好
     */
    public static String readXlsxAppend(String filePath){
        try {
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
            //获取工作簿下sheet的个数
            int sheetNum = xssfWorkbook.getNumberOfSheets();
            StringBuffer content=new StringBuffer();
            //遍历工作簿中的所有数据
            for(int i = 0;i<sheetNum;i++) {
                XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
                int maxRow = sheet.getLastRowNum();
                for (int row = 0; row <= maxRow; row++) {
                    if(sheet.getRow(row)==null){
                        continue;
                    }
                    int maxRol = sheet.getRow(row).getLastCellNum();

                    if (row!=colHeadRow && sheet.getRow(row).getCell(0).toString().length()>0 && !checkChar(sheet.getRow(row).getCell(0).toString().charAt(0))){
                        StringBuffer ts=new StringBuffer();
                        for (int j = 0; j < maxRol; j++) {
                            if (sheet.getRow(row).getCell(j)==null){
                                continue;
                            }
                            String rowAndCol=sheet.getRow(row).getCell(j).toString();
                            if (rowAndCol.length()==0){
                                continue;
                            }
                            ts.append(rowAndCol+" ");
                        }
                        content.append(appendVar(ts.toString()));
                        continue;
                    }
                    if (row>colHeadRow){
                        if (sheet.getRow(row).getCell(colEn).toString().length()==0){
                            continue;
                        }
                    }

                    Map<String,String> map=new HashMap<>();
                    for (int rol = 0; rol < maxRol; rol++){
                        String rowAndCol=sheet.getRow(row).getCell(rol).toString();
                        if (rowAndCol==null || rowAndCol.length()==0){
                            continue;
                        }
                        if (row==colHeadRow){
                            switch (rowAndCol){
                                case colEnName:
                                    colEn=rol;
                                    break;
                                case colCnName:
                                    colCn=rol;
                                    break;
                            }
                        }
                        if (row>colHeadRow){
                            if(rol==colCn){
                                map.put("cn",rowAndCol);
                            }
                            if(rol==colEn){
                                map.put("en",transferString(rowAndCol));
                            }
                        }
                    }
                    if (map.isEmpty()){
                        continue;
                    }
                    String var= appendVar(map);
                    content.append(var);
                }
            }
            return content.toString();
        } catch (IOException e) {
             e.printStackTrace();
        }
        return "eroor";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值