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);
}
}
分享到:
相关推荐
在日常的开发工作中,我们时常会遇到导出Word文档报表的需求,比如公司的财务报表、医院的患者统计报表、电商平台的销售报表等等。 导出Word方式多种多样,通常有以下几种方式: 1. 使用第三方Java工具类库Hutool的...
java生成word文档完美解决方案.
java生成word的实例 java导出Word文档的实例.rar
最近因项目开发的需要,整理了一份用JAVA导出WORD文档,其部署步骤如下: 1、将jacob-1.14.3-x86.dll放在服务器的系统盘(或运行本机的系统):\WINDOWS\system32目录下。 2、将jacob-1.14.3-x86.dll放在JDK 的 bin ...
java生成word的实例 java导出Word文档的实例 项目说明
使用freemarker技术,首先将word模板另存为xml文档,方便填充数据。然后将后缀名改为ftl,简单方便.
用java生成word文档方法简单方法技巧
java生成word文档 有研究或探讨或开源的请加群:37424970 或联系本人MSN或邮箱:zhuseahui@yahoo.com.cn
博文链接:https://guoyiqi.iteye.com/blog/78638
JAVA生成word文档代码加说明,通过java进行word文档的生成
java:多个图片导出到word文档里显示,用于打印图片导出
java doc,java生成word文档。
java 生成Word文档-模板示例
完整的JAVA导出WORD源代码,解压即可运行
包括freemarker.jar以及使用freemarker导出work文档的方法
Java生成word文档Freemarker教程 详细说明文档,帮组文档
java生成word文档.pdf
java导出word文档 jar包
java导出文件, freemarker导出word需要ftl文件,java导出Word常见问题。2.TXT修改后缀名为.ftl