Compare commits

..

3 Commits

Author SHA1 Message Date
黄海 9c6ef691b9 变更
2 years ago
黄海 91f25ce761 变更
2 years ago
黄海 2f47e73464 变更
2 years ago

@ -1,16 +1,9 @@
package UnitTest;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.aspose.cells.Workbook;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
@ -20,20 +13,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex {
/**
*
*
* @param str
* @return true ; false
*/
public static boolean isStr2Num(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static final String excelPath = "c:\\录取分数导出结果.xlsx";
public static String path = "D:\\dsWork\\FengHuang\\FengHuang\\src\\main\\java\\UnitTest\\河南2021年专业录取分数.xlsx";
@ -62,6 +42,18 @@ public class TestRegex {
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);
@ -84,47 +76,47 @@ public class TestRegex {
//调整宽度
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.toString());
// }
// }
// //写入异常数据
// XSSFSheet sheet2 = xssfWorkbook.getSheetAt(1);
// for (int i = 0; i < errlist.size(); i++) {
// short rowHeight = 30 * 20;
// Record record = errlist.get(i);
// XSSFRow row = sheet2.createRow(i + 2);
// XSSFCell cell = row.createCell(0);
// cell.setCellStyle(cellStyleTxt);
// cell.setCellValue(record.getStr("id"));
// row.setHeight(rowHeight);
//
// cell = row.createCell(1);
// cell.setCellStyle(cellStyleTxt);
// cell.setCellValue(record.getStr("name"));
// row.setHeight(rowHeight);
// }
//填充
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);
@ -139,24 +131,72 @@ public class TestRegex {
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("(.*?)\\((\\d+)元/年(.*)\\)");
regExp.add("(.*?)\\((\\d+)元年;(.*)\\)");
regExp.add("(.*?)\\((.*?)元/年;(.*)\\)");
regExp.add("(.*?)\\((.*?)元/年\\)");
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<>();
@ -167,7 +207,8 @@ public class TestRegex {
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);
record.set("id", i - 1);
boolean flag = false;
for (int j = 0; j < regExp.size(); j++) {
String r1 = regExp.get(j);
@ -181,12 +222,20 @@ public class TestRegex {
if (isStr2Num(xf.toString())) {
record.set("xf", xf);
if (matcher.groupCount() >= 3) {
record.set("memo", matcher.group(3));
String memo = matcher.group(3);
memo = changeMemo(memo);
record.set("memo", memo);
}
} else {
record.set("xf", "学费待定");
if (matcher.groupCount() >= 2) {
record.set("memo", matcher.group(2));
String memo = matcher.group(2);
if (!leftMatch(memo)) memo = "(" + memo;
if (!rightMatch(memo)) memo += ")";
//姜莹莹需求
memo.replace("(", "");
memo.replace(")", "");
record.set("memo", memo);
}
}
break;
@ -195,7 +244,7 @@ public class TestRegex {
list.add(record);
if (!flag) {
Record r1 = new Record();
r1.set("id", i + 1);
r1.set("id", i - 1);
r1.set("name", str);
errList.add(r1);
}

@ -0,0 +1,93 @@
package com.dsideal.FengHuang.Util;
import com.sun.istack.internal.NotNull;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DigitUtils {
//读音与汉字对应的表
private static final Map<Character, Integer> Arab2Chinese = new HashMap<Character, Integer>() {
{
put('零', 0);
put('一', 1);
put('二', 2);
put('三', 3);
put('四', 4);
put('五', 5);
put('六', 6);
put('七', 7);
put('八', 8);
put('九', 9);
put('十', 10);
}
};
/**
*
*/
private static final Map<Character, Integer> UnitMap = new HashMap<Character, Integer>() {
{
put('十', 10);
put('百', 100);
put('千', 1000);
put('万', 10000);
put('亿', 10000 * 10000);
}
};
//正则提取
private static Pattern pattern = Pattern.compile("[零一二三四五六七八九十]?[十百千万亿]?");
/**
*
* <p>
*
* 130000000
* 11
* 10000
* 125
* <p>
* +
* *+*...
*
* @param chinese
* @return 1328
*/
@NotNull
public static Integer Chinese2Arab(String chinese) {
Objects.requireNonNull(chinese);
//判断参数合法性
if (!pattern.matcher(chinese).replaceAll("").trim().equals("")) {
throw new InvalidParameterException();
}
Integer result = 0;
Matcher matcher = pattern.matcher(chinese);
//正则提取所有数字
while (matcher.find()) {
String res = matcher.group(0);
if (res.length() == 2) {
result += Arab2Chinese.get(res.charAt(0)) * UnitMap.get(res.charAt(1));
} else if (res.length() == 1) {
//处理三十、一百万的情况
if (UnitMap.containsKey(res.charAt(0))) {
result *= UnitMap.get(res.charAt(0));
} else if (Arab2Chinese.containsKey(res.charAt(0))) {
result += Arab2Chinese.get(res.charAt(0));
}
}
}
return result;
}
}
Loading…
Cancel
Save