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. 手游:cocos2d-x3.0 移植 wp8 开发 各种 “蛋疼”问题的汇总

    蛋疼的问题的起源: wp8 做应用开发,显示显示中文,源码包含中文都是没有一点问题的, 只是cocos2d-x 的编码方式(UTF-8),引起的一系列的问题. 1:不能显示服务器返回的中文 2:c++ ...

  2. 从 Android 静音看正确的查找 bug 的姿势

    0.写在前面 没抢到小马哥的红包,无心回家了,回公司写篇文章安慰下自己TT..话说年关难过,bug多多,时间久了难免头昏脑热,不辨朝暮,难识乾坤...艾玛,扯远了,话说谁没踩过坑,可视大家都是如何从坑 ...

  3. python描述符descriptor(二)

    python内置的描述符 python有些内置的描述符对象,property.staticmethod.classmethod,python实现如下: class Property(object): ...

  4. jstring 和char 之间的转换方法

    //jstring to char* char* jstringTostring(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsst ...

  5. Rails3.2.3+ruby1.9.3 环境搭建,提示安全警告

    错误描述: 照着教程搭建了Rails的环境,能够正常运行,但是会但一个警告,如下: SECURITY WARNING: No secret option provided to Rack::Sessi ...

  6. JSP个人总结

    应用JSP技术开发动态网站 JSP基本语法 默认JSP: <%@ page language="java" contentType="text/html; char ...

  7. 六月计划#2B(6.10-6.16)

    4/7 STL set 数学 快速傅立叶(FFT) 高斯消元 动态规划 斜率优化

  8. Linux kernel ‘lbs_debugfs_write’函数数字错误漏洞

    漏洞名称: Linux kernel ‘lbs_debugfs_write’函数数字错误漏洞 CNNVD编号: CNNVD-201311-421 发布时间: 2013-11-29 更新时间: 2013 ...

  9. CH Round #17 舞动的夜晚

    舞动的夜晚 CH Round #17 描述 L公司和H公司举办了一次联谊晚会.晚会上,L公司的N位员工和H公司的M位员工打算进行一场交际舞.在这些领导中,一些L公司的员工和H公司的员工之间是互相认识的 ...

  10. Windows下的.NET+ Memcached安装

    转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...