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

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