main
黄海 2 years ago
parent 9c6ef691b9
commit 5002b83ba0

@ -1,255 +1,269 @@
package UnitTest; package UnitTest;
import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelUtil;
import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.*;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class TestRegex { public class TestRegex {
public static final String excelPath = "c:\\录取分数导出结果.xlsx"; public static final String excelPath = "c:\\录取分数导出结果.xlsx";
public static String path = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\河南2021年专业录取分数.xlsx"; public static String path = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\河南2021年专业录取分数.xlsx";
public static List<List<Object>> ReadExcel() { public static List<List<Object>> ReadExcel() {
ExcelReader reader = ExcelUtil.getReader(path); ExcelReader reader = ExcelUtil.getReader(path);
List<List<Object>> readAll = reader.read(); List<List<Object>> readAll = reader.read();
reader.close(); reader.close();
return readAll; return readAll;
} }
public static void ExportExcel(List<Record> list, List<Record> errlist) throws IOException { public static void ExportExcel(List<Record> list, List<Record> errlist) throws IOException {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(path)); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(path));
XSSFSheet sheet1 = xssfWorkbook.getSheetAt(0); XSSFSheet sheet1 = xssfWorkbook.getSheetAt(0);
XSSFFont txtFont = xssfWorkbook.createFont(); XSSFFont txtFont = xssfWorkbook.createFont();
txtFont.setFontHeightInPoints((short) 11); //字体大小 txtFont.setFontHeightInPoints((short) 11); //字体大小
txtFont.setFontName("等线"); //字体 txtFont.setFontName("等线"); //字体
XSSFCellStyle cellStyleTxt = xssfWorkbook.createCellStyle(); XSSFCellStyle cellStyleTxt = xssfWorkbook.createCellStyle();
cellStyleTxt.setFont(txtFont); cellStyleTxt.setFont(txtFont);
cellStyleTxt.setAlignment(HorizontalAlignment.CENTER); cellStyleTxt.setAlignment(HorizontalAlignment.CENTER);
cellStyleTxt.setFillForegroundColor(IndexedColors.LIME.getIndex()); cellStyleTxt.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyleTxt.setWrapText(true);//设置自动换行 cellStyleTxt.setWrapText(true);//设置自动换行
cellStyleTxt.setVerticalAlignment(VerticalAlignment.CENTER); cellStyleTxt.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleTxt.setBorderBottom(BorderStyle.THIN); // 底部边框 cellStyleTxt.setBorderBottom(BorderStyle.THIN); // 底部边框
cellStyleTxt.setBorderLeft(BorderStyle.THIN); // 左边边框 cellStyleTxt.setBorderLeft(BorderStyle.THIN); // 左边边框
cellStyleTxt.setBorderRight(BorderStyle.THIN); // 右边边框 cellStyleTxt.setBorderRight(BorderStyle.THIN); // 右边边框
cellStyleTxt.setBorderTop(BorderStyle.THIN); // 上边边框 cellStyleTxt.setBorderTop(BorderStyle.THIN); // 上边边框
XSSFCellStyle cellStyleErr = xssfWorkbook.createCellStyle(); XSSFCellStyle cellStyleErr = xssfWorkbook.createCellStyle();
cellStyleErr.setFont(txtFont); cellStyleErr.setFont(txtFont);
cellStyleErr.setAlignment(HorizontalAlignment.CENTER); cellStyleErr.setAlignment(HorizontalAlignment.CENTER);
cellStyleErr.setFillForegroundColor(IndexedColors.RED.getIndex()); cellStyleErr.setFillForegroundColor(IndexedColors.RED.getIndex());
cellStyleErr.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyleErr.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyleErr.setWrapText(true);//设置自动换行 cellStyleErr.setWrapText(true);//设置自动换行
cellStyleErr.setVerticalAlignment(VerticalAlignment.CENTER); cellStyleErr.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleErr.setBorderBottom(BorderStyle.THIN); // 底部边框 cellStyleErr.setBorderBottom(BorderStyle.THIN); // 底部边框
cellStyleErr.setBorderLeft(BorderStyle.THIN); // 左边边框 cellStyleErr.setBorderLeft(BorderStyle.THIN); // 左边边框
cellStyleErr.setBorderRight(BorderStyle.THIN); // 右边边框 cellStyleErr.setBorderRight(BorderStyle.THIN); // 右边边框
cellStyleErr.setBorderTop(BorderStyle.THIN); // 上边边框 cellStyleErr.setBorderTop(BorderStyle.THIN); // 上边边框
//表头 //表头
XSSFRow row = sheet1.getRow(0); XSSFRow row = sheet1.getRow(0);
XSSFCell cell = row.createCell(12); XSSFCell cell = row.createCell(12);
cell.setCellValue("专业名称"); cell.setCellValue("专业名称");
cell.setCellStyle(cellStyleTxt); cell.setCellStyle(cellStyleTxt);
//调整宽度 //调整宽度
sheet1.setColumnWidth(12, 156 * 50 + 184); sheet1.setColumnWidth(12, 156 * 50 + 184);
row = sheet1.getRow(0); row = sheet1.getRow(0);
cell = row.createCell(13); cell = row.createCell(13);
cell.setCellValue("学费"); cell.setCellValue("学费");
cell.setCellStyle(cellStyleTxt); cell.setCellStyle(cellStyleTxt);
//调整宽度 //调整宽度
sheet1.setColumnWidth(13, 60 * 50 + 184); sheet1.setColumnWidth(13, 60 * 50 + 184);
row = sheet1.getRow(0); row = sheet1.getRow(0);
cell = row.createCell(14); cell = row.createCell(14);
cell.setCellValue("备注"); cell.setCellValue("备注");
cell.setCellStyle(cellStyleTxt); cell.setCellStyle(cellStyleTxt);
//调整宽度 //调整宽度
sheet1.setColumnWidth(14, 512 * 50 + 184); sheet1.setColumnWidth(14, 512 * 50 + 184);
//填充 //填充
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Record record = list.get(i); Record record = list.get(i);
int id = record.getInt("id"); int id = record.getInt("id");
String zhuanye = record.getStr("zhuanye"); String zhuanye = record.getStr("zhuanye");
String xf = record.getStr("xf"); String xf = record.getStr("xf");
String memo = record.getStr("memo"); String memo = record.getStr("memo");
try { try {
row = sheet1.getRow(id + 1); row = sheet1.getRow(id + 1);
cell = row.createCell(12); cell = row.createCell(12);
cell.setCellValue(zhuanye); cell.setCellValue(zhuanye);
cell.setCellStyle(cellStyleTxt); cell.setCellStyle(cellStyleTxt);
cell = row.createCell(13); cell = row.createCell(13);
cell.setCellValue(xf); cell.setCellValue(xf);
cell.setCellStyle(cellStyleTxt); cell.setCellStyle(cellStyleTxt);
cell = row.createCell(14); cell = row.createCell(14);
cell.setCellValue(memo); cell.setCellValue(memo);
cell.setCellStyle(cellStyleTxt); cell.setCellStyle(cellStyleTxt);
} catch (Exception err) { } catch (Exception err) {
System.out.println(err); System.out.println(err);
} }
} }
//写入异常数据 //写入异常数据
for (int i = 0; i < errlist.size(); i++) { for (int i = 0; i < errlist.size(); i++) {
Record record = errlist.get(i); Record record = errlist.get(i);
int id = record.getInt("id"); int id = record.getInt("id");
row = sheet1.getRow(id + 1); row = sheet1.getRow(id + 1);
cell = row.createCell(12); cell = row.createCell(12);
cell.setCellValue("人为处理"); cell.setCellValue("人为处理");
cell.setCellStyle(cellStyleErr); cell.setCellStyle(cellStyleErr);
cell = row.createCell(13); cell = row.createCell(13);
cell.setCellValue("人为处理"); cell.setCellValue("人为处理");
cell.setCellStyle(cellStyleErr); cell.setCellStyle(cellStyleErr);
cell = row.createCell(14); cell = row.createCell(14);
cell.setCellValue("人为处理"); cell.setCellValue("人为处理");
cell.setCellStyle(cellStyleErr); cell.setCellStyle(cellStyleErr);
} }
//生成文件 //生成文件
File file = new File(excelPath); File file = new File(excelPath);
try { try {
FileOutputStream fileOutputStreane = new FileOutputStream(file); FileOutputStream fileOutputStreane = new FileOutputStream(file);
xssfWorkbook.write(fileOutputStreane); xssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush(); fileOutputStreane.flush();
fileOutputStreane.close(); fileOutputStreane.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* /**
* *
* @param str *
* @return true ; false * @param str
*/ * @return true ; false
public static boolean isStr2Num(String str) { */
try { public static boolean isStr2Num(String str) {
Integer.parseInt(str); try {
return true; Integer.parseInt(str);
} catch (NumberFormatException e) { return true;
return false; } catch (NumberFormatException e) {
} return false;
} }
public static boolean leftMatch(String str) { }
int ls = str.indexOf("(");
int rs = str.indexOf(")"); public static boolean leftMatch(String str) {
if (rs < ls) return false; int ls = str.indexOf("(");
return true; int rs = str.indexOf(")");
} if (rs < ls) return false;
return true;
public static boolean rightMatch(String str) { }
int ls = str.lastIndexOf("(");
int rs = str.lastIndexOf(")"); public static boolean rightMatch(String str) {
if (rs < ls) return false; int ls = str.lastIndexOf("(");
return true; int rs = str.lastIndexOf(")");
} if (rs < ls) return false;
return true;
public static String changeMemo(String memo) { }
if (!leftMatch(memo)) memo += "(";
if (!rightMatch(memo)) memo += ")"; public static String changeMemo(String memo) {
//姜莹莹需求 if (!leftMatch(memo)) memo += "(";
memo.replace("(", ""); if (!rightMatch(memo)) memo += ")";
memo.replace(")", ""); //姜莹莹需求
memo = memo.replace("三", "3"); memo.replace("(", "");
memo = memo.replace("四", "4"); memo.replace(")", "");
memo = memo.replace("五", "5"); memo = memo.replace("三", "3");
memo = memo.replace("六", "6"); memo = memo.replace("四", "4");
return memo; memo = memo.replace("五", "5");
} memo = memo.replace("六", "6");
return memo;
}
public static void main(String[] args) throws IOException {
//按输入顺序进行正则匹配
List<String> regExp = new ArrayList<>(); public static void main(String[] args) throws IOException {
regExp.add("(.*?)\\((\\d+)元/年;(.*)\\)"); //按输入顺序进行正则匹配
regExp.add("(.*?)\\((.*?)元/年;(.*)\\)"); List<String> regExp = new ArrayList<>();
regExp.add("(.*?)\\((\\d+)元/年;(.*)\\)");
regExp.add("(.*?)\\((\\d+)元/年(.*)\\)"); regExp.add("(.*?)\\((.*?)元/年;(.*)\\)");
regExp.add("(.*?)\\((.*?)元/年(.*)\\)");
regExp.add("(.*?)\\((\\d+)元/年(.*)\\)");
regExp.add("(.*?)\\((\\d+?)元年;(.*)\\)"); regExp.add("(.*?)\\((.*?)元/年(.*)\\)");
regExp.add("(.*?)\\((.*?)元年;(.*)\\)");
regExp.add("(.*?)\\((\\d+?)元年;(.*)\\)");
regExp.add("(.*?)\\((\\d+)元/年\\)"); regExp.add("(.*?)\\((.*?)元年;(.*)\\)");
regExp.add("(.*?)\\((.*?)元/年\\)");
regExp.add("(.*?)\\((\\d+)元/年\\)");
regExp.add("(.*?)\\((\\d+)元年\\)"); regExp.add("(.*?)\\((.*?)元/年\\)");
regExp.add("(.*?)\\((.*?)元年\\)");
regExp.add("(.*?)\\((\\d+)元年\\)");
regExp.add("(.*?)\\(学费待定;(.*)\\)"); regExp.add("(.*?)\\((.*?)元年\\)");
regExp.add("(.*?)\\(学费待定(.*)\\)");
regExp.add("(.*?)\\(学费待定;(.*)\\)");
//模拟需要处理的串 regExp.add("(.*?)\\(学费待定(.*)\\)");
List<List<Object>> source = ReadExcel();
//理论与应用力学(郭永怀力学实验班)(5500元/年) //模拟需要处理的串
List<Record> list = new ArrayList<>(); List<List<Object>> source = ReadExcel();
List<Record> errList = new ArrayList<>(); //理论与应用力学(郭永怀力学实验班)(5500元/年)
List<Record> list = new ArrayList<>();
for (int i = 1; i < source.size(); i++) {//放过表头 List<Record> errList = new ArrayList<>();
Record record = new Record();
String str = source.get(i).get(6).toString();//第7列是专业名称 for (int i = 1; i < source.size(); i++) {//放过表头
//计算左括号个数,右括号个数,如果右括号数小于左括号数,则添加一个右括号 Record record = new Record();
long lc = str.chars().filter(ch -> ch == '(').count(); String str = source.get(i).get(6).toString();//第7列是专业名称
long rc = str.chars().filter(ch -> ch == ')').count(); //计算左括号个数,右括号个数,如果右括号数小于左括号数,则添加一个右括号
if (rc < lc) str += ")"; long lc = str.chars().filter(ch -> ch == '(').count();
record.set("id", i - 1); long rc = str.chars().filter(ch -> ch == ')').count();
if (rc < lc) str += ")";
boolean flag = false; record.set("id", i - 1);
for (int j = 0; j < regExp.size(); j++) {
String r1 = regExp.get(j); boolean flag = false;
Pattern pattern = Pattern.compile(r1); for (int j = 0; j < regExp.size(); j++) {
Matcher matcher = pattern.matcher(str); String r1 = regExp.get(j);
if (matcher.find()) { Pattern pattern = Pattern.compile(r1);
flag = true; Matcher matcher = pattern.matcher(str);
record.set("zhuanye", matcher.group(1)); if (matcher.find()) {
String xf = matcher.group(2); flag = true;
xf = xf.replace("r", ""); String zhuanye = matcher.group(1);
if (isStr2Num(xf.toString())) { String xz = "";
record.set("xf", xf); int xzStart = zhuanye.indexOf("(学制");
if (matcher.groupCount() >= 3) { if (xzStart >= 0) {
String memo = matcher.group(3); int xzStop = xzStart;
memo = changeMemo(memo); while (xzStop < zhuanye.length() && String.valueOf(zhuanye.charAt(xzStop)) != ")") {
record.set("memo", memo); xzStop++;
} }
} else { xz = zhuanye.substring(xzStart, xzStop);
record.set("xf", "学费待定"); zhuanye = zhuanye.replace(xz, "");
if (matcher.groupCount() >= 2) { }
String memo = matcher.group(2); record.set("zhuanye", zhuanye);
if (!leftMatch(memo)) memo = "(" + memo;
if (!rightMatch(memo)) memo += ")"; String xf = matcher.group(2);
//姜莹莹需求 xf = xf.replace("r", "");
memo.replace("(", ""); if (isStr2Num(xf.toString())) {
memo.replace(")", ""); record.set("xf", xf);
record.set("memo", memo); if (matcher.groupCount() >= 3) {
} String memo = matcher.group(3);
} memo = changeMemo(memo);
break; record.set("memo", memo + xz);
} }
} } else {
list.add(record); record.set("xf", "学费待定");
if (!flag) { if (matcher.groupCount() >= 2) {
Record r1 = new Record(); String memo = matcher.group(2);
r1.set("id", i - 1); if (!leftMatch(memo)) memo = "(" + memo;
r1.set("name", str); if (!rightMatch(memo)) memo += ")";
errList.add(r1); //姜莹莹需求
} memo.replace("(", "");
} memo.replace(")", "");
ExportExcel(list, errList); record.set("memo", memo + xz);
System.out.println("恭喜,结果数据导出成功,地址:" + excelPath); }
} }
} break;
}
}
list.add(record);
if (!flag) {
Record r1 = new Record();
r1.set("id", i - 1);
r1.set("name", str);
errList.add(r1);
}
}
ExportExcel(list, errList);
System.out.println("恭喜,结果数据导出成功,地址:" + excelPath);
}
}

Loading…
Cancel
Save