Skip to content

Instantly share code, notes, and snippets.

@JonasGao
Last active June 12, 2019 06:07
Show Gist options
  • Save JonasGao/8dce1653fce207c790436be8b4c2809c to your computer and use it in GitHub Desktop.
Save JonasGao/8dce1653fce207c790436be8b4c2809c to your computer and use it in GitHub Desktop.
用来从 excel 生成 pojo
package com.tools;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import static javax.lang.model.SourceVersion.isKeyword;
public class App {
public static void main(String[] args) throws IOException {
final Workbook workbook = WorkbookFactory.create(new File("C:\\Users\\Jonas\\Desktop\\工作簿1.xlsx"));
final ModelBuilder builder = new ModelBuilder("com.example.cainiao.apidemo.LogisticsInterface");
for (Sheet sheet : workbook) {
FieldDescription fieldDescription = null;
for (Row cells : sheet) {
String fieldName = getFieldName(cells);
if (fieldName != null) {
if (fieldDescription != null) {
builder.property(fieldDescription);
}
fieldDescription = new FieldDescription();
fieldDescription.setName(fieldName);
}
if (fieldDescription != null) {
fieldDescription.addDocLine(getDocLine(cells));
final String type = getType(cells);
if (type != null) {
fieldDescription.setType(type);
}
String require = getRequire(cells);
if (require != null) {
fieldDescription.setRequire(require);
}
}
}
}
final Path path = Paths.get("./LogisticsInterface.java");
Files.writeString(path, builder.toString());
}
private static String getRequire(Row cells) {
final String cellStringValue = getCellStringValue(cells, 2);
if (cellStringValue != null)
return cellStringValue.trim();
return null;
}
private static String getType(Row cells) {
return getCellStringValue(cells, 1);
}
private static String getDocLine(Row cells) {
return getCellStringValue(cells, 3);
}
private static String getFieldName(Row cells) {
return getCellStringValue(cells, 0);
}
private static String getCellStringValue(Row cells, int i) {
final Cell cell = cells.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
if (cell == null) {
return null;
}
if (cell.getCellType() != CellType.STRING) {
return null;
}
return cell.getStringCellValue();
}
public static class FieldDescription {
private String name;
private String type;
private List<String> docLines = new ArrayList<>();
private boolean _enum = false;
private String require = "否";
private String fieldName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCaseName() {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void addDocLine(String docLine) {
if (docLine == null) {
return;
}
if (docLine.contains("枚举值")) {
_enum = true;
}
docLines.add(docLine);
}
public List<String> getDocLines() {
return docLines;
}
public boolean isEnum() {
return _enum;
}
public String getRequire() {
return require;
}
public void setRequire(String require) {
this.require = require;
}
public String getFieldName() {
if (this.fieldName == null) {
String name = getName();
if (isKeyword(name)) {
name = "x" + name;
}
this.fieldName = name;
}
return this.fieldName;
}
}
public static class ModelBuilder {
private final StringBuilder stringBuilder = new StringBuilder();
private final List<FieldDescription> properties = new ArrayList<>();
private int tab = 0;
private boolean newLine = true;
public ModelBuilder(String className) {
String packageName = null;
if (className.contains(".")) {
packageName = className.substring(0, className.lastIndexOf("."));
className = className.substring(className.lastIndexOf(".") + 1);
}
if (packageName != null) {
a("package ").a(packageName).end();
}
line();
a("public class ").a(className).open();
line();
}
private ModelBuilder end() {
a(";").line();
return this;
}
private ModelBuilder line() {
stringBuilder.append("\n");
newLine = true;
return this;
}
private ModelBuilder a(String content) {
if (newLine) {
for (int i = 0; i < tab; i++) {
stringBuilder.append(" ");
}
newLine = false;
}
stringBuilder.append(content);
return this;
}
private ModelBuilder open() {
a(" {").line();
tab++;
return this;
}
private ModelBuilder close() {
tab--;
a("}").line();
return this;
}
@Override
public String toString() {
for (FieldDescription property : properties) {
if (!property.isEnum()) {
continue;
}
enumValueInterface(property);
line();
}
for (FieldDescription property : properties) {
field(property);
line();
}
for (FieldDescription property : properties) {
setter(property);
line();
getter(property);
line();
}
close();
return stringBuilder.toString();
}
private void enumValueInterface(FieldDescription property) {
javaDoc(property);
a("public interface ").a(property.getCaseName()).open();
line();
a("/* 请自行处理").line();
for (String docLine : property.getDocLines()) {
a(docLine).line();
}
a(" */").line();
line();
close();
}
private void field(FieldDescription property) {
javaDoc(property);
a("private ").a(property.getType()).a(" ").a(property.getFieldName()).end();
}
private void javaDoc(FieldDescription property) {
a("/**").line();
a(" * 是否必填:").a(property.getRequire()).line();
a(" * ").line();
for (String docLine : property.getDocLines()) {
if (docLine == null || docLine.isEmpty()) {
a(" * ").line();
continue;
}
final int maxDocLen = 60;
if (docLine.length() <= maxDocLen) {
a(" * ").a(docLine).line();
} else {
int len = docLine.length();
int startIndex = 0;
for (int end = maxDocLen; startIndex < len; startIndex = end, end += maxDocLen) {
final String substring = docLine.substring(startIndex, end > len ? len : end);
a(" * ");
if (startIndex > 0) {
a(" ");
}
a(substring).line();
}
}
}
a(" */").line();
}
private void getter(FieldDescription property) {
a("public ").a(property.getType()).a(" get").a(property.getCaseName()).a("()").open();
a("return this.").a(property.getFieldName()).end();
close();
}
private void setter(FieldDescription property) {
a("public void set").a(property.getCaseName()).a("(").a(property.getType()).a(" ").a(property.getFieldName()).a(")").open();
a("this.").a(property.getFieldName()).a(" = ").a(property.getFieldName()).end();
close();
}
public void property(FieldDescription fieldDescription) {
properties.add(fieldDescription);
}
}
}

|名称|类型|是否必填|文档说明| |actionType|String|是|xxxx|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment