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"));
}
}
- 大小: 5.8 KB
分享到:
相关推荐
POI操作Word需要的jar包 博文链接:https://lxzqz.iteye.com/blog/2428443
poi操作word文档所需的jar包,可以读取文档中的段落表格等内容。
使用POI读写Word doc文件 Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。在hwpf里面我们使用HWPFDocument来表示一个word doc文档
android使用POI操作word docx文档,根据 【***】这种标识规则,替换掉docx文档中的内容
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
POI对Word的操作,特别是针对生成报告模板时,向word中写入文本和图片。
POI是apache提供的可以操作word文档的第三方jar。POI能操作word是使用XWPFDocument对象。
poi操作word的模板,读取数据后,将数据填充到新生成的文件中
java 利用POI读取Word文件中的内容 java 利用POI读取Word文件中的内容
资源描述了poi对于word的操作,poi合并word文档,poi设置word页码,内含maven依赖文档和完整代码,是一个完整工程,打开即可用
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701
JAVA操作MS office 工具 POI操作WORD 官方测试案例 DEMO(含jar包,IDEA) 可直接运行测试。 ------------------------------------------------------ 1 什么是Apache POI 全称Apache POI,使用Java编写的免费...
将assets中.xml中的doc文件中,就会生成word文档和excel文档
POI导出Word包含页眉 页脚的设置,标题的设置以及显示显示设置,包括POI操作Word完整的jar包
POI 生成Word文档
POI读取word文档的文字内容和图片内容
android使用POI操作替换word docx文档,根据 【***】这种标识规则,替换掉docx文档中的内容
word 2007 示例文件 值得注意的是 POI 在读取 word 文件的时候不会读取 word 文件中的图片信息 还有就是对于 2007 版的 word docx 如果 word 文件中有表格 所有表格中的数据都会在读取出来的字符串的最后 ">这是一个...
使用poi替换word中的图片,无需加书签,doc/docx均可,亲测。