iText、poi操作word2007(读取,生成)
关于生成word文件以及插入文字、表格、图片等功能,我使用了poi和itext,因为poi插入图片的jar包我在网上查并不是太完全,也可能我没找到如何使用,所以插入图片我用的是itext
iText所需jar包,在pom文件中配置
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.2</version>
</dependency>
1、创建word2007文件并插入表格(poi,所需jar包在我其他博客里有对poi的介绍,或去网上查)
//创建word2007文件插入表格
public void createWord2007() {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p1 = doc.createParagraph(); XWPFTable table = doc.createTable(11, 4);
// CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();
CTTblPr tblPr = table.getCTTbl().getTblPr();
tblPr.getTblW().setType(STTblWidth.DXA);
tblPr.getTblW().setW(new BigInteger("7000")); // 设置上下左右四个方向的距离,可以将表格撑大
table.setCellMargins(20, 20, 20, 20); // 表格
List<XWPFTableCell> tableCells = table.getRow(0).getTableCells(); XWPFTableCell cell = tableCells.get(0);
XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
XWPFRun run = newPara.createRun();
/** 内容居中显示 **/
newPara.setAlignment(ParagraphAlignment.CENTER);
run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000红色*/
run.setUnderline(UnderlinePatterns.THICK);
run.setText("第1行1列");
tableCells.get(1).setText("第1行2列");
tableCells.get(2).setText("第1行3列");
tableCells.get(3).setText("第1行4列");
tableCells = table.getRow(1).getTableCells();
tableCells.get(0).setText("第2行1列");
tableCells.get(1).setText("第2行2列");
tableCells.get(2).setText("第2行3列");
tableCells.get(3).setText("第2行4列"); // 设置字体对齐方式
p1.setAlignment(ParagraphAlignment.CENTER);
p1.setVerticalAlignment(TextAlignment.TOP); // 第一页要使用p1所定义的属性
XWPFRun r1 = p1.createRun(); // 设置字体是否加粗
r1.setBold(true);
r1.setFontSize(20); // 设置使用何种字体
r1.setFontFamily("Courier"); // 设置上下两行之间的间距
r1.setTextPosition(20);
r1.setText("标题"); FileOutputStream out;
try {
out = new FileOutputStream("d:/word2007.docx");
// 以下代码可进行文件下载
// response.reset();
// response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
// response.setHeader("Content-Disposition",
// "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
// OutputStream out = response.getOutputStream();
// this.doc.write(out);
// out.flush(); doc.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("success");
}
2、插入图片,文字(iText)
public void exportImg() {
Document document = null;
try {
document = new Document();
RtfWriter2.getInstance(document, new FileOutputStream("D:/word2007.doc"));
document.open();
Paragraph title = new Paragraph("你好,Word!");
document.add(title);
try {
Image image = Image.getInstance("C:\\Users\\zhxn\\Desktop\\1.jpg");
//插入一个图片
document.add(image);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close();
}
}
}
iText、poi操作word2007(读取,生成)的更多相关文章
- Java POI 操作Excel(读取/写入)
pom.xml依赖: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...
- java poi 操作
Java POI 操作Excel(读取/写入) https://www.cnblogs.com/dzpykj/p/8417738.html Java操作Excel之Poi基本操作 https://my ...
- poi操作excel2007(读取、生成、编辑)
因为现在再写excel2003版的比较low,所以我在这就不介绍了,直接介绍2007,我所用的编程软件是IDEA poi操作office总共有6个jar包,在pom.xml文件中配置如下,也可下载后直 ...
- Java使用poi从数据库读取数据生成Excel表格
想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...
- POI操作Excel详解,读取xls和xlsx格式的文件
package org.ian.webutil; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- POI操作Excel
POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...
- 使用POI操作Excel时对事先写入模板的公式强制执行
场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...
- 利用POI操作不同版本号word文档中的图片以及创建word文档
我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
随机推荐
- Cannot Allocate New Log
http://www.itpub.net/thread-1842569-1-1.html http://czmmiao.iteye.com/blog/2277675
- CF864A Fair Game
CF864A Fair Game 题意翻译 CF864A Fair Game 题意: Petya和Vasya决定玩一个游戏,他们有偶数张卡片,每张卡片上一个数字.每人选择一个数字(两个人选择的数字不能 ...
- Gradle:构建脚本概要
一.构建块 1.每一个构建块都包括三个基本构建块:project.task和property: 2.每一个构建块包括至少一个project,进而又包括一个或多个task: 3.project和task ...
- POJ-3984-迷宫问题-BFS(广搜)-手写队列
题目链接:id=3984">http://poj.org/problem? id=3984 这个本来是个模板题,可是老师要去不能用STL里的queue,得自己手写解决.ORZ....看 ...
- 路由器wiff设置
1.将一根网线连接至路由wankou 2.将另外一根网页连接1.2.3.4口中一个,另外一个连接至电脑 3.登录192.168.1.1,进行设置向导选择ppoe,然后登录网络设置无线名称+密码 4.保 ...
- spring Batch实现数据库大数据量读写
spring Batch实现数据库大数据量读写 博客分类: spring springBatchquartz定时调度批处理 1. data-source-context.xml <?xml v ...
- 在linux環境下安裝jprofiler_linux_8_0_2.sh
1.安装jprofiler_linux_8_0_2.sh chmod+x jprofiler_linux_8_0_2.sh ./jprofiler_linux_8_0_2.sh –c 安装过程略..差 ...
- VC 对话框程序加入工具栏button图标及其buttontooltip
注意:本人使用VC++2010开发环境进行測试. 在使用VC开发对话框程序时不像开发单文档程序和多文档程序那么方便,非常多资源都须要自己手动加入.近期在开发一个程序时.想尝试在对话框程序里面加入 工具 ...
- 读配置文件能够保持顺序的 Java Properties 类
序 前几天,公司项目中有一个需求是读取配置文件的.并且最好可以保证载入到内存中的顺序可以和配置文件里的顺序一致,可是.假设使用 jdk 中提供的 Properties 类的话,读取配置文件后.载入到内 ...
- 一个Python项目的创建架构
要进行Python项目的编写,很多人刚开始一筹莫展,不知道该如何去构建一个项目,现在粗略的描述一下一个项目的创建过程,供大家参考了解一下: 大家可以先忽略其中创建的函数 ,每个包的含义都有定义,大家可 ...