main
黄海 2 years ago
parent 1f00c534db
commit e58575a51a

@ -3,9 +3,18 @@ package UnitTest;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.File;
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;
@ -27,24 +36,70 @@ public class TestRegex {
} }
} }
public static void main(String[] args) { public static void ExportExcel(List<Record> list) throws IOException {
String template = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\录取分数导出模板.xlsx";
//创建工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(template));
//读取第一个工作表(这里的下标与list一样的从0开始取之后的也是如此)
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
for (int i = 0; i < list.size(); i++) {
Record record = list.get(i);
XSSFRow row = sheet.createRow(i + 2);
XSSFCell cell = row.createCell(0);
cell.setCellValue(record.getStr("id"));
cell = row.createCell(1);
cell.setCellValue(record.getStr("zhuanye"));
cell = row.createCell(2);
cell.setCellValue(record.getStr("xf"));
cell = row.createCell(3);
cell.setCellValue(record.getStr("memo"));
}
//生成文件
File file = new File("c:\\录取分数导出结果.xlsx");
try {
FileOutputStream fileOutputStreane = new FileOutputStream(file);
xssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush();
fileOutputStreane.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//按输入顺序进行正则匹配 //按输入顺序进行正则匹配
List<String> regExp = new ArrayList<>(); List<String> regExp = new ArrayList<>();
regExp.add("(.*?)\\((\\d+)元/年;(.*)\\)"); regExp.add("(.*?)\\((\\d+)元/年;(.*)\\)");
regExp.add("(.*?)\\((\\d+)元/年(.*)\\)");
regExp.add("(.*?)\\((\\d+)元年;(.*)\\)");
regExp.add("(.*?)\\((.*?)元/年;(.*)\\)");
regExp.add("(.*?)\\(学费待定;(.*)\\)"); regExp.add("(.*?)\\(学费待定;(.*)\\)");
regExp.add("(.*?)\\((\\d+)元/年\\)"); regExp.add("(.*?)\\((\\d+)元/年\\)");
regExp.add("(.*?)\\(学费待定(.*)\\)");
//模拟需要处理的串 //模拟需要处理的串
String path = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\TestRegex.txt"; String path = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\TestRegex.txt";
byte[] sb = FileUtil.readBytes(new File(path)); byte[] sb = FileUtil.readBytes(new File(path));
//内存读写流 不用回收关闭
ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(sb); ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(sb);
String str1 = IoUtil.read(byteArrayInputStream, CharsetUtil.UTF_8); String str1 = IoUtil.read(byteArrayInputStream, CharsetUtil.UTF_8);
String[] txtArray = str1.split("\r\n"); String[] txtArray = str1.split("\r\n");
List<Record> list = new ArrayList<>();
for (int i = 0; i < txtArray.length; i++) {
Record record = new Record();
for (int i = 0; i < Math.min(310,txtArray.length); i++) {
String str = txtArray[i]; String str = txtArray[i];
System.out.println("第" + (i + 1) + "行:"); //计算左括号个数,右括号个数,如果右括号数小于左括号数,则添加一个右括号
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; boolean flag = false;
for (int j = 0; j < regExp.size(); j++) { for (int j = 0; j < regExp.size(); j++) {
String r1 = regExp.get(j); String r1 = regExp.get(j);
@ -52,21 +107,28 @@ public class TestRegex {
Matcher matcher = pattern.matcher(str); Matcher matcher = pattern.matcher(str);
if (matcher.find()) { if (matcher.find()) {
flag = true; flag = true;
System.out.println("专业名称:" + matcher.group(1)); record.set("zhuanye", matcher.group(1));
if (isStr2Num(matcher.group(2).toString())) { String xf=matcher.group(2);
System.out.println("学费:" + matcher.group(2)); if (isStr2Num(xf.toString())) {
if (matcher.groupCount() >= 3) System.out.println("描述:" + matcher.group(3)); record.set("xf", xf);
if (matcher.groupCount() >= 3) {
record.set("memo", matcher.group(3));
}
} else { } else {
System.out.println("学费:学费待定"); record.set("xf", "学费待定");
if (matcher.groupCount() >= 2) System.out.println("描述:" + matcher.group(2)); if (matcher.groupCount() >= 2) {
record.set("memo", matcher.group(2));
}
} }
break; break;
} }
} }
list.add(record);
if (!flag) { if (!flag) {
System.out.println("有一个文本无法匹配现有正则规则,需要人工处理!"); System.out.println("有一个文本无法匹配现有正则规则,需要人工处理!");
System.out.println(str); System.out.println(str);
} }
} }
ExportExcel(list);
} }
} }

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save