fremark導出word,帶圖片的

package mil.bordercontrol.maintest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.ResourceUtils;

import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class WordTest {
	
	public static void main(String args[]){
		WordTest t=new WordTest();
		t.write();
	}

	public void write() {
		try {
			OutputStream os =  new FileOutputStream("c:\\data.doc");
			OutputStreamWriter oWriter = new OutputStreamWriter(os,"UTF-8");
			Configuration cfg=new Configuration();
			cfg.setDirectoryForTemplateLoading(ResourceUtils.getFile("E:\\wordmode"));
			cfg.setEncoding(cfg.getLocale(), "UTF-8");
			Template temp=cfg.getTemplate("intelligence1.xml");
			Writer out=new BufferedWriter(oWriter);
			temp.process(getRootWord(), out);
			out.flush();
			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Map<String ,String> getRootWord(){
		Map<String, String> root=new HashMap<String, String>();
		root.put("title", "title");
		root.put("report", "report");
		root.put("editorName", "editorName");
		root.put("checkName", "checkName");
		root.put("subscribeName", "subscribeName");
		root.put("time", "2016年03月23日09時");
		root.put("unitName", "unitName");
		root.put("number", "121");
		root.put("unitNameWS", "unitNameWS");
		root.put("exportDateTime", "2016年03月23日09時");
		root.put("allTransactionCase", "allTransactionCase");
		root.put("content", "content");
		root.put("imgs", intelligenceWord());
		return root;
	}
	
	public String intelligenceWord(){
		String str="123";
		String width="250";
		String height="280";
		String fileName="a.jpg";
		BASE64Encoder encoder = new BASE64Encoder();
		File file=new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg");
		InputStream input=null;
		byte[] fileBytes=new byte[(int) file.length()];
		try {
			input=new FileInputStream(file);
			input.read(fileBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
				try {
					if(input!=null)
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		str="<w:p wsp:rsidR=\"00B168E8\" wsp:rsidRPr=\"00F66CC4\" wsp:rsidRDefault=\"00860288\" wsp:rsidP=\"00860288\"><w:pPr><w:spacing w:line=\"360\" w:line-rule=\"auto\"/><w:rPr><w:rFonts w:ascii=\"仿宋_GB2312\" w:fareast=\"仿宋_GB2312\" w:h-ansi=\"Times New Roman\"/><wx:font wx:val=\"仿宋_GB2312\"/><w:sz w:val=\"32\"/><w:sz-cs w:val=\"32\"/><w:u w:color=\"FF0000\"/></w:rPr></w:pPr>";
		str+="<w:r><w:rPr><w:rFonts w:ascii=\"仿宋_GB2312\" w:fareast=\"仿宋_GB2312\" w:hint=\"fareast\"/><wx:font wx:val=\"仿宋_GB2312\"/><w:sz w:val=\"32\"/><w:sz-cs w:val=\"32\"/></w:rPr><w:t>  </w:t></w:r><w:r><w:rPr><w:rFonts w:ascii='仿宋_GB2312' w:fareast='仿宋_GB2312' w:hint='fareast'/><wx:font wx:val='仿宋_GB2312'/><w:sz w:val='28'/><w:sz-cs w:val='28'/></w:rPr><w:pict><w:binData w:name='wordml://"+fileName+"'>"+encoder.encodeBuffer(fileBytes).trim()+"</w:binData><v:shape id='_x0000_i1025' type='#_x0000_t75' style='width:"+width+"px;height:"+height+"px'><v:imagedata src='wordml://"+fileName+"' o:title='"+fileName+"'/></v:shape></w:pict></w:r>";
		str+="</w:p>";
		return str;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章