/**
*
expWordByFtl方法主要用于-采用模板(freemarker)的方式导出数据到word.
*
优缺点-操作方便,但是设置模板有些繁杂.
*
使用方法-1、建立模板文档,2、另存为xml文件,3、将格式xml改为ftl,4、采用freemarker语法编辑ftl文件.
*
第三方jar-freemarker-2.3.19.jar.
* import freemarker.template.Configuration;
* import freemarker.template.Template;
* import freemarker.template.TemplateException;
*
*
* 山河戀夢 Jul 3, 2015 - 9:50:12 PM
*
* @param contentMap 数据源(模板中用到的数据键值对)
* @param filePath 导出的文档
* @throws IOException
* @throws TemplateException
*/
private static void expWordByFtl(Map 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();
}
/**
*
expWordByPoi方法主要用于-采用poi导出html文档到word.
*
优缺点-可将html生成word,没找到设置页眉页脚的方法,导出的文档需点击“视图-页面视图”.
*
第三方jar-poi-3.9.jar.
* import org.apache.poi.poifs.filesystem.DirectoryEntry;
* import org.apache.poi.poifs.filesystem.DocumentEntry;
* import org.apache.poi.poifs.filesystem.POIFSFileSystem;
*
*
* 山河戀夢 Jul 3, 2015 - 9:39:34 PM
*
* @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();
}
/**
*
expWordByItext_Html方法主要用于-采用rtf方式导出html文档到word.
*
优缺点-可将html文档生成word,可设置页眉页脚等其他文档属性.
*
第三方jar-itext-2.1.7.jar、itext-rtf-2.1.7.jar.
* import com.lowagie.text.Document;
* import com.lowagie.text.DocumentException;
* import com.lowagie.text.Element;
* import com.lowagie.text.HeaderFooter;
* import com.lowagie.text.Paragraph;
* import com.lowagie.text.Phrase;
* import com.lowagie.text.Rectangle;
* import com.lowagie.text.html.simpleparser.HTMLWorker;
* import com.lowagie.text.html.simpleparser.StyleSheet;
* import com.lowagie.text.rtf.RtfWriter2;
*
*
* 山河戀夢 Jul 3, 2015 - 9:18:47 PM
*
* @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 htmlList = HTMLWorker.parseToList(new StringReader(content), ss);
for (Element e : htmlList) {
context.add(e);
}
doc.add(context);
// 关闭资源
doc.close();
out.close();
}