Java:Java实现world档案的自由读取修改
Java怎么读取一个world文件?并实现文件的修改等操作?
对于这个问题很多初学者会理直气壮的说用IO流啊,Java的IO确实是用来进行档案的读取等操作的,用JavaIO进行TXT文件的读取等操作,完全没毛病,但是对于world等高阶文件的读取,单纯的靠IO是无法实现的,那么java怎么读取world文件呢?当然是借助第三方提供的jar包来实现了,今天主要聊聊怎么读取world文件.

java进行excel的读取请参见上篇:Java:java五行程式码实现Excel的快速汇出
所使用到的jar是:
/*
* 文 件 名: MSWordPoi4.java
* 版 权: Sunny Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
* 描 述:
* 修 改 人: L.Hao
* 修改时间: 2014-8-8
* 跟踪单号:
* 修改单号:
* 修改内容:
*/
package com.test;
/**
*
*
*
* @author L.Hao
* @version [版本号, 2014-8-8]
* @see [相关类/方法]
* @since [产品/模组版本]
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.Fields;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
public class MSWordPoi4
{
/**
* @param args
*/
public static void main(String[] args)
{
Map map = new HashMap();
map.put("${sub}", "湖南大学");
map.put("${item2.school}", "湖南大学");
map.put("${item2.address}", "湖南");
// map.put("${item1.name}", "王五1");
// map.put("${item1.numberStudent}", "编号002");
// map.put("${item1.sex}", "男2");
// map.put("${item1.age}", "19");
String srcPath = "D:\temp\铸造工艺卡.doc";
readwriteWord(srcPath, map);
}
/**
* 实现对word读取和修改操作
*
* @param filePath
* word模板路径和名称
* @param map
* 待填充的资料,从数据库读取
*/
public static void readwriteWord(String filePath, Map map)
{
// 读取word模板
// String fileDir = new
// File(base.getFile(),"http://www.cnblogs.com/http://www.cnblogs.com/../doc/").getCanonicalPath();
FileInputStream in = null;
try
{
in = new FileInputStream(new File(filePath));
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
HWPFDocument hdt = null;
try
{
hdt = new HWPFDocument(in);
}
catch (IOException e1)
{
e1.printStackTrace();
}
Fields fields = hdt.getFields();
Iterator it = fields.getFields(FieldsDocumentPart.MAIN)
.iterator();
while (it.hasNext())
{
System.out.println(it.next().getType());
}
//读取word文字内容
Range range = hdt.getRange();
TableIterator tableIt = new TableIterator(range);
//迭代文件中的表格
int ii = 0;
while (tableIt.hasNext()) {
Table tb = (Table) tableIt.next();
ii++;
System.out.println("第"+ii+"个表格资料...................");
//迭代行,预设从0开始
for (int i = 0; i
TableRow tr = tb.getRow(i);
//只读前8行,标题部分
if(i >=8) break;
//迭代列,预设从0开始
for (int j = 0; j
TableCell td = tr.getCell(j);//取得单元格
//取得单元格的内容
for(int k=0;k
Paragraph para =td.getParagraph(k);
String s = para.text();
System.out.println(s);
} //end for
} //end for
} //end for
} //end while
//System.out.println(range.text());
// 替换文字内容
for (Map.Entry entry : map.entrySet())
{
range.replaceText(entry.getKey(), entry.getValue());
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
String fileName = "" + System.currentTimeMillis();
fileName += ".doc";
FileOutputStream out = null;
try
{
out = new FileOutputStream("F:/" + fileName, true);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
hdt.write(ostream);
}
catch (IOException e)
{
e.printStackTrace();
}
// 输出字节流
try
{
out.write(ostream.toByteArray());
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
ostream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
java读取world文件的第三方jar可能有多种,本方案仅供参考

原文:https://blog.csdn.net/xiyushiyi/article/details/41519235