使用POI的hwpf操作word乱码
public void testWord(String file,String newPath) {
//File file = new File(fileString);
try {
FileInputStream in = new FileInputStream(file);
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();
StyleSheet styleSheet = hwpf.getStyleSheet();
TableIterator it = new TableIterator(range);
// 遍历一个DOC中的所有表格
while (it.hasNext()) {
Table tb = (Table) it.next();
// 遍历表格的行
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 遍历表格的列
for (int j = 0; j < tr.numCells(); j++) {
// 往表格中插入数据
TableCell td = tr.getCell(j);
String text = "哈哈,输入中文";
int p = td.numParagraphs();
Paragraph para = td.getParagraph(p);
ParagraphProperties pp = new ParagraphProperties();
if(j%2==0){
para.insertBefore(text);
}
}
}
}
// 在表格外面插入内容
CharacterProperties cp = new CharacterProperties();
cp.setBold(true);
cp.setCharacterSpacing(10);
cp.setChse(cp.SPRM_CHARSCALE);
cp.setCapitalized(true);
File outputFile = new File(newPath);
OutputStream output = new FileOutputStream(outputFile);
hwpf.write(output);
output.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
这是现在用的程序,我的需求是,先读取原来的word信息,包括文字、表格、样式等,然后再输入内容,再生成word文档。
可现在的问题很奇怪,我的原版word里有二个表,当第二个表中文多的话,生成的新word后面就会有乱码尾巴,第二个表中文越多,后面乱码就越多,汗死
为什么会有这个问题呢,不知道先辈们有没有遇到过
网上说,这个未解决中文问题,不知道为什么,我这个可以插入中文的
就是二个表或多个表了,就会有乱码,希望这里有哪位前辈遇到过类似的问题,与解决办法
...全文
相关推荐
poi3.7支持中文编码
java读取excel,ppt,word,支持中文、2003、2007
基于poi实现word/excel转换为HTML(且兼容.doc.docx.xls.xlsx)
基于poi实现word/excel转换为HTML(且兼容.doc.docx.xls.xlsx) 对于简单的word Excel 转换 足够。excel转换时存在火狐浏览器不兼容乱码问题,希望多多交流。
使用POI来处理Excel和Word文件格式
使用POI来处理Excel和Word文件格式
java实现HTML转Word
java实现HTML转Word
Java将doc转docx工具类方法,maven项目,Idea可直接运行
适用于将doc转换成docx maven项目 doc批量转换成docx


Java SE
