Android实现word模板套打功能

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

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

步骤一

制作word模板

Android实现word模板套打功能_第1张图片

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

Android实现word模板套打功能_第2张图片

应修正为

Android实现word模板套打功能_第3张图片

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

打印输出代码如下:

private void initData(InspectionBean.RowsBean bean) {
        Map map = 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);
            List listParagraphs = document.getParagraphs();
            processParagraphs(listParagraphs, map);

            //读取页脚
//            List footerList = document.getFooterList();
//            processParagraph(footerList, map);
            ToastUtils.makeText(InspectionPrintActivity.this, "开始打印", Toast.LENGTH_SHORT).show();
            //处理表格
            Iterator it = document.getTablesIterator();
            while (it.hasNext()) {//循环操作表格
                XWPFTable table = it.next();
                List rows = table.getRows();
                for (XWPFTableRow row : rows) {//取得表格的行
                    List cells = 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);
                        }*/
                        List paragraphListTable = 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(List footerList, Map map) {
        if (footerList != null && footerList.size() > 0) {
            for (XWPFFooter footer : footerList) {
                //读取一般段落
                List paragraphs = footer.getParagraphs();
                processParagraphs(paragraphs, map);
                //处理表格
                List tables = footer.getTables();
                for (int i = 0; i < tables.size(); i++) {
                    XWPFTable xwpfTable = tables.get(i);
                    List rows = xwpfTable.getRows();
                    for (XWPFTableRow row : rows) {//取得表格的行
                        List cells = row.getTableCells();
                        for (XWPFTableCell cell : cells) {//取得单元格
                            List paragraphListTable = cell.getParagraphs();
                            processParagraphs(paragraphListTable, map);
                        }
                    }
                }
            }
        }
    }

    //处理段落
    public static void processParagraphs(List paragraphList, Map param) {
        if (paragraphList != null && paragraphList.size() > 0) {
            for (XWPFParagraph paragraph : paragraphList) {
                List runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    String text = run.getText(0);
//                    Log.i("text",text);
                    if (text != null) {
                        boolean isSetText = false;
                        for (Map.Entry entry : 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

你可能感兴趣的:(android,studio)