关于生成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(读取,生成)的更多相关文章

  1. Java POI 操作Excel(读取/写入)

    pom.xml依赖: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  2. java poi 操作

    Java POI 操作Excel(读取/写入) https://www.cnblogs.com/dzpykj/p/8417738.html Java操作Excel之Poi基本操作 https://my ...

  3. poi操作excel2007(读取、生成、编辑)

    因为现在再写excel2003版的比较low,所以我在这就不介绍了,直接介绍2007,我所用的编程软件是IDEA poi操作office总共有6个jar包,在pom.xml文件中配置如下,也可下载后直 ...

  4. Java使用poi从数据库读取数据生成Excel表格

    想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...

  5. POI操作Excel详解,读取xls和xlsx格式的文件

    package org.ian.webutil;   import java.io.File; import java.io.FileInputStream; import java.io.FileN ...

  6. POI操作Excel

    POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...

  7. 使用POI操作Excel时对事先写入模板的公式强制执行

    场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...

  8. 利用POI操作不同版本号word文档中的图片以及创建word文档

    我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...

  9. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

随机推荐

  1. SSM知识巩固

    ------------------------- 绑定页面提交的多个数据  绑定数组 --------------------------------------- 绑定list(需求:批量修改商品 ...

  2. static类型autowired 注入失败

    原代码:注入commonService对象失败 @Autowired private static CommonService commonService; public static List< ...

  3. 路由器wiff设置

    1.将一根网线连接至路由wankou 2.将另外一根网页连接1.2.3.4口中一个,另外一个连接至电脑 3.登录192.168.1.1,进行设置向导选择ppoe,然后登录网络设置无线名称+密码 4.保 ...

  4. hdu 2151

    就是一个dp,数组内存的步数, 数组没清空,wa了一次 #include<cstdio> #include<algorithm> #include<cstring> ...

  5. MySQL具体解释(8)----------MySQL线程池总结(二)

    这篇文章是对上篇文章的一个补充,主要环绕下面两点展开.one-connection-per-thread的实现方式以及线程池中epoll的使用. one-connection-per-thread 依 ...

  6. MVC/MVP/MVVM区别——MVVM就是angular,视图和数据双向绑定

    摘自:http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html 一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面 ...

  7. ROS-URDF文件标签解读

    前言:URDF文件标签解读.margin: auto; width: 700px; height: 100px; ; width: 700px; text-align: center; 一.连杆(li ...

  8. 移动端 | table 布局

    <table border=” cellspacing="> <caption>表格标题</caption> <tr> <td alig ...

  9. 数组常用API

    内容待添加... //根据分数排名字 //方法1 var students = ['小明','小红','小花'] var scores = {小明:,小红:,小花:} //1 添加分数到student ...

  10. 维基百科 MediaWiki API 解析

    使用开放的 API 做一个自己的小项目,是一个很好的学习方法.但好像开放的 API 选择并不多.这里给大家多一个选择,简单介绍一下维基百科使用的 MediaWiki API. 简介 先简单介绍几个容易 ...