itext-rtf-2.1.7.jar,下载地址:http://download.csdn.net/detail/xuxu198899223/7717727



package word;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.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,并设置纸张文档
* @param filePath 文档路径
* @throws DocumentException
* @throws IOException
*/
private static void openWordFile(String filePath) throws DocumentException, IOException {
document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream(filePath));
document.open();
baseFont = BaseFont.createFont();
} /**
* 设置标题
* @param title 标题
* @return
* @throws DocumentException
*/
private static boolean setTitle(String title) throws DocumentException {
Font font = new Font(baseFont, 12, Font.BOLD);
Paragraph pTitle = new Paragraph(title + "\n");
pTitle.setFont(font);
pTitle.setAlignment(Element.ALIGN_CENTER);
return document.add(pTitle);
} /**
* 设置文档内容
* @param content文档内容
* @return
* @throws DocumentException
*/
private static boolean setContent(String content) throws DocumentException {
Font font = new Font(baseFont, 10, Font.NORMAL);
Paragraph pContent = new Paragraph(content);
//设置字体
pContent.setFont(font);
pContent.setAlignment(Element.ALIGN_LEFT);
pContent.setSpacingAfter(5);
pContent.setFirstLineIndent(20);
return document.add(pContent);
} /**
* 创建丰富内容的word文档
* @param filePath 文档保存地址
* @param title 文档标题
* @param contents 文档内容
* @return
*/
public static boolean CreateWordFile(String filePath, String title, List<String> contents) {
boolean returnValue = false;
try {
openWordFile(filePath);
returnValue = setTitle(title);
for (int i = 0; i < contents.size(); i++) {
returnValue = returnValue && setContent(contents.get(i));
}
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return returnValue;
} /**
* 创建单一内容的word文档
* @param filePath 文档保存地址
* @param title 文档标题
* @param content 文档内容
* @return
*/
public static boolean CreateWordFile(String filePath, String title, String content) {
boolean returnValue = false;
try {
openWordFile(filePath);
returnValue = setTitle(title);
returnValue = returnValue && setContent(content);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return returnValue;
} @SuppressWarnings({ "unused", "static-access" })
public static void main(String[] args) {
WordUtil wordUtil = new WordUtil();
List<String> strList = new ArrayList<String>();
//传入内容为字符串
wordUtil.CreateWordFile("C:\\word.doc", "标题居中", "我爱Java");
//传入内容为字符串List
//wordUtil.CreateWordFile("e:\\word.doc", "标题居中", strList);
}
}

Java生成word文档的更多相关文章

  1. 使用Java生成word文档(附源码)

    当我们使用Java生成word文档时,通常首先会想到iText和POI,这是因为我们习惯了使用这两种方法操作Excel,自然而然的也想使用这种生成word文档.但是当我们需要动态生成word时,通常不 ...

  2. Java生成 Word文档的并打印解决方案

    户要求用程序生成标准的word文档,要能打印,而且不能变形,以前用过很多解决方案,都在客户严格要求下牺牲的无比惨烈. POI读word文档还行,写文档实在不敢恭维,复杂的样式很难控制不提,想象一下一个 ...

  3. [转载]Java生成Word文档

    在开发文档系统或办公系统的过程中,有时候我们需要导出word文档.在网上发现了一个用PageOffice生成word文件的功能,就将这块拿出来和大家分享. 生成word文件与我们编辑word文档本质上 ...

  4. [原创]Java生成Word文档

    在开发文档系统或办公系统的过程中,有时候我们需要导出word文档.在网上发现了一个用PageOffice生成word文件的功能,就将这块拿出来和大家分享. 生成word文件与我们编辑word文档本质上 ...

  5. poi读写word模板 / java生成word文档

    有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 从数据库读取记录替换上述变量 import java.io.FileOutputStream; import java.util ...

  6. JAVA生成Word文档(经过测试)

    首先告诉大家这篇文章的原始出处:http://www.havenliu.com/java/514.html/comment-page-1#comment-756 我也是根据他所描述完成的,但是有一些地 ...

  7. java使用freemarker 生成word文档

      java 生成word文档     最近需要做一个导出word的功能, 在网上搜了下, 有用POI,JXL,iText等jar生成一个word文件然后将数据写到该文件中,API非常繁琐而且拼出来的 ...

  8. PoiDocxDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0),目前只能java生成】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个是<PoiDemo[Android将表单数据生成Word文档的方案之二(基于Poi4.0.0)]>的扩展,上一篇是根 ...

  9. FreemarkerJavaDemo【Android将表单数据生成Word文档的方案之一(基于freemarker2.3.28,只能java生成)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个方案只能在java中运行,无法在Android项目中运行.所以此方案是:APP将表单数据发送给后台,后台通过freemarker ...

随机推荐

  1. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程

    一. 1. 2.pizza-flow.xml <?xml version="1.0" encoding="UTF-8"?> <flow xml ...

  2. Android:Fragment+ViewPager实现Tab滑动

    public class FragAdapter extends FragmentPagerAdapter { private List<Fragment> fragments ; pub ...

  3. ide远程调试

    这篇写得好:http://qifuguang.me/2015/09/18/IntelliJ%E8%BF%9C%E7%A8%8B%E8%B0%83%E8%AF%95%E6%95%99%E7%A8%8B/

  4. lnmp.org一键安装包

    LNMP安装快速导航:LNMP安装提示,LNMP安装教程.安装失败处理.虚拟主机管理.可选组件.LNMP文件目录说明.状态管理. 系统需求: CentOS/Debian/Ubuntu Linux系统 ...

  5. Understanding Network Class Loaders

    By Qusay H. Mahmoud, October 2004     When Java was first released to the public in 1995 it came wit ...

  6. [Hadoop源码解读](二)MapReduce篇之Mapper类

    前面在讲InputFormat的时候,讲到了Mapper类是如何利用RecordReader来读取InputSplit中的K-V对的. 这一篇里,开始对Mapper.class的子类进行解读. 先回忆 ...

  7. ☀【Zepto】

    http://zeptojs.com/ https://github.com/madrobby/zepto Zepto 中文手册 http://www.360weboy.com/handbook/ze ...

  8. Maximum Flow Exhaustion of Paths Algorithm

    参考youtube上的视频: http://www.youtube.com/watch?v=sxyCzzUuXLo 笔记: 只要是那条路上为0后,就不会再走那条路. 所以没有S->U->W ...

  9. [codevs3296]有序数组合并

    题目描述 Description 合并两个有序数组A和B,使得结果依然有序. 进阶:合并两个有序数组A和B,假设A有n个数,B有m个数,A数组后面还有m个空余空间,需要将结果保存在A中. 请使用O(n ...

  10. 【CSS】Beginner6:Border

    1.border-style:solid实线,dashed虚线,double双线,dotted点状,groove凹槽,ridge垄状,inset,outset 2.border0-width:bord ...