语法:
依赖jar包
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rups</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.5</version>
</dependency>
1.设置word纸张的大小
Rectangle r = new Rectangle(纸张宽度,纸张高度);
2.设置创建文件参数
Document document = new Document(r(ps:纸张大小),右边距
,左边距
,上边距
,下边距);
3.在本地生成word文件
RtfWriter2.getInstance(document(ps:创建文件属性), new FileOutputStream(存放文件路径));
4.打开word文件填充数据
document.open();
字体
//当然方法的重载很多,可以自行选择
Font font = FontFactory.getFont("字体名称","字体编码",字体大小);
//设置字体颜色,颜色在下面
font.setColor(颜色);
//例子:宋体字
Font font2=FontFactory.getFont("STSong-Light","Cp1252",2);
表格操作
//创建表格
Table table=new Table(列数[,行数]);
//设置表格的宽度
table.setWidths(表格宽度);
//设置每列占宽比例
table.setWidth([第一列比例,第二列比例........])
//添加单元格;注意:行和列都是从0开始的
table.addCell(单元格,第几行,第几列);
//设置表格中单元格之间的边距
table.setSpacing(数字);//数字越大边距越大
单元格,填充表格数据
//创建单元格
Cell cell = new Cell(填充单元格的内容);
内容(ps:下面只是我常用的几种,应该够用):
字符串
图片
段落
//水平
cell.setHorizontalAlignment(对齐方式);
//垂直
cell.setVerticalAlignment(对齐方式)
对齐方式(ps:还有很多,此列出常用的几种):
Element.ALIGN_CENTER:居中
Element.ALIGN_LEFT :左对齐
Element.ALIGN_RIGHT :右对齐
Element.ALIGN_BOTTOM:向下对齐
Element.ALIGN_TOP :向上对齐
//设置单元格占用多少行,单元格默认一行
cell.setRowspan(行数);
//设置单元格占用多少列,单月个默认一列
cell.setColspan(列数);
//设置单元格边框颜色
cell.setBorderColor(颜色);
颜色:
Color.WHITE:白色
COlor.RED :红色
//自己想要的颜色,填上三原色就行
Color c=new Color(r, g, b);
ps:还用很多颜色,Color中很多,根据自己的选择
段落
//创建段落,段落不会不超过当前行的长度不会换行
Paragraph paragraph = new Paragraph(内容);ps:内容参考单元格中的段落
//追加元素
Paragraph.add(内容);
图片
Image image = Image.getInstance("图片路径");
//设置图片大小
image.scalePercent(图片宽度,图片高度);
例子:
下面是我根据上面语法写的例子供大家参考
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
public class MakeWordTest {
public static void main(String[] args) {
CreateWord c=new CreateWord();
c.createWord();
System.out.println("创建word成功");
}
}
class CreateWord{
public void createWord() {
//1.创建矩形
Rectangle r=new Rectangle(500,500);
//2.设置文件参数
Document document=new Document(r,10,10,10,10);
//3.把文件写的路径
try {
RtfWriter2.getInstance(document,new FileOutputStream("D:\\static\\sharedoc\\"+System.currentTimeMillis()+".doc"));
//打开word进行填充数据
document.open();
//创建表格
Table table=new Table(3);
table.setWidth(80);
/************************************************填充logo图片开始**************************************************/
//创建图片
Image image = Image.getInstance("D:\\static\\sharedoc\\78ff504e6b35cca1b416fcbcd3dc03f2.jpg");
//设置图片大小
image.scalePercent(13,13);
//常见单元格填充图片
Cell cellLogo=new Cell(image);
//把单元格填充到表格中
table.addCell(cellLogo,0,0);
/************************************************填充logo图片结束**************************************************/
/************************************************填充内容**************************************************/
//常见单元格填充图片
Cell cell1=new Cell("垃圾分类");
//把单元格填充到表格中
table.addCell(cell1,1,1);
//常见单元格填充图片
Font font2=FontFactory.getFont("STSong-Light","Cp1252",5);
Cell cell2=new Cell(new Paragraph("可回收垃圾、湿垃圾、干垃圾、有害垃圾",font2));
//把单元格填充到表格中
table.addCell(cell2,1,2);
Cell cell3=new Cell("跨行");
cell3.setRowspan(2);
//把单元格填充到表格中
table.addCell(cell3,2,1);
Cell cell4=new Cell("跨列");
cell4.setColspan(2);
//把单元格填充到表格中
table.addCell(cell4,4,1);
Cell cell5=new Cell("跨行跨列");
cell5.setColspan(2);
cell5.setRowspan(2);
//把单元格填充到表格中
table.addCell(cell5,6,1);
Cell cell6=new Cell("设置边框颜色为白色");
cell6.setBorderColor(Color.WHITE);
table.addCell(cell6,8,0);
//空格
Cell cell7=new Cell("空格\u00a0 \u00a0 \u00a0空格");
cell7.setBorderColor(Color.RED);
cell7.setColspan(3);
table.addCell(cell7,9,0);
//设置单元格的边距
table.setSpacing(5);
//
Image image2 = Image.getInstance("D:\\static\\sharedoc\\78ff504e6b35cca1b416fcbcd3dc03f2.jpg");
//设置图片大小
image2.scalePercent(6,6);
Paragraph paragraph = new Paragraph("图片和字同一个单元格不换行",font2);
paragraph.add(image2);
Cell cell8=new Cell(paragraph);
table.addCell(cell8,10,0);
Cell cell9=new Cell("当前数据");
cell9.addElement(new Paragraph("追加数据",font2));
font2.setColor(Color.red);
table.addCell(cell9,11,0);
Cell cell10=new Cell("图片在单元格上,左有间隙");
cell10.setColspan(2);
table.addCell(cell10,12,0);
Font font3=FontFactory.getFont("STSong-Light","Cp1252",3);
Image image3 = Image.getInstance("D:\\static\\sharedoc\\78ff504e6b35cca1b416fcbcd3dc03f2.jpg");
image3.scalePercent(6,6);
Cell cell11=new Cell(new Paragraph("\u00a0",font3));
Paragraph paragraph2 = new Paragraph("\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0",font2);
paragraph2.add(image3);
cell11.setColspan(2);
cell11.addElement(paragraph2);
table.addCell(cell11,13,0);
/************************************************填充logo图片结束**************************************************/
//创建单元格
//把表格填充在word中
document.add(table);
} catch (FileNotFoundException e) {
System.out.println("创建word路径错误");
e.printStackTrace();
} catch (BadElementException e) {
System.out.println("创建表格错误");
e.printStackTrace();
} catch (DocumentException e) {
System.out.println("word填充表格失败");
e.printStackTrace();
} catch (MalformedURLException e) {
System.out.println("图片不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("图片不存在");
e.printStackTrace();
}finally {
//关闭
if(document!=null) {
document.close();
}
}
}
}

网友评论