자바 워드 문서 생 성 [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();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.