android功能需求文档模板,Android实现word模板套打功能

需求:Android打印固定格式的表单。

过程:制作word模板,进行数据填充,打印输出。

步骤一

制作word模板

6e6e224f3ad72f0328065a5b684d334f.png

遇到问题:直接使用word编辑好的模板会导致打印出来后,部分占位符未被替换。因为word保存时会将占位符的标签分割,如下图:

877c911e921eb3799a81f4eccd950eea.png

应修正为

371d032ac208d7eda7c5d0974178fb00.png

具体步骤为:将编辑好的文档“现场检查笔录.docx”,保存为“现场检查笔录.xml”,使用notepad++打开后,进行修正,当然notepad++需要安装xml tool插件进行格式调整。修正后保存为“现场检查笔录.docx”。模板即完成。

打印输出代码如下:

private void initData(InspectionBean.RowsBean bean) {

Mapmap = new HashMap();

if(null != bean){

map.put("$checkTimeStart$", bean.getCheckTimeStart()==null ?"":bean.getCheckTimeStart().substring(0,bean.getCheckTimeStart().indexOf(".")));

map.put("$checkTimeEnd$", bean.getCheckTimeEnd()==null ?"":bean.getCheckTimeEnd().substring(0,bean.getCheckTimeEnd().indexOf(".")));

map.put("$inspectedAdd$", bean.getInspectedAdd());

map.put("$inspectUnitName$", bean.getInspectUnitName() + bean.getInspectedUnitOther());

map.put("$inspectedUnitName$", bean.getInspectedUnitName() + bean.getInspectedUnitOther());

map.put("$inspection$", bean.getOnsiteInspection());

LogUtils.i(TAG,"onsiteInspection="+bean.getOnsiteInspection());

}

try {

//读取示例文书

docName = "现场检查笔录.docx";

InputStream is = getAssets().open(docName);

//读取段落(一般段落,页眉页脚没办法读取)

//自定义的XWPFDocument,解决官方版图片不显示问题

CustomXWPFDocument document = new CustomXWPFDocument(is);

ListlistParagraphs = document.getParagraphs();

processParagraphs(listParagraphs, map);

//读取页脚

// ListfooterList = document.getFooterList();

// processParagraph(footerList, map);

ToastUtils.makeText(InspectionPrintActivity.this, "开始打印", Toast.LENGTH_SHORT).show();

//处理表格

Iteratorit = document.getTablesIterator();

while (it.hasNext()) {//循环操作表格

XWPFTable table = it.next();

Listrows = table.getRows();

for (XWPFTableRow row : rows) {//取得表格的行

Listcells = row.getTableCells();

for (XWPFTableCell cell : cells) {//取得单元格

/* if ("$IMG$".equals(cell.getText())) {

//直接插入图片会在文档的最底端,所以要插在固定位置,要把图片放在表格里

//所以使用判断单元格,并清除单元格放置图片的方式来实现图片定位

cell.removeParagraph(0);

XWPFParagraph pargraph = cell.addParagraph();

document.addPictureData(getAssets().open("1.png"), XWPFDocument.PICTURE_TYPE_PNG);

document.createPicture(document.getAllPictures().size() - 1, 120, 120, pargraph);

}*/

ListparagraphListTable = cell.getParagraphs();

processParagraphs(paragraphListTable, map);

}

}

}

FileOutputStream fopts = new FileOutputStream(newPath);

document.write(fopts);

//打印

doMopriaPrint("/storage/emulated/0/现场检查笔录.docx");

if (fopts != null) {

fopts.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

//处理页脚中的段落,其实就是用方法读取了下页脚中的内容,然后也会当做一般段落处理

private void processParagraph(ListfooterList, Mapmap) {

if (footerList != null && footerList.size() > 0) {

for (XWPFFooter footer : footerList) {

//读取一般段落

Listparagraphs = footer.getParagraphs();

processParagraphs(paragraphs, map);

//处理表格

Listtables = footer.getTables();

for (int i = 0; i < tables.size(); i++) {

XWPFTable xwpfTable = tables.get(i);

Listrows = xwpfTable.getRows();

for (XWPFTableRow row : rows) {//取得表格的行

Listcells = row.getTableCells();

for (XWPFTableCell cell : cells) {//取得单元格

ListparagraphListTable = cell.getParagraphs();

processParagraphs(paragraphListTable, map);

}

}

}

}

}

}

//处理段落

public static void processParagraphs(ListparagraphList, Mapparam) {

if (paragraphList != null && paragraphList.size() > 0) {

for (XWPFParagraph paragraph : paragraphList) {

Listruns = paragraph.getRuns();

for (XWPFRun run : runs) {

String text = run.getText(0);

// Log.i("text",text);

if (text != null) {

boolean isSetText = false;

for (Map.Entryentry : param.entrySet()) {

String key = entry.getKey();

if (text.indexOf(key) != -1) {

isSetText = true;

Object value = entry.getValue();

if (value instanceof String) {//文本替换

text = text.replace(key, value.toString());

}

}

}

if (isSetText) {

run.setText(text, 0);

}

}

}

}

}

}

输出word文档已保存在手机内部存储

/storage/emulated/0/现场检查笔录.docx

如需打印直接手机连上蓝牙或WiFi打印机即可。

用到的poi:

provided files('libs/poi-3.9-20121203.jar')

provided files('libs/poi-ooxml-schemas-3.9-20121203.jar')

provided files('libs/poi-ooxml-3.9-20121203.jar')

provided files('libs/xmlbeans-2.3.0.jar')

provided files('libs/dom4j-1.6.1.jar')

provided files('libs/org.apache.poi.xwpf.converter.core-1.0.0.jar')

provided files('libs/org.apache.poi.xwpf.converter.xhtml-1.0.2.jar')

Demo地址:https://download.csdn.net/download/shuimu_88/11251521

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值