基于对SSM框架的掌握之后,继而对springboot的搭建 。
初始的工作就是对重装系统的电脑进行所有的环境配置 及其之后对常用框架的搭建。之后对springboot框架的持续学习。
环境搭建完成之后的内容,就是——
1、熟悉使用EasyPoi工具
2、使用EasyPoi读取以上两个文档的内容
3、获取文档中"{{}}"包裹的变量名并生成Java文件,例如Model.java
Model.java:
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
import java.time.LocalDate;
@Data
@Accessors(chain = true)
public class BudgetAdjustDeptReportModel {
@ApiModelProperty(value = "年度")
private int year = LocalDate.now().getYear();
@ApiModelProperty(value = "部门")
private String dept = "-";
@ApiModelProperty(value = "季度")
private String quarter;
@ApiModelProperty(value = "季度")
private BigDecimal ybggIncome;
}
接下来就是持续了解 部分掌握easyPoi工具——
前端传递参数,接受返回的文件并下载到本地
后端:接受传递过来的参数,查找出对应的实体类,通过easypoi提供的模板和控制类,写入文件
模板:根据要写入的值确定word模板,并将其放入启动类所在包的resource下,这里的模板,就联想到
thymeleaf模板。
实体:实体类添加@Escel相关注释
确定word文档模板:
{{area}} 双括号内放变量或常量
{{fd:(Time;yyyy-MM-dd)}} fd:(obj;yyyy-MM-dd)格式化时间格式
{{fe:List t}} fe:obj t t为别名
为了提取word文档的指定内容 利用了字符串的截取及其替代——replaceAll的参数是regex,即基于正则表达式的替换,比如,可以通过replaceAll(“d”, “*”)把一个字符串所有的数字字符都换成星号;replaceAll是一个非常好用的方法 基于正则表达式的运用能更好的实现提取。同时需要注意的是 一些被替代的需要转义字符的使用。
比如:
sub = sub.replaceAll("[^a-z^A-Z]", "");
"[^a-z^A-Z]" 表示非英文字母的
表示提取纯英文。
第一份任务安排是读取word文档 自动生成JavaBean
.docx:
其中 字段信息不仅于此;所以手动去创建类及属性 并不实际:
编写类实现自动生成:
package com.boot.demo.output;
import com.boot.demo.service.EntityProperty;
import com.boot.demo.service.FieldProperty;
import com.boot.demo.service.ParseConfig;
import com.boot.demo.service.WordPOI;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class WordPoiImpl {
public static void main(String[] args) throws IOException {
WordPOI wordPOI=new WordPOI();
String BigDecimal="BigDecimal";
List list = new ArrayList<>();
List fieldProperties=new ArrayList<>();
ParseConfig parseConfig=new ParseConfig();
try {
OPCPackage opcPackage = POIXMLDocument.openPackage("C:\Users\Redic\IdeaProjects\demo\src\main\resources\word\ystz.docx");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
String text2007 = extractor.getText();
String str1=text2007.replaceAll("\{\{","}}");
String subSentences[] = str1.split("\}\}");
String allStr="";
for (String sub : subSentences) {
sub=sub.replaceAll("[^a-z^A-Z]", "");
allStr+=sub+" ";
}
String subNew[]=allStr.split(" ");
for (String no:subNew) {
if(!no.isEmpty())
list.add(no);
}
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j
if (list.get(i).equals(list.get(j)))
list.remove(i);
}
}
// Iterator it = list.iterator();测试是否 去除
// while (it.hasNext()) {
// System.out.println(it.next());
// }
} catch (Exception e) {
e.printStackTrace();
}
FieldProperty fieldProperty1=new FieldProperty(list.get(0),"int");
fieldProperties.add(fieldProperty1);
fieldProperties.add(new FieldProperty(list.get(1),"String"));
for(int i=2;i
fieldProperties.add(new FieldProperty(list.get(i),BigDecimal));
}
EntityProperty entityProperty=new EntityProperty("com.boot.demo.pojo","BudgetAdjustDeptReportModel",fieldProperties);
wordPOI.generateEntity(entityProperty,false,"C:\Users\Redic\IdeaProjects\demo\src\main\java\com\boot\demo\pojo",parseConfig);
}
}
FieldProperty:
package com.boot.demo.service;
public class FieldProperty {
private String name;
private String type;
private String desc;
public FieldProperty()
{
}
public FieldProperty(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "FieldInfo{" +
"name='" + name + ''' +
", type=" + type +
", desc='" + desc + ''' +
'}';
}
}
EntityProperty:
package com.boot.demo.service;
import java.util.List;
public class EntityProperty {
private String packageName;
private String className;
private List filedPropertyList;//字段属性集合
public EntityProperty() {
}
public EntityProperty(String packageName, String className) {
this.packageName = packageName;
this.className = className;
}
public EntityProperty(String packageName, String className, List filedPropertyList) {
this.packageName = packageName;
this.className = className;
this.filedPropertyList = filedPropertyList;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List getFiledPropertyList() {
return filedPropertyList;
}
public void setFiledPropertyList(List filedPropertyList) {
this.filedPropertyList = filedPropertyList;
}
@Override
public String toString() {
return "EntityProperty{" +
"packageName='" + packageName + ''' +
", className='" + className + ''' +
", filedPropertyList=" + filedPropertyList +
'}';
}
}
wordPOI:
public void generateEntity(EntityProperty entityProperty, boolean isDoc, String outPath,ParseConfig parseConfig) throws IOException{
String className = entityProperty.getClassName();
String javaName = className + ".java";
StringBuffer buffer = new StringBuffer();
buffer.append("package "+ entityProperty.getPackageName()+";nn");
//是否使用Lobmok
boolean useLombok = parseConfig.isUseLombok();
//是否生成get和set方法
boolean genGetterAndSetter = parseConfig.isGenGetterAndSetter();
//是否生成toString方法
boolean genToString = parseConfig.isGenToString();
//是否包含泛型
boolean genericity = false;
//是否导入List
boolean importList = false;
//是否导入Set
boolean importBigDecimal = true;
//是否导入Map
boolean importMap = false;
List filedPropertyList = entityProperty.getFiledPropertyList();
if(filedPropertyList !=null ){
StringBuffer bufferField = new StringBuffer();
StringBuffer bufferGetSet = new StringBuffer();
StringBuffer bufferToString = new StringBuffer();
//toString开头
if(genToString && !useLombok){
bufferToString.append("t@Overriden")
.append("tpublic String toString(){n")
.append("ttreturn ")
.append(""").append(className).append("{" + n");
}
int index = 0;
int size = filedPropertyList.size();
for(FieldProperty filedProperty : filedPropertyList){
bufferField.append("tn");
bufferField.append("tprivate " + filedProperty.getType() + " " + filedProperty.getName() + ";n");
String fieldType = filedProperty.getType();
if(fieldType!=null){
//遍历是否包含泛型
if(fieldType.contains("") || fieldType.equals("T")){
genericity = true;
}
//遍历是否包含集合
if(fieldType.startsWith("List")){
importList = true;
}else if(fieldType.startsWith("Set")){
importBigDecimal = true;
}else if(fieldType.startsWith("Map")){
importMap = true;
}
}
if(!useLombok){//只有当不使用Lombok时,才会根据genGetterAndSetter 和 genToString 来判断是否生成get、set、toString等方法
String fieldName = filedProperty.getName();
//是否生成get和set方法
if(genGetterAndSetter){
String getterName = fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
//Generate getter
if("boolean".equalsIgnoreCase(fieldType)){
bufferGetSet.append("tpublic " + fieldType + " is" + getterName + "(){n")
.append("ttreturn " + fieldName + ";n")
.append("t}nn");
}else{
bufferGetSet.append("tpublic " + fieldType + " get" + getterName + "(){n")
.append("ttreturn " + fieldName + ";n")
.append("t}nn");
}
//Generate setter
bufferGetSet.append("tpublic void set" + getterName + "(" + fieldType + " " + filedProperty.getName() + "){n")
.append("ttthis." + fieldName + " = " + fieldName + ";n")
.append("t}nn");
}
if(genToString){//生成toString方法
bufferToString.append("tttt"").append(fieldName).append("=" + ")
.append(fieldName);
if(index < size-1){
bufferToString.append(" + "," + n");
}else{
bufferToString.append(" + "}";n");
}
}
}
index++;
}
if(parseConfig.isSerializable()){//导入Serializable
buffer.append("import java.io.Serializable;n");
}
if(importList){//导入List
buffer.append("import java.util.List;n");
}
if(importBigDecimal){//导入Set
buffer.append("import java.math.BigDecimal;n");
}
if(importMap){//导入Map
buffer.append("import java.util.Map;n");
}
if(useLombok){//导入Lombok
buffer.append("import lombok.Data;n");
}
buffer.append("n");
//是否显示头部注释说明
if(parseConfig.isShowHeader()){
String header = parseConfig.getHeader();
if(header!=null && header.length()>0){
buffer.append("n");
}
}
if(useLombok){//添加Lombok的data注解
buffer.append("@Datan");
}
//拼接类
buffer.append("public class " + className);
if(genericity){
buffer.append("");
}
//序列化
if(parseConfig.isSerializable()){
buffer.append(" implements Serializable");
}
buffer.append(" {nn");
//添加字段
buffer.append(bufferField).append("n");
if(!useLombok){
if(genGetterAndSetter){
buffer.append(bufferGetSet);
}
if(genToString){
//toString结尾
bufferToString.append("t}nn");
buffer.append(bufferToString);
}
}
}
buffer.append("n}nn");
File dirFile = new File(outPath);
if(dirFile!=null && !dirFile.exists()){
dirFile.mkdirs();
}
//将内容写入到文件
File file = new File(outPath,javaName);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),parseConfig.getCharsetName()));
//doc格式的文档解析后会有u0007字符需统一过滤
writer.write(isDoc ? buffer.toString().replace("u0007","") : buffer.toString());
writer.flush();
writer.close();
}
}
代码 在注解、类型和初值方面 仍然需要优化。



