脚本代码
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;
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>
转换模板