大家好,这是我的故事!😊
读取xls/xlsx文件获取数据并且拼接成代码
题意
【题目描述】
为了满足公司工作需要,维护前端需要对较多字段名进行更改。手动更新效率太低,现在代码支持,一次一个表格。
【难点】
该功能实现几乎没有难点,主要就是对xls/xlsx文件操作的jar包的版本问题,版本不一致会出现各种报错。
java
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AutoOutCode {
public static void main(String[] args) {
File file = new File("C:\\Users\\Administrator\\Desktop\\1.xlsx");
try {
InputStream fis = new FileInputStream(file);
//读取导入的Excel文件内容
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis);
//获取第一个sheet工作薄
Sheet sheet = xssfWorkbook.getSheetAt(0);
//读取出来的数据放到ListMap中
List<Map<String, Object>> mapList =
new ArrayList<>();
//获取sheet的最大row下标,实际获取的行数-1
int lastRowNum = sheet.getLastRowNum();
if(lastRowNum > 0){
for(Row row : sheet){
//跳过表头
if(row.getRowNum() == 0){
continue;
}
//判断导入的excel文件中的是否存在空
boolean flag = isExistNull(row);
if(true){
Map<String, Object> susMap = new HashMap<>();
susMap.put("id", row.getCell(0).getStringCellValue());
susMap.put("intro", row.getCell(1).getStringCellValue());
susMap.put("def", row.getCell(2).getStringCellValue());
row.getCell(0).getStringCellValue();
//System.out.println(row.getCell(0).getStringCellValue());
//开始格式化
MyFormat(susMap);
}else{
//获取当前行数
int currentNum = row.getRowNum() + 1;
//return "第" + currentNum + "行数据出错,不能为空!"
System.out.println("第" + currentNum +
"行数据出错,不能为空!");
}
}
}else{
System.out.println("您导入的excel文件为空,请重新导入!");
//return "您导入的excel文件为空,请重新导入!";
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param map
*/
//自己格式化
public static void MyFormat(Map map){
if (map.get("id").toString() == null){
return;
}
String Code = "";
if (map.get("intro") != null && map.get("intro").toString().length() >= 0){
Code += "/**" + "\n" + " * " + map.get("intro") + "\n" + " */" + "\n";
}
Code += "private String " + map.get("id") + " ";
if (map.get("def") == null || map.get("def").toString().length()==0){
Code += "= \"\";";
}else {
Code += "= \"" + map.get("def") + "\";";
}
Code+="\n";
String UpId = null;
char[] cs=map.get("id").toString().trim().toCharArray();
cs[0]-=32;
UpId = String.valueOf(cs);
Code += "public String " + UpId + " { get { return " + map.get("id") + "; } set { " + map.get("id") + " = value; } }";
Code+="\n";
System.out.println(Code);
}
//判断导入的excel文件中row行的每个cell时候是否存在空
public static boolean isExistNull(Row row){
boolean flag0 = false;
boolean flag1 = false;
boolean flag2 = false;
Cell cell0 = row.getCell(0);
if(cell0 == null){
flag0 = false;
}else{
cell0.setCellType(Cell.CELL_TYPE_STRING);
if(cell0.getStringCellValue() == null){
flag0 = false;
}
}
Cell cell1 = row.getCell(1);
if(cell1 == null){
flag1 = false;
}else{
cell1.setCellType(Cell.CELL_TYPE_STRING);
if(cell1.getStringCellValue() == null){
flag1 = false;
}
}
Cell cell2 = row.getCell(2);
if(cell2 == null){
flag2 = false;
}else{
cell2.setCellType(Cell.CELL_TYPE_STRING);
if(cell2.getStringCellValue() == null){
flag2 = false;
}
}
if (flag0 || flag1 || flag2)
return true;
return false;
}
}