zoukankan      html  css  js  c++  java
  • [置顶] java通过poi操纵word

    最近几天公司需要使用java处理报表,显示成word格式。
    有很多种处理方式,我采用了poi的处理。今天在做demo的时候遇到问题:word中的内容除了图片之外都可以读取到,然后我使用range.replaceText("ak", "自己人");替换word中的ak。打印代码显示成功替换,但是为什么我查看word,里面什么也没有啊。具体代码粘贴如下:
    public class PoiDemo {
    	public static void main(String[] args) {
    //		writeDoc2("D:\aaa.doc");
    		try {
    			HWPFDocument document=new HWPFDocument(new FileInputStream("D:\aaa.doc"));
    			Range range=document.getRange();
    			range.replaceText("ak", "自己人");
    			String str=range.text();
    			System.out.println("--------->"+str);
    			writeDoc("D:\aaa.doc",str);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static boolean writeDoc(String path,String string){
    		boolean w=false;
    		byte b[]=string.getBytes();
    		FileOutputStream fs;
    		try {
    			fs = new FileOutputStream("D:\aaa.doc");
    
    			HWPFOutputStream  hos=new HWPFOutputStream();
    			hos.writeTo(fs);
    			hos.close();
    			w=true;
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		System.out.println("------->"+w);
    		return w;
    	}
    }
    当然你要上官网下载poi的jar。期待有高人知道问题的答案。
  • 相关阅读:
    document对象补充
    JavaScript(DOM操作)(Window.document对象)
    DOM、Window操作
    JavaScript基础
    格式布局
    洛谷P2756 飞行员配对方案问题
    洛谷P2526 【SHOI2001】小狗散步
    洛谷P1129 【ZJOI2007】矩阵游戏
    洛谷P1640 【SCOI2010】连续攻击游戏
    二分图
  • 原文地址:https://www.cnblogs.com/pigga/p/10098234.html
Copyright © 2011-2022 走看看