Java导出Word文档案例

摘自:http://blog.sina.com.cn/s/blog_65f209a501015287.html

最近做的一个Flex项目中要把报表导出到Word文件中。网上查了好多资料后最终在网友的帮助和自己的奋斗下成功搞定。现把代码贴出来供大家批评指正并一起学习。

package org.replace;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.util.Iterator;

import java.util.Map;

public class ReportWord {

public String replaceStr(String content, String oldcontent,

String newcontent) {

String rc = encodeToUnicode(newcontent);

String target = "";

oldcontent = "$" + oldcontent + "$";

target = content.replace(oldcontent, rc);

return target;

}

public String encodeToUnicode(String str) {

if (str == null)

return "";

StringBuilder sb = new StringBuilder(str.length() * 2);

for (int i = 0; i < str.length(); i++) {

sb.append(encodeToUnicode(str.charAt(i)));

}

return sb.toString();

}

public String encodeToUnicode(char character) {

if (character > 255) {

return "&#" + (character & 0xffff) + ";";

} else {

return String.valueOf(character);

}

}

public void exportWordFile(String inputPath, String outPath,

Map<String, String> data) {

String sourname = inputPath;

String sourcecontent = "";

InputStream ins = null;

try {

ins = new FileInputStream(sourname);

byte[] b = new byte[1638400];// 提高对文件的读取速度,特别是对于1M以上的文件

if (!new File(sourname).isFile()) {

System.out.println("源模板文件不存在");

return;

}

int bytesRead = 0;

while (true) {

bytesRead = ins.read(b, 0, 1638400);

if (bytesRead == -1) {

System.out.println("读取模板文件结束");

break;

}

sourcecontent += new String(b, 0, bytesRead);

}

} catch (Exception e) {

e.printStackTrace();

}

String targetcontent = "";

String oldText = "";

Object newValue;

try {

Iterator<String> keys = data.keySet().iterator();

int keysfirst = 0;

while (keys.hasNext()) {

oldText = (String) keys.next();

newValue = data.get(oldText);

String newText = (String) newValue;

if (keysfirst == 0) {

targetcontent = replaceStr(sourcecontent, oldText, newText);

keysfirst = 1;

} else {

targetcontent = replaceStr(targetcontent, oldText, newText);

keysfirst = 1;

}

}

FileWriter fw = new FileWriter(outPath, true);

PrintWriter out = new PrintWriter(fw);

if (targetcontent.equals("") || targetcontent == "") {

out.println(sourcecontent);

} else {

out.println(targetcontent);

}

out.close();

fw.close();

System.out.println(outPath + " 生成文件成功");

} catch (IOException e) {

e.printStackTrace();

}

}

}

测试代码为:

package org.replace;

import java.io.File;

import java.util.HashMap;

public class ExportFile {

public static void main(String[] args) {

ReportWord rw = new ReportWord();

HashMap<String, String> map = new HashMap<String, String>();

map.put("1", "张三");

map.put("2", "李四");

map.put("3", "王五");

map.put("4", "赵六");

map.put("5", "彩笔");

map.put("6", "张三");

map.put("7", "李四");

map.put("8", "王五");

map.put("9", "赵六");

map.put("10", "彩笔");

map.put("11", "张三");

map.put("12", "李四");

map.put("13", "王五");

map.put("14", "赵六");

map.put("15", "彩笔");

String inUrl = "D:" + File.separator + "itest.mht";

String outUrl = "C:" + File.separator + "Users"

+ File.separator + "Administrator" + File.separator + "Desktop"

+ File.separator + "itest.doc";

File fileOut = new File(outUrl);

File fileIn = new File(inUrl);

if(fileOut.exists()){

fileOut.delete();

System.out.println(inUrl + "文件已存在,已删除!");

}if(!fileIn.exists()){

return;

}

rw.exportWordFile(inUrl, outUrl, map);

}

}

使用的模板为.mht文件。