13 changed files with 684 additions and 91 deletions
@ -0,0 +1,209 @@ |
|||
package com.yxt.common.base.utils; |
|||
|
|||
import cn.hutool.extra.pinyin.PinyinUtil; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @Author dimengzhe |
|||
* @Date 2022/9/28 15:54 |
|||
* @Description |
|||
*/ |
|||
public class HanZiConverterPinYin { |
|||
|
|||
public HanZiConverterPinYin() { |
|||
// 初始化汉字拼音字母对照表
|
|||
initChinesePinyinComparisonMapList(); |
|||
} |
|||
|
|||
/** |
|||
* 汉字拼音字母对照表 |
|||
*/ |
|||
private List<ChinesePinyinComparisonMap> chinesePinyinComparisonMapList; |
|||
|
|||
/** |
|||
* 初始化汉字拼音字母对照表 |
|||
*/ |
|||
private void initChinesePinyinComparisonMapList() { |
|||
chinesePinyinComparisonMapList = new ArrayList<ChinesePinyinComparisonMap>(); |
|||
|
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-20319, -20284, 'A')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-20283, -19776, 'B')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-19775, -19219, 'C')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-19218, -18711, 'D')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-18710, -18527, 'E')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-18526, -18240, 'F')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-18239, -17923, 'G')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-17922, -17418, 'H')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-17417, -16475, 'J')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-16474, -16213, 'K')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-16212, -15641, 'L')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-15640, -15166, 'M')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-15165, -14923, 'N')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14922, -14915, 'O')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14914, -14631, 'P')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14630, -14150, 'Q')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14149, -14091, 'R')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14090, -13319, 'S')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-13318, -12839, 'T')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-12838, -12557, 'W')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-12556, -11848, 'X')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-11847, -11056, 'Y')); |
|||
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-11055, -10247, 'Z')); |
|||
} |
|||
|
|||
/** |
|||
* 遍历获取首字母 |
|||
*/ |
|||
public char getPY(char c) throws Exception { |
|||
byte[] bytes = String.valueOf(c).getBytes("GBK"); |
|||
|
|||
//双字节汉字处理
|
|||
if (bytes.length == 2) { |
|||
|
|||
int hightByte = 256 + bytes[0]; |
|||
int lowByte = 256 + bytes[1]; |
|||
int asc = (256 * hightByte + lowByte) - 256 * 256; |
|||
|
|||
// 遍历转换
|
|||
for (ChinesePinyinComparisonMap map : this.chinesePinyinComparisonMapList) { |
|||
if (asc >= map.getSAscll() && asc <= map.getEAscll()) { |
|||
return map.getCode(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 单字节或其他直接输入,不执行编码
|
|||
return c; |
|||
} |
|||
|
|||
/** |
|||
* 获取汉字拼音 |
|||
*/ |
|||
public String getHanZiPY(String str) { |
|||
try { |
|||
StringBuilder pyStrBd = new StringBuilder(); |
|||
|
|||
for (char c : str.toCharArray()) { |
|||
pyStrBd.append(getPY(c)); |
|||
} |
|||
|
|||
return pyStrBd.toString(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 汉字拼音字母对照类 |
|||
*/ |
|||
@Data |
|||
@AllArgsConstructor |
|||
private class ChinesePinyinComparisonMap { |
|||
// 区间开头
|
|||
private int sAscll; |
|||
|
|||
// 区间结尾
|
|||
private int eAscll; |
|||
|
|||
// 对应字母
|
|||
private char code; |
|||
} |
|||
|
|||
private static final Map<String, Object> DYZMAP = setDYZMap(); |
|||
|
|||
private static Map<String, Object> setDYZMap() { |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("仇", "QIU"); |
|||
map.put("柏", "BO"); |
|||
map.put("牟", "MU"); |
|||
map.put("颉", "XIE"); |
|||
map.put("解", "XIE"); |
|||
map.put("尉", "YU"); |
|||
map.put("奇", "JI"); |
|||
map.put("单", "SHAN"); |
|||
map.put("谌", "SHEN"); |
|||
map.put("乐", "YUE"); |
|||
map.put("召", "SHAO"); |
|||
map.put("朴", "PIAO"); |
|||
map.put("区", "OU"); |
|||
map.put("查", "ZHA"); |
|||
map.put("曾", "ZENG"); |
|||
map.put("缪", "MIAO"); |
|||
map.put("晟", "CHENG"); |
|||
map.put("员", "YUN"); |
|||
map.put("贠", "YUN"); |
|||
map.put("黑", "HE"); |
|||
map.put("重", "CHONG"); |
|||
map.put("秘", "BI"); |
|||
map.put("冼", "XIAN"); |
|||
map.put("折", "SHE"); |
|||
map.put("翟", "ZHAI"); |
|||
map.put("盖", "GE"); |
|||
map.put("万俟", "MOQI"); |
|||
map.put("尉迟", "YUCHI"); |
|||
return map; |
|||
} |
|||
|
|||
public static String getPinYinFirst(String str) { |
|||
HanZiConverterPinYin hanZiConverterPinYin = new HanZiConverterPinYin(); |
|||
char firstChar = str.toCharArray()[0]; |
|||
String firstString = ""; |
|||
if (Character.toString(firstChar).matches("^[\u4e00-\u9fa5]+$")) { // 为中文
|
|||
char[] charPinYinChar = PinYinUtils.getCharPinYinChar(firstChar); |
|||
String result = ""; |
|||
if (DYZMAP.containsKey(Character.toString(firstChar))) { |
|||
result = DYZMAP.get(Character.toString(firstChar)).toString().substring(0, 1); |
|||
} else { |
|||
result = StringUtils.join(charPinYinChar[0]); |
|||
} |
|||
firstString = result.toUpperCase(); |
|||
} else if (Character.toString(firstChar).matches("^[a-zA-Z]")) { // 为英文字母
|
|||
firstString = Character.toString(firstChar).toUpperCase(); |
|||
} |
|||
|
|||
// String code = hanZiConverterPinYin.getHanZiPY(str);
|
|||
String code = PinyinUtil.getFirstLetter(str, ""); |
|||
StringBuffer buffer = new StringBuffer(code); |
|||
code = buffer.replace(0, 1, firstString).toString(); |
|||
code = code.toUpperCase(); |
|||
System.out.println(code); |
|||
return code; |
|||
} |
|||
|
|||
//测试
|
|||
public static void main(String[] args) { |
|||
HanZiConverterPinYin hanZiConverterPinYin = new HanZiConverterPinYin(); |
|||
String str = "王佳琪"; |
|||
char firstChar = str.toCharArray()[0]; |
|||
String firstString = ""; |
|||
if (Character.toString(firstChar).matches("^[\u4e00-\u9fa5]+$")) { // 为中文
|
|||
char[] charPinYinChar = PinYinUtils.getCharPinYinChar(firstChar); |
|||
String result = ""; |
|||
if (DYZMAP.containsKey(Character.toString(firstChar))) { |
|||
result = DYZMAP.get(Character.toString(firstChar)).toString().substring(0, 1); |
|||
} else { |
|||
result = StringUtils.join(charPinYinChar[0]); |
|||
} |
|||
firstString = result.toUpperCase(); |
|||
} else if (Character.toString(firstChar).matches("^[a-zA-Z]")) { // 为英文字母
|
|||
firstString = Character.toString(firstChar).toUpperCase(); |
|||
} |
|||
|
|||
// String code = hanZiConverterPinYin.getHanZiPY(str);
|
|||
String code = PinyinUtil.getFirstLetter("王佳琪", ""); //c-s
|
|||
StringBuffer buffer = new StringBuffer(code); |
|||
code = buffer.replace(0, 1, firstString).toString(); |
|||
code = code.toUpperCase(); |
|||
System.out.println(code); |
|||
} |
|||
} |
@ -0,0 +1,220 @@ |
|||
package com.yxt.common.base.utils; |
|||
|
|||
import java.math.BigDecimal; |
|||
import java.util.regex.Matcher; |
|||
import java.util.regex.Pattern; |
|||
|
|||
/** |
|||
* @Author dimengzhe |
|||
* @Date 2022/8/17 11:26 |
|||
* @Description |
|||
*/ |
|||
public class MoneyToChineseUtils { |
|||
|
|||
/** |
|||
* 不考虑分隔符的正确性 |
|||
*/ |
|||
private static final Pattern AMOUNT_PATTERN = Pattern.compile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$"); |
|||
private static final char[] RMB_NUMS = "零壹贰叁肆伍陆柒捌玖".toCharArray(); |
|||
private static final String[] UNITS = {"元", "角", "分", "整"}; |
|||
private static final String[] U1 = {"", "拾", "佰", "仟"}; |
|||
private static final String[] U2 = {"", "万", "亿"}; |
|||
|
|||
/** |
|||
* 将金额(整数部分等于或少于12位,小数部分2位)转换为中文大写形式. |
|||
* |
|||
* @param money 金额数字 |
|||
* @return 中文大写 |
|||
*/ |
|||
public static String convert(Double money) { |
|||
money = Math.abs(money); |
|||
if (Double.valueOf(0).equals(money)) { |
|||
return "零元整"; |
|||
} |
|||
String amount = moneyFormat(money); |
|||
// 去掉分隔符
|
|||
amount = amount.replace(",", ""); |
|||
// 验证金额正确性
|
|||
|
|||
Matcher matcher = AMOUNT_PATTERN.matcher(amount); |
|||
if (!matcher.find()) { |
|||
|
|||
} |
|||
// 整数部分
|
|||
String integer = matcher.group(1); |
|||
// 小数部分
|
|||
String fraction = matcher.group(2); |
|||
|
|||
String result = ""; |
|||
if (!integer.equals("0")) { |
|||
// 整数部分
|
|||
result += integer2rmb(integer) + UNITS[0]; |
|||
} |
|||
if (fraction.equals("00")) { |
|||
// 添加[整]
|
|||
result += UNITS[3]; |
|||
} else if (fraction.startsWith("0") && integer.equals("0")) { |
|||
// 去掉分前面的[零]
|
|||
result += fraction2rmb(fraction).substring(1); |
|||
} else { |
|||
// 小数部分
|
|||
result += fraction2rmb(fraction); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 将金额小数部分转换为中文大写 |
|||
*/ |
|||
private static String fraction2rmb(String fraction) { |
|||
// 角
|
|||
char jiao = fraction.charAt(0); |
|||
// 分
|
|||
char fen = fraction.charAt(1); |
|||
return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : "")) + (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : ""); |
|||
} |
|||
|
|||
/** |
|||
* 将金额整数部分转换为中文大写 |
|||
* |
|||
* @param integer |
|||
* @return |
|||
*/ |
|||
private static String integer2rmb(String integer) { |
|||
StringBuilder buffer = new StringBuilder(); |
|||
// 从个位数开始转换
|
|||
int i, j; |
|||
for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) { |
|||
char n = integer.charAt(i); |
|||
if (n == '0') { |
|||
// 当n是0且n的右边一位不是0时,插入[零]
|
|||
if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') { |
|||
buffer.append(RMB_NUMS[0]); |
|||
} |
|||
// 插入[万]或者[亿]
|
|||
if (j % 4 == 0) { |
|||
if (i > 0 && integer.charAt(i - 1) != '0' |
|||
|| i > 1 && integer.charAt(i - 2) != '0' |
|||
|| i > 2 && integer.charAt(i - 3) != '0') { |
|||
buffer.append(U2[j / 4]); |
|||
} |
|||
} |
|||
} else { |
|||
if (j % 4 == 0) { |
|||
// 插入[万]或者[亿]
|
|||
buffer.append(U2[j / 4]); |
|||
} |
|||
// 插入[拾]、[佰]或[仟]
|
|||
buffer.append(U1[j % 4]); |
|||
// 插入数字
|
|||
buffer.append(RMB_NUMS[n - '0']); |
|||
} |
|||
} |
|||
return buffer.reverse().toString(); |
|||
} |
|||
|
|||
/** |
|||
* 对金额的格式调整到分 |
|||
* |
|||
* @param preMoney |
|||
* @return |
|||
*/ |
|||
public static String moneyFormat(Double preMoney) { |
|||
//23->23.00
|
|||
String money = preMoney.toString(); |
|||
StringBuffer sb = new StringBuffer(); |
|||
if (money == null) { |
|||
return "0.00"; |
|||
} |
|||
int index = money.indexOf("."); |
|||
if (index == -1) { |
|||
return money + ".00"; |
|||
} else { |
|||
//整数部分
|
|||
String s0 = money.substring(0, index); |
|||
//小数部分
|
|||
String s1 = money.substring(index + 1); |
|||
if (s1.length() == 1) { |
|||
//小数点后一位
|
|||
s1 = s1 + "0"; |
|||
} else if (s1.length() > 2) { |
|||
//如果超过3位小数,截取2位就可以了
|
|||
s1 = s1.substring(0, 2); |
|||
} |
|||
sb.append(s0); |
|||
sb.append("."); |
|||
sb.append(s1); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
|
|||
/** |
|||
* 对金额的格式调整到分 |
|||
* |
|||
* @param preMoney |
|||
* @return |
|||
*/ |
|||
public static String moneyFormat(BigDecimal preMoney) { |
|||
//23->23.00
|
|||
String money = preMoney.toString(); |
|||
StringBuffer sb = new StringBuffer(); |
|||
if (money == null) { |
|||
return "0.00"; |
|||
} |
|||
int index = money.indexOf("."); |
|||
if (index == -1) { |
|||
return money + ".00"; |
|||
} else { |
|||
//整数部分
|
|||
String s0 = money.substring(0, index); |
|||
//小数部分
|
|||
String s1 = money.substring(index + 1); |
|||
if (s1.length() == 1) { |
|||
//小数点后一位
|
|||
s1 = s1 + "0"; |
|||
} else if (s1.length() > 2) { |
|||
//如果超过3位小数,截取2位就可以了
|
|||
s1 = s1.substring(0, 2); |
|||
} |
|||
sb.append(s0); |
|||
sb.append("."); |
|||
sb.append(s1); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
|
|||
/** |
|||
* 对金额的格式调整到分 |
|||
* |
|||
* @param preMoney |
|||
* @return |
|||
*/ |
|||
public static String moneyFormat(String preMoney) { |
|||
//23->23.00
|
|||
String money = preMoney; |
|||
StringBuffer sb = new StringBuffer(); |
|||
if (money == null) { |
|||
return "0.00"; |
|||
} |
|||
int index = money.indexOf("."); |
|||
if (index == -1) { |
|||
return money + ".00"; |
|||
} else { |
|||
//整数部分
|
|||
String s0 = money.substring(0, index); |
|||
//小数部分
|
|||
String s1 = money.substring(index + 1); |
|||
if (s1.length() == 1) { |
|||
//小数点后一位
|
|||
s1 = s1 + "0"; |
|||
} else if (s1.length() > 2) { |
|||
//如果超过3位小数,截取2位就可以了
|
|||
s1 = s1.substring(0, 2); |
|||
} |
|||
sb.append(s0); |
|||
sb.append("."); |
|||
sb.append(s1); |
|||
} |
|||
return sb.toString(); |
|||
} |
|||
} |
@ -0,0 +1,101 @@ |
|||
package com.yxt.common.base.utils; |
|||
|
|||
import com.jacob.activeX.ActiveXComponent; |
|||
import com.jacob.com.ComThread; |
|||
import com.jacob.com.Dispatch; |
|||
import freemarker.template.Configuration; |
|||
import freemarker.template.Template; |
|||
import freemarker.template.Version; |
|||
|
|||
import java.io.*; |
|||
import java.util.Date; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* @Author dimengzhe |
|||
* @Date 2022/10/18 17:11 |
|||
* @Description |
|||
*/ |
|||
public class WordConvertUtils { |
|||
|
|||
/** |
|||
* 根据ftl模板生成word |
|||
* |
|||
* @param map 数据 |
|||
* @param typeName 模板名称 |
|||
* @param sourcePath 模板路径 |
|||
* @param targetPath 保存目标路径 |
|||
* @param fileName 文件名 |
|||
*/ |
|||
public static void creatWord(Map<String, Object> map, File file, String targetPath, String fileName, String dir) { |
|||
String curDate = DateUtils.dateConvertStr(new Date(), "yyyy年MM月dd日"); |
|||
try { |
|||
//Configuration 用于读取ftl文件
|
|||
Configuration configuration = new Configuration(new Version("2.3.0")); |
|||
configuration.setDefaultEncoding("utf-8"); |
|||
//指定路径的第二种方式
|
|||
configuration.setDirectoryForTemplateLoading(new File(dir)); |
|||
//输出文档路径及名称
|
|||
File targetFile = new File(targetPath); |
|||
if (!targetFile.exists()) { |
|||
targetFile.mkdirs(); |
|||
} |
|||
targetPath = targetPath + fileName; |
|||
File outFile = new File(targetPath); |
|||
//以utf-8的编码读取ftl文件
|
|||
Template template = configuration.getTemplate(file.getName(), "utf-8"); |
|||
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240); |
|||
template.process(map, out); |
|||
out.close(); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* word转换为pdf |
|||
* |
|||
* @param wordFile word的路径 |
|||
* @param pdfPath pdf的路径 |
|||
* @param pdfName pdf名称 |
|||
*/ |
|||
public static void doc2pdf(String wordFile, String pdfPath, String pdfName) { |
|||
ActiveXComponent app = null; |
|||
System.out.println("开始转换..."); |
|||
long start = System.currentTimeMillis(); |
|||
Dispatch document = null; |
|||
try { |
|||
ComThread.InitSTA(); |
|||
// 打开word
|
|||
app = new ActiveXComponent("Word.Application"); |
|||
// 获得word中所有打开的文档
|
|||
Dispatch documents = app.getProperty("Documents").toDispatch(); |
|||
// 打开文档
|
|||
document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); |
|||
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
|||
File targetFile = new File(pdfPath); |
|||
if (!targetFile.exists()) { |
|||
targetFile.mkdirs(); |
|||
} |
|||
String pdfFile = pdfPath + pdfName; |
|||
File target = new File(pdfFile); |
|||
if (target.exists()) { |
|||
target.delete(); |
|||
} |
|||
Dispatch.call(document, "SaveAs", pdfFile, 17); |
|||
long end = System.currentTimeMillis(); |
|||
System.out.println("转换完成..用时:" + (end - start) + "ms."); |
|||
} catch (Exception e) { |
|||
System.out.println("转换失败" + e.getMessage()); |
|||
} finally { |
|||
// 关闭文档
|
|||
Dispatch.call(document, "Close", false); |
|||
// 关闭office
|
|||
app.invoke("Quit", 0); |
|||
System.out.println("关闭文档"); |
|||
// 如果没有这句话,winword.exe进程将不会关闭
|
|||
ComThread.Release(); |
|||
} |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue