Android开发——使用Jword生成本地word文档

时间:2023-03-09 17:49:19
Android开发——使用Jword生成本地word文档

本文主要介绍如何使用Jword生成本地word文档,这里涉及到Jword的使用技巧,本文给出相应的代码,需要的朋友可以参考下。

为什么使用Jword呢?因为IText 、Freemark在安卓平台上压根不好使呗!
首先,Jword的网址:[Jword的网址](http://http://www.independentsoft.de/jword/index.html) 下载Jword.zip之后,解压,使用JWord/lib-android中的jword-1.0.jar放到项目中引入。
根据Jword网址中的demo,或者压缩包中也有,来写个demo。
下面以生成一个word表格为例,这样就ok了,测试成功,再根据自己的需求丰富一下就可以了。

WordDocument doc = new WordDocument();

Run run1 = new Run();
run1.addText("Below is one table with 5 rows and 3 columns.");

Paragraph paragraph1 = new Paragraph();
paragraph1.add(run1);

Cell cell1 = new Cell();

Row row1 = new Row();
row1.add(cell1);
row1.add(cell1);
row1.add(cell1); Table table1 = new Table(StandardBorderStyle.SINGLE_LINE);
table1.setWidth(new Width(TableWidthUnit.PERCENT, 100));

table1.add(row1);
table1.add(row1);
table1.add(row1);
table1.add(row1);
table1.add(row1);

doc.getBody().add(paragraph1);
doc.getBody().add(table1); try {
doc.save(getExternalSdCardPath() + "/test.docx");
} catch (IOException e) {
e.printStackTrace();
}

本文转载自:http://www.mobile-open.com/2015/41277.html