Java的操作word文档.docVIP

  • 2
  • 0
  • 约2.63千字
  • 约 4页
  • 2017-05-04 发布于四川
  • 举报
Java的操作word文档

Java操作Word文档 操作微软word办公软件的开发工具: Apache基金会提供的POI 通过freemarker去解析xml Java2word iText Jacob 通过对以上工具的对比,本人发现还是Itext比较简单易用,很容易上手,能够很轻松的处理word的样式、表格等。 贴上代码,供大家参考: Jar包准备: itext-2.0.1.jar 核心包 iTextAsian.jar解决word样式、编码问题扩展包 1、设置标题样式 public static Paragraph setParagraphTitle(String content,Font contentFont){ Paragraph p = new Paragraph(content, contentFont); p.setAlignment(Table.ALIGN_CENTER); p.setIndentationLeft(60); p.setIndentationRight(60); p.setSpacingBefore(20); return p; } 2、设置内容样式: public static Paragraph setParagraphStyle(String content,Font contentFont){ Paragraph p = new Paragraph(content, contentFont); p.setFirstLineIndent(40);// 首行缩进 p.setAlignment(Paragraph.ALIGN_JUSTIFIED);// 对齐方式 p.setLeading(30);// 行间距 p.setIndentationLeft(60);// 左边距,右边距 p.setIndentationRight(60); return p; } 3、设置文档末尾时间: public static Paragraph setParagraphTime(Font contentFont){ Paragraph p = new Paragraph(FormatUtil.getCurrentDate(), contentFont); p.setIndentationLeft(250); p.setIndentationRight(60); p.setLeading(30); p.setFirstLineIndent(40); return p; } 4、开始写word文档咯: public static void WriteDoc(String path,MapString,String map){ Document document = null; try { File file = new File(path); if (!file.exists()) { file.createNewFile(); } document = new Document(PageSize.A4); RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 设置title body 中文字体 及样式 BaseFont cnFont = BaseFont.createFont(STSongStd-Light,UniGB-UCS2-H, BaseFont.NOT_EMBEDDED); Font titleFont = new Font(cnFont,22, Font.NORMAL, new Color(0,0, 0)); Font contentFont = new Font(cnFont, 16, Font.NORMAL, new Color(0,0, 0)); // 设置文本标题 String titleInfo = “标题”; // 设置文本内容 String contentFirst =啊啊啊啊啊啊啊啊啊啊; String contentSecond=啊啊啊啊啊啊啊啊啊啊啊啊; String contentThird=啊啊啊啊啊啊啊啊啊啊啊啊啊; String contentFourth=啊啊啊啊啊啊啊啊啊啊; document.add(setParagraphTitle(titleInfo,titleFont)); document.add(setParagraph

文档评论(0)

1亿VIP精品文档

相关文档