java1349033501 2017-08-31 11:31 采纳率: 0%
浏览 661

poi替换字符串部分失败

poi字符串替换部分失败
public class Xwp07Test {
public static void searchAndReplace(String srcPath, String destPath,

Map map) {

try {

            XWPFDocument document = new XWPFDocument(  
                    POIXMLDocument.openPackage(srcPath)); 


            replaceFooterAndHeader(document);
            // 替换段落中的指定文字  
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();  
            while (itPara.hasNext()) {  
                XWPFParagraph paragraph = (XWPFParagraph) itPara.next();  
                //String s = paragraph.getParagraphText();          
                Set<String> set = map.keySet();  
                Iterator<String> iterator = set.iterator();  
                while (iterator.hasNext()) {  
                    String key = iterator.next();  
                    List<XWPFRun> run=paragraph.getRuns();  
                     for(int i=0;i<run.size();i++)  
                     {  
                      if(run.get(i).getText(run.get(i).getTextPosition())!=null && run.get(i).getText(run.get(i).getTextPosition()).equals(key))  
                      {      
                        /**参数0表示生成的文字是要从哪一个地方开始放置,设置文字从位置0开始 
                         * 就可以把原来的文字全部替换掉了 
                        * */           
                          run.get(i).setText(map.get(key),0);      
                      }      
                     }      
                }      
            }  

            // 替换表格中的指定文字  
            Iterator<XWPFTable> itTable = document.getTablesIterator();  
            while (itTable.hasNext()) {  
                XWPFTable table = (XWPFTable) itTable.next();  
                int rcount = table.getNumberOfRows();  
                for (int i = 0; i < rcount; i++) {  
                    XWPFTableRow row = table.getRow(i);  
                    List<XWPFTableCell> cells = row.getTableCells();  
                    for (XWPFTableCell cell : cells) {  
                        for (Entry<String, String> e : map.entrySet()) {  
                            if (cell.getText().equals(e.getKey())) {  
                                cell.removeParagraph(0);  
                                cell.setText(e.getValue());  
                            }  
                        }  
                    }  
                }  
            }  
            FileOutputStream outStream = null;  
            outStream = new FileOutputStream(destPath);  
            document.write(outStream);  
            outStream.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

 //处理页脚
 public static void replaceFooterAndHeader(XWPFDocument doc){
        List<XWPFParagraph> footers = doc.getHeaderFooterPolicy().getDefaultFooter().getParagraphs();
        List<XWPFParagraph> headers = doc.getHeaderFooterPolicy().getDefaultHeader().getParagraphs();
        //处理页脚
        for (XWPFParagraph paragraph : footers) {
            List<XWPFRun> runs = paragraph.getRuns();
            for (XWPFRun run : runs) {
                String text = run.getText(0);
                System.out.println("text"+text);
                if(StringUtils.isNotEmpty(text)){
                    for(Entry<String, String> entry : map.entrySet()){
                        String key = entry.getKey();
                        System.out.println("key"+key);
                        if(text.indexOf(key) != -1){
                            Object value = entry.getValue();
                            if(value instanceof String){
                                text = text.replace(key, value.toString());
                                run.setText(text,0);
                            }
                        }
                    }
                }
            }
        }
 }

    public static Map<String, String> map = new HashMap<String, String>();  
    public static void main(String[] args) throws Exception {  

        map.put("${contract}", "001");  
        map.put("${sellers}", "普若迪");  
        map.put("${itemName}", "进货");  
        map.put("${tenderId}", "82736");  
        map.put("${mechanismName}", "一汽重工");  
        map.put("${member1}", "我");  
        map.put("${member2}", "你"); 

        map.put("${id}", "1");  
        map.put("${commodityName}", "大众");  
        map.put("${model}", "四座");  
        map.put("${manufacturesName}", "一汽集团");  
        map.put("${origin}", "湖南");  
        map.put("${quantiy}", "20");  
        map.put("${unit}", "200000.00");  
        map.put("${configuration}", "很好"); 
        map.put("${price}", "200000"); 
        map.put("${subtotal}", "4000000.00"); 
        map.put("${id}", "1");  
        map.put("${commodityName}", "大众");  
        map.put("${model}", "四座");  
        map.put("${manufacturesName}", "一汽集团");  
        map.put("${origin}", "湖南");  
        map.put("${quantiy}", "20");  
        map.put("${unit}", "200000.00");  
        map.put("${configuration}", "很好"); 
        map.put("${price}", "200000"); 
        map.put("${subtotal}", "4000000.00"); 

        map.put("${rb}", "肆百万"); 
        map.put("${yq}", "4000000.00"); 
        map.put("${c1}", "001"); 
        map.put("${sj}", "可不可以啊");
        map.put("${loc}", "长沙");
        map.put("${place}", "学校");
        map.put("${mo}", "没问题");
        map.put("${cont}", "001");
        String srcPath = "C:\\Users\\baobao\\工作\\01广州大学货物采购合同(模版)(适用于合同总价20万元以上货物采购)170705.doc";  
        String destPath = "D:\\word\\temp.doc";  
        searchAndReplace(srcPath, destPath, map); 
    }

}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!