poi操作word文档

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"));
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值