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 ...
随机推荐
- cat主要有三大功能
cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename 只能创建新文件,不能编辑已有文件.3.将几个文件合并为一 ...
- wcf教程
WCF Tutorial WCF stands for Windows Communication Foundation. It is a framework for building, config ...
- Mysql规范和使用注意点(转)
命名规范: 1表名,字段名,索引名称使用小写字母,数字采用下划线进行分割 2.表名采用模块名3个缩小字符 '前缀'之后顺序为表明 3.表名,字段名不超过32个字符 4.存储尸体数据的表,名称使用名词, ...
- Automator一键生成所需的iOS 图片icon
iOS到8了, 终于受不了它的各种尺寸的icon了. 写一个Finder服务来一键生成吧. 拖放几次再重复, 无技术含量, 但很有用. // 存放目录 ~/资源库/Services/
- 【转】CString类型互转 int
CString类型互转 int 原文网址:http://www.cnitblog.com/Hali/archive/2009/06/25/59632.html CString类型的转换成int 将字 ...
- zz-rtl8188eu的linux-usb-wifi调试及驱动编译150210
//zz//####################################################################### zz-rtl8188eu的linux-usb ...
- 一行代码搞定Adapter
15年Google I/O大会发不了三个重要支持库 >Material design (Android Support Design) >百分比布局:Percent support lib ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.1
Given any $k$-tupel of linearly independent vectors $X$ as above, there exists a $k$-tuple $Y$ biort ...
- NET中的引用类型和值类型 zt
.NET中的类型分为值类型和引用类型,他们在内存布局,分配,相等性,赋值,存储以及一些其他的特性上有很多不同,这些不同将会直接影响到我们应用程序 的效率.本文视图对.NET 基础类型中的值类型和引用类 ...
- Oracle.ManagedDataAccessDTC.dll 使用
ODP.NET, Managed Driver Setup This section explains the setup and configuration steps required for u ...