Java导出Word文档(可设置字体)
2017-01-20 17:41
549 查看
原作者:兰博基尼
http://hujinfan.iteye.com/blog/847029
导出内容导Word文档中,可设置字体,亲测可用。
需求jar包:
itext-rtf-2.1.7.jar
itext-2.1.7.jar
http://hujinfan.iteye.com/blog/847029
导出内容导Word文档中,可设置字体,亲测可用。
importjava.io.FileOutputStream; import java.io.IOException; import java.util.List; importcom.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.rtf.RtfWriter2; public class WordUtil { private static Document document; private static BaseFont baseFont; //创建word,并设置纸张文档 private staticvoid openWordFile(String fileName) throws DocumentException, IOException{ document = newDocument(PageSize.A4); RtfWriter2.getInstance(document,new FileOutputStream(fileName)); document.open(); baseFont =BaseFont.createFont(); } //设置标题 private staticboolean setTitle(String title) throws DocumentException { Font font = new Font(baseFont,12, Font.BOLD); Paragraph pTitle = newParagraph(title + "\n"); pTitle.setFont(font); pTitle.setAlignment(Element.ALIGN_CENTER); returndocument.add(pTitle); } //设置文档内容 private staticboolean setContent(String content) throws DocumentException { Font font = new Font(baseFont,10, Font.NORMAL); Paragraph pContent = newParagraph(content); pContent.setFont(font); pContent.setAlignment(Element.ALIGN_LEFT); pContent.setSpacingAfter(5); pContent.setFirstLineIndent(20); returndocument.add(pContent); } //对外使用的接口 public staticboolean CreateWordFile(String url, String title, Listcontents) { boolean returnValue =false; try { openWordFile(url); returnValue =setTitle(title); for (int i =0; i < contents.size(); i++) { returnValue= returnValue && setContent(contents.get(i)); } document.close(); } catch (DocumentException e){ // TODOAuto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODOAuto-generated catch block e.printStackTrace(); } returnreturnValue; } //对外使用的接口 public static boolean CreateWordFile(String url,String title, Stringcontent) { boolean returnValue =false; try { openWordFile(url); returnValue =setTitle(title); returnValue =returnValue && setContent(content); document.close(); } catch (DocumentException e){ // TODOAuto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODOAuto-generated catch block e.printStackTrace(); } returnreturnValue; } public static void main(String[]args ) { WordUtil wordUtil = newWordUtil(); List strList = newArrayList(); //传入内容为字符串 wordUtil.CreateWordFile("e:\\word.doc","标题居中", "我爱Java"); //传入内容为字符串List //wordUtil.CreateWordFile("e:\\word.doc","标题居中", strList); } }
需求jar包:
itext-rtf-2.1.7.jar
itext-2.1.7.jar
相关文章推荐
- Java语言结合Freemaker技术导出Word文档
- 在Java中导出word、excel格式文件时JSP页面头的设置
- JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦
- Java导出Word文档使用iText操作
- java开发导出Excel和Word文档
- Java导出Word文档
- Java导出word/execl文档
- Java POI导出Word文档
- Java使用freemarker导出word文档
- java freemaker 导出word文档 高逼格教程
- JAVA不使用POI,用PageOffice动态导出Word文档
- JAVA Freemarker 导出word文档实例带有图片
- Java用freemarker导出word文档
- java使用freemarker模板导出word(带有合并单元格)文档
- Java导出Word文档案例
- JAVA 将数据导出Word文档(经过测试)
- java实现的导出word文档
- Itext设置导出word文件的字体
- java 导出word格式的文档
- Java实现用Freemarker完美导出word文档(带图片)