案例
/**
变量中文名
/
private String cI*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";
}
}