java生成word?

sjd8888 2010-02-08 06:23:13
各位好:

现在我有这么个需求:linux服务器中生成包含表格的word文档。
我尝试了用POI,但是存在乱码问题。请问大家是否有好的方法?谢谢。
...全文
1026 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
csj_9_9 2012-07-20
  • 打赏
  • 举报
回复
有跨平台的开源框架吗?
sjd8888 2010-04-07
  • 打赏
  • 举报
回复
使用Itext可以较好的实现该功能
sjd8888 2010-02-18
  • 打赏
  • 举报
回复
To gesanri,

你好,setEncoding好像是在生成Excel才用到的吧?生成word貌似没用的!3q
  • 打赏
  • 举报
回复
POI是个不错的选择,既然有乱码就要解决乱码问题呗。
hfliyy 2010-02-09
  • 打赏
  • 举报
回复
FileOutputStream、FileInputStream 用于写入诸如图像数据之类的原始字节的流。
处理中文要使用字符流,如FileReader、FileWriter等。
gesanri 2010-02-09
  • 打赏
  • 举报
回复
我记得有个setEncoding方法,你查下API,设成utf-16编码
sjd8888 2010-02-09
  • 打赏
  • 举报
回复
呵呵 试了很多方式都没解决,所以向各位求助了。
sjd8888 2010-02-08
  • 打赏
  • 举报
回复
上面是我目前采用的方法。
sjd8888 2010-02-08
  • 打赏
  • 举报
回复
public static void writeDoc(String path) {
try{
FileOutputStream ostream = new FileOutputStream(path);
FileInputStream in = new FileInputStream("C:\\b.doc");
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);

Range range = hwpf.getRange();
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 = "第" + i + "行第" + j + "列";
int p = td.numParagraphs();
Paragraph para = td.getParagraph(p);
ParagraphProperties pp = new ParagraphProperties();

para.insertBefore(new String(text.getBytes("GB2312")));

}
}
}

//在表格外面插入内容
CharacterProperties cp = new CharacterProperties();
cp.setBold(true);
cp.setCharacterSpacing(10);
cp.setChse(cp.SPRM_CHARSCALE);
cp.setCapitalized(true);

int p = range.numParagraphs();
Paragraph para = range.getParagraph(p - 1);
para.insertAfter("test poi successful!", cp);
para.insertAfter("中文测试", cp);
hwpf.write(ostream);
ostream.close();
}catch(Exception e) {
e.fillInStackTrace();
System.out.println(e.getMessage());
}

}

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧