import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class ExportWordTest {
public void createDocContext()throws DocumentException, IOException{
//设置纸张大小
Document document = new Document(PageSize.A4.rotate());
//建立一个书写器,与document对象关联
RtfWriter2.getInstance(document, new FileOutputStream("test.doc"));
document.open();
//设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//标题字体风格
Font titleFont = new Font(bfChinese,12,Font.BOLD);
//正文字体风格
Font contextFont = new Font(bfChinese,8,Font.NORMAL);
// 添加页眉
HeaderFooter header = new HeaderFooter(new Phrase("header"), false);
header.setAlignment(Rectangle.ALIGN_CENTER);
document.setHeader(header);
//设置Table表格,创建一个25列的表格
Table table = new Table(33);
table.setWidth(100);//占页面宽度比例
table.setAlignment(Element.ALIGN_CENTER);//居中
table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.setAutoFillEmptyCells(true);//自动填满
table.setBorderWidth(1);//边框宽度
table.setCellsFitPage(true);
//设置表数据
//标题
Paragraph date = new Paragraph("登记表",contextFont);
//设置对齐方式
date.setAlignment(Element.ALIGN_CENTER);
Cell cell = new Cell(date);
cell.setColspan(33);
cell.setBorderWidth(0);
table.addCell(cell);
//日期
Date d = new Date();
date = new Paragraph(d.getDate()+":"+d.getDate(),contextFont);
//设置对齐方式
date.setAlignment(Element.ALIGN_LEFT);
cell = new Cell(date);
cell.setColspan(33);
cell.setBorderWidth(0);
table.addCell(cell);
for(int i=0;i<1;i++){
date = new Paragraph("姓名姓名",contextFont);
//设置对齐方式
date.setAlignment(Element.ALIGN_CENTER);
cell = new Cell(date);
cell.setColspan(2);
table.addCell(cell);
for(int j=0;j<31;j++){
date = new Paragraph(""+(j+1),contextFont);
//设置对齐方式
date.setAlignment(Element.ALIGN_CENTER);
cell = new Cell(date);
table.addCell(cell);
}
}
table.endHeaders();
//设置表数据
for(int i=0;i<100;i++){
date = new Paragraph("wy"+i,contextFont);
//设置对齐方式
date.setAlignment(Element.ALIGN_CENTER);
cell = new Cell(date);
cell.setColspan(2);
table.addCell(cell);
for(int j=0;j<31;j++){
date = new Paragraph("data"+(j+1),contextFont);
//设置对齐方式
date.setAlignment(Element.ALIGN_CENTER);
cell = new Cell(date);
table.addCell(cell);
}
}
document.add(table);
// 添加页脚
HeaderFooter footer = new HeaderFooter(new Phrase("页脚",contextFont), true);
footer.setAlignment(Rectangle.ALIGN_LEFT);
document.setFooter(footer);
document.close();
}
public static void main(String[] args) {
ExportWordTest word = new ExportWordTest();
try {
word.createDocContext();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
itext导出word
最新推荐文章于 2021-02-24 02:40:52 发布