Java生成word文档
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文档的更多相关文章
- 使用Java生成word文档(附源码)
当我们使用Java生成word文档时,通常首先会想到iText和POI,这是因为我们习惯了使用这两种方法操作Excel,自然而然的也想使用这种生成word文档.但是当我们需要动态生成word时,通常不 ...
- Java生成 Word文档的并打印解决方案
户要求用程序生成标准的word文档,要能打印,而且不能变形,以前用过很多解决方案,都在客户严格要求下牺牲的无比惨烈. POI读word文档还行,写文档实在不敢恭维,复杂的样式很难控制不提,想象一下一个 ...
- [转载]Java生成Word文档
在开发文档系统或办公系统的过程中,有时候我们需要导出word文档.在网上发现了一个用PageOffice生成word文件的功能,就将这块拿出来和大家分享. 生成word文件与我们编辑word文档本质上 ...
- [原创]Java生成Word文档
在开发文档系统或办公系统的过程中,有时候我们需要导出word文档.在网上发现了一个用PageOffice生成word文件的功能,就将这块拿出来和大家分享. 生成word文件与我们编辑word文档本质上 ...
- poi读写word模板 / java生成word文档
有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 从数据库读取记录替换上述变量 import java.io.FileOutputStream; import java.util ...
- JAVA生成Word文档(经过测试)
首先告诉大家这篇文章的原始出处:http://www.havenliu.com/java/514.html/comment-page-1#comment-756 我也是根据他所描述完成的,但是有一些地 ...
- java使用freemarker 生成word文档
java 生成word文档 最近需要做一个导出word的功能, 在网上搜了下, 有用POI,JXL,iText等jar生成一个word文件然后将数据写到该文件中,API非常繁琐而且拼出来的 ...
- PoiDocxDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0),目前只能java生成】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个是<PoiDemo[Android将表单数据生成Word文档的方案之二(基于Poi4.0.0)]>的扩展,上一篇是根 ...
- FreemarkerJavaDemo【Android将表单数据生成Word文档的方案之一(基于freemarker2.3.28,只能java生成)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个方案只能在java中运行,无法在Android项目中运行.所以此方案是:APP将表单数据发送给后台,后台通过freemarker ...
随机推荐
- easyui源码翻译1.32--ComboBox(下拉列表框)
前言 扩展自$.fn.combo.defaults.使用$.fn.combobox.defaults重写默认值对象.下载该插件翻译源码 下拉列表框显示一个可编辑文本框和下拉式列表,用户可以选择一个值或 ...
- [Quick-x]制作新手引导高亮区域方法之一:混合模式
demo下载:https://github.com/chenquanjun/Quick-x-HighlightArea 1.混合模式 (1)首先创建一个全屏的CCRenderTexture实例 这里使 ...
- 【HDOJ】3509 Buge's Fibonacci Number Problem
快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. /* 3509 */ #inc ...
- JSP个人总结
应用JSP技术开发动态网站 JSP基本语法 默认JSP: <%@ page language="java" contentType="text/html; char ...
- POJ_3273_Monthly_Expense_(二分,最小化最大值)
描述 http://poj.org/problem?id=3273 共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值. Monthly Expense ...
- sql 日期时间格式转换
Sql日期时间格式转换 sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, ...
- EPUB弹出窗口式脚注
网上搜到一些国学典籍的EPUB版,虽有古人的注解,但正文和注解混排在一起,当我只想迅速读正文的时候比较碍眼.于是研究了一下 EPUB3 中有关脚注(footnote)的规格定义,写了一个 Python ...
- HDOJ 1863
#include<stdio.h> #include<string.h> int father[105],depth[105]; int dist[105],map[101][ ...
- Java&MySQL Type Mapping
MySQL Type Name Return value of GetColumnClassName Returned as Java Class BIT(1) (new in MySQL-5.0) ...
- 【HTML】Intermediate4:Tables:rowspan and colspan
1.</th> header cell As with td elements,these must be enclosed inside tr elements 2.</tr co ...