package com.zte.xh.fund.util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Fields; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; /** * 用来操作word的方法类 * * @author Jay_Lee * */ public class WordUtil { private static String[] CHINESE_MONEY = new String[] { "零", "壹", "貮", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟", "萬" }; /** * 导入poi-3.12.jar和poi-scratchpad.jar 实现对word读取和修改操作 * * @param filePath * word模板路径和名称 * @param map * 待填充的数据,从数据库读取/自己填写 * @throws IOException */ public static void readwriteWord(String filePath, Map<String, String> map, String toFile, String fileName) throws IOException { // 首先看目录存在否,不存在先创建目录 File temp = new File(filePath); if (!temp.exists()) { temp.mkdirs(); } File temp1 = new File(toFile); if (!temp1.exists()) { temp1.mkdirs(); } // 读取world文档 FileInputStream fis = new FileInputStream(new File(filePath)); // 读取world文档 HWPFDocument hdt = new HWPFDocument(fis); // 读取word文本内容 Range range = hdt.getRange(); // 替换文本内容 for (Map.Entry<String, String> entry : map.entrySet()) { range.replaceText(entry.getKey(), entry.getValue()); } ByteArrayOutputStream ostream = new ByteArrayOutputStream(); FileOutputStream out = null; out = new FileOutputStream(toFile + File.separator + fileName, true); hdt.write(ostream); out.write(ostream.toByteArray()); out.close(); } // 根据金额的小写来查询其中的大写 public static String transformMoney(String money) { String resultMoney = ""; int length = money.length(); // 如果是一位,直接返回大写数字 if (length == 1) { String index = String.valueOf(money.charAt(0)); if (index.equals("0")) { return ""; } else { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 1]; return resultMoney; } } // 如果是2位,先判断第一位,再迭代上一个方法 if (length == 2) { String index = String.valueOf(money.charAt(0)); if (index.equals("0")) { resultMoney = transformMoney(money.substring( money.length() - 1, money.length())); return resultMoney; } else { String last = money.substring(money.length() - 1, money.length()); if (last.equals("0")) { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 4] + CHINESE_MONEY[CHINESE_MONEY.length - 1]; return resultMoney; } else { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 4] + transformMoney(money.substring( money.length() - 1, money.length())); return resultMoney; } } } // 三位,以此内推,记得三位可能中间有0 if (length == 3) { String index = String.valueOf(money.charAt(0)); if (index.equals("0")) { resultMoney = transformMoney(money.substring( money.length() - 2, money.length())); return resultMoney; } else { String indexMid = money.substring(1, 2); String indexEnd = money.substring(2, 3); if (indexMid.equals("0") && indexEnd.equals("0")) { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 3] + CHINESE_MONEY[CHINESE_MONEY.length - 1]; return resultMoney; } else if (indexMid.equals("0")) { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 3] + CHINESE_MONEY[0] + transformMoney(money.substring( money.length() - 1, money.length())); return resultMoney; } else { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 3] + transformMoney(money.substring( money.length() - 2, money.length())); return resultMoney; } } } // 四位数 if (length == 4) { String index = String.valueOf(money.charAt(0)); if (index.equals("0")) { resultMoney = transformMoney(money.substring( money.length() - 3, money.length())); } else { String indexTwo = money.substring(1, 2); String indexThree = money.substring(2, 3); String indexEnd = money.substring(3, 4); if (indexTwo.equals("0") && indexThree.equals("0") && indexEnd.equals("0")) { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 2] + CHINESE_MONEY[CHINESE_MONEY.length - 1]; } else if (indexTwo.equals("0")) { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 2] + CHINESE_MONEY[0] + transformMoney(money.substring( money.length() - 2, money.length())); } else { resultMoney = CHINESE_MONEY[Integer.valueOf(index)] + CHINESE_MONEY[CHINESE_MONEY.length - 2] + transformMoney(money.substring( money.length() - 3, money.length())); } } return resultMoney; } return null; } public static List<String> getWordText(String path) throws Exception { // 读取world文档 FileInputStream fis = new FileInputStream(new File(path)); // 读取world文档 HWPFDocument hdt = new HWPFDocument(fis); List<String> listText = new ArrayList<String>(); // 读取word文本内容 Range range = hdt.getRange(); //文档段落数目 int paragraphCount=range.numParagraphs(); //遍历段落读取数据 for(int i=0;i<paragraphCount;i++) { Paragraph pph=range.getParagraph(i); /* String temp = pph.text(); temp = replaceString(temp);*/ // 这里替换掉word的格式符号以方便页面展示 listText.add( pph.text()); } return listText; } public static String replaceString(String str){ str = str.replaceAll("", " "); /* str = str.replace("\r", "<br/>");*/ return str; } public static void main(String[] args) throws Exception { // 设置数据 /* * Map<String, String> map = new HashMap<String, String>(); * map.put("name", "demo1"); map.put("time", new * SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); * readwriteWord("E:\\testDemo.doc", map, "E:\\"); */ getWordText("F:" + File.separator + "model1.doc"); // System.out.println(transformMoney("0001")); } }