java 读写 模板文件-分享

来源:互联网 发布:摄影测量数据处理软件 编辑:程序博客网 时间:2024/05/01 00:10


    博客分类:


        Map<String, String> map = new HashMap<String, String>();  
        map.put("${sub}", "湖南大学");  
        map.put("${item2.school}", "湖南大学");  
        map.put("${item2.address}", "湖南");  
//        map.put("${item1.name}", "王五1");  
//        map.put("${item1.numberStudent}", "编号002");  
//        map.put("${item1.sex}", "男2");  
//        map.put("${item1.age}", "19");  
        String srcPath = "D:\\铸造工艺卡.doc";  
        readwriteWord(srcPath, map);  
    }  
      

伦理片 http://www.dotdy.com/


    * 实现对word读取和修改操作 
    *  
    * @param filePath 
    *            word模板路径和名称 
    * @param map 
    *            待填充的数据,从数据库读取 
    */  
    public static void readwriteWord(String filePath, Map<String, String> map)  
    {  
        // 读取word模板  
        // String fileDir = new  
        // File(base.getFile(),"http://www.cnblogs.com/http://www.cnblogs.com/../doc/").getCanonicalPath();  
        FileInputStream in = null;  
        try  
        {  
            in = new FileInputStream(new File(filePath));  
        }  
        catch (FileNotFoundException e1)  
        {  
            e1.printStackTrace();  
        }  
        HWPFDocument hdt = null;  
        try  
        {  
            hdt = new HWPFDocument(in);  
        }  
        catch (IOException e1)  
        {  
            e1.printStackTrace();  
        }  
        Fields fields = hdt.getFields();  
        Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN)  
                .iterator();  
        while (it.hasNext())  
        {  
            System.out.println(it.next().getType());  
        }  
          
        //读取word文本内容  
        Range range = hdt.getRange();  
        TableIterator tableIt = new TableIterator(range);   
        //迭代文档中的表格  
        int ii = 0;  
        while (tableIt.hasNext()) {    
            Table tb = (Table) tableIt.next();    
            ii++;  
            System.out.println("第"+ii+"个表格数据...................");  
            //迭代行,默认从0开始  
            for (int i = 0; i < tb.numRows(); i++) {    
                TableRow tr = tb.getRow(i);    
                //只读前8行,标题部分  
                if(i >=8) break;  
                //迭代列,默认从0开始  
                for (int j = 0; j < tr.numCells(); j++) {    
                    TableCell td = tr.getCell(j);//取得单元格  
                    //取得单元格的内容  
                    for(int k=0;k<td.numParagraphs();k++){    

影音先锋电影 http://www.iskdy.com/      
        // 替换文本内容  
        for (Map.Entry<String, String> entry : map.entrySet())  

    

  • poi-3.8.rar (7.1 MB)
0 0
原创粉丝点击