您的位置:首页 > 其它

使用iText导出Word(含有Table)示例

2013-02-22 16:59 309 查看
使用iText导出Word,需要的Jar包有iText-2.1.7.jar,iTextAsian.jar,iText-rtf-2.1.7.jar

Expert expert=new Expert();

.......expert的一些属性省略

String fileName=new String(("专家信息-"+expert.getName()).getBytes("GBK"),"ISO-8859-1");//文件名称

response.setContentType("application/doc;charset=utf-8");

response.setHeader("Content-disposition", "attachment;filename=" + fileName+".doc");//文件标题,打开类型

com.lowagie.text.Rectangle rect=new com.lowagie.text.Rectangle(com.lowagie.text.PageSize.A4);//设置页面

rect.setBackgroundColor(Color.WHITE);//设置背景色

com.lowagie.text.Document document=new com.lowagie.text.Document(rect);//创建文件

document.setMargins(10, 20, 30, 40);//页边距

RtfWriter2 writer2=RtfWriter2.getInstance(document, response.getOutputStream());//创建书写器与document关联

// 文档属性

document.addTitle("Title@sample"); // 标题

document.addAuthor("Author@nicaisheng");// 作者

document.addSubject("Subject@iText sample");// 主题

document.addKeywords("Keywords@iText");// 关键字

document.addCreator("Creator@iText");// 创建者

//设置字体

com.lowagie.text.pdf.BaseFont bfChinese=com.lowagie.text.pdf.BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

com.lowagie.text.Font fontChinese=new com.lowagie.text.Font(bfChinese, 8, Font.NORMAL);// 创建字体,设置family,size,style,还可以设置color

com.lowagie.text.Font titleChinese=new com.lowagie.text.Font(bfChinese, 20, Font.BOLD);

com.lowagie.text.Font BoldChinese = new com.lowagie.text.Font(bfChinese, 14, Font.BOLD);

com.lowagie.text.Font subBoldFontChinese = new com.lowagie.text.Font(bfChinese, 8, Font.BOLD);

//添加标题

document.open();

com.lowagie.text.Paragraph title=new com.lowagie.text.Paragraph("专家信息",titleChinese);

title.setAlignment(Element.ALIGN_CENTER);

//title.setLeading(1f);

document.add(title);

//生成表格

float[] widths = {25f, 30f, 30f,25f,25f,25f };// 设置表格的列宽和列数 默认是4列

Table table=new Table(6);

table.setWidths(widths);

//table.setSpacing(20f);

//table.setWidth(100);// 设置表格宽度为100%

Cell cell=null;

cell=new Cell(new com.lowagie.text.Paragraph("姓名", subBoldFontChinese));

cell.setBackgroundColor(Color.LIGHT_GRAY);

cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);

table.addCell(cell);

cell=new Cell(new com.lowagie.text.Paragraph(expert.getName(), fontChinese));

table.addCell(cell);

cell=new Cell(new com.lowagie.text.Paragraph("性别", subBoldFontChinese));

cell.setBackgroundColor(Color.LIGHT_GRAY);

cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);

table.addCell(cell);

cell=new Cell(new com.lowagie.text.Paragraph(expert.getGender(), fontChinese));

table.addCell(cell);

cell=new Cell(new com.lowagie.text.Paragraph("身份证号", subBoldFontChinese));

cell.setBackgroundColor(Color.LIGHT_GRAY);

cell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);

table.addCell(cell);

cell=new Cell(new com.lowagie.text.Paragraph(expert.getCitizenNumber(), fontChinese));

table.addCell(cell);

document.add(table);

document.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: