자바 워드 문서 생 성 [1]

/**
 * <p>expWordByFtl      -    (freemarker)        word.</p>
 * <p>   -    ,          .</p>
 * <p>    -1、      ,2、   xml  ,3、   xml  ftl,4、  freemarker    ftl  .</p>
 * <p>   jar-freemarker-2.3.19.jar.<br>
 * import freemarker.template.Configuration;<br>
 * import freemarker.template.Template;<br>
 * import freemarker.template.TemplateException;
 * </p>
 * <p>
 *      Jul 3, 2015 - 9:50:12 PM
 * </p>
 * @param contentMap    (           )
 * @param filePath      
 * @throws IOException 
 * @throws TemplateException 
 */
private static void expWordByFtl(Map<String, Object> contentMap, String filePath) throws IOException, TemplateException {
	Configuration configuration = new Configuration();
	configuration.setDefaultEncoding("UTF-8");
	configuration.setDirectoryForTemplateLoading(new File("D:/"));
	File file = new File(filePath);
	Template tpl = configuration.getTemplate("art_tbl.ftl", "UTF-8");
	Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), 1024);
	tpl.process(contentMap, writer);
	writer.close();
}

 
/**
 * <p>expWordByPoi      -  poi  html   word.</p>
 * <p>   -  html  word,            ,        “  -    ”.</p>
 * <p>   jar-poi-3.9.jar.<br>
 * import org.apache.poi.poifs.filesystem.DirectoryEntry;<br>
 * import org.apache.poi.poifs.filesystem.DocumentEntry;<br>
 * import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 * </p>
 * <p>
 *      Jul 3, 2015 - 9:39:34 PM
 * </p>
 * @param content html    
 * @param filePath      
 * @throws IOException 
 */
private static void expWordByPoi(String content, String filePath) throws IOException {
	byte[] buf = content.getBytes();
	ByteArrayInputStream bais = new ByteArrayInputStream(buf);
	POIFSFileSystem poifs = new POIFSFileSystem();
	DirectoryEntry directory = poifs.getRoot();
	DocumentEntry document = directory.createDocument("WordDocument", bais);
	FileOutputStream fos = new FileOutputStream(filePath);
	poifs.writeFilesystem(fos);
	File file = new File(filePath);
	//     
	bais.close();
	fos.close();
}
/**
 * <p>expWordByItext_Html      -  rtf    html   word.</p>
 * <p>   -  html    word,              .</p>
 * <p>   jar-itext-2.1.7.jar、itext-rtf-2.1.7.jar.<br>
 * import com.lowagie.text.Document;<br>
 * import com.lowagie.text.DocumentException;<br>
 * import com.lowagie.text.Element;<br>
 * import com.lowagie.text.HeaderFooter;<br>
 * import com.lowagie.text.Paragraph;<br>
 * import com.lowagie.text.Phrase;<br>
 * import com.lowagie.text.Rectangle;<br>
 * import com.lowagie.text.html.simpleparser.HTMLWorker;<br>
 * import com.lowagie.text.html.simpleparser.StyleSheet;<br>
 * import com.lowagie.text.rtf.RtfWriter2;
 * </p>
 * <p>
 *      Jul 3, 2015 - 9:18:47 PM
 * </p>
 * @param content html    
 * @param filePath      
 * @throws IOException 
 * @throws DocumentException 
 */
private static void expWordByItext_Html(String content, String filePath) throws IOException, DocumentException {
	//     
	OutputStream out = new FileOutputStream(filePath);
	Document doc = new Document(com.lowagie.text.PageSize.A4);
	RtfWriter2.getInstance(doc, out);
	doc.open();
	//       
	HeaderFooter header = new HeaderFooter(new Phrase("    !"), false);
	header.setAlignment(Rectangle.ALIGN_CENTER);
	doc.setHeader(header);
	//       
	Paragraph context = new Paragraph();
	StyleSheet ss = new StyleSheet();
	List<Element> htmlList = HTMLWorker.parseToList(new StringReader(content), ss);
	for (Element e : htmlList) {
		context.add(e);
	}
	doc.add(context);
	//     
	doc.close();
	out.close();
}

 
 
 
 

좋은 웹페이지 즐겨찾기