使用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();
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();
相关文章推荐
- JAVA 使用iText导出PDF(含有Table)示例
- 使用iText导出PDF(含有Table)示例
- 开源Word读写组件DocX,通过word模板,导出用户简历使用示例
- 使用IText实现office Word文档导出
- Java使用IText(VM模版)导出PDF,IText导出word(二)
- 使用iText导出word和pdf(经典)
- [原创]开源Word读写组件DocX,通过word模板,导出用户简历使用示例
- 使用iText导出word 可自由设置为横页和纵页
- 使用itext导出word文档
- Aspose.Words使用word模板中的书签插入图片(导出含有图片的word)
- 使用NPOI将数据导出为word格式里的table
- 使用itext导出含表格的word文档
- 使用iText在word文档中插入复杂的Table表格
- 使用Itext结合Jfreechart图表导出带图表的word文档
- 开源Word读写组件DocX,通过word模板,导出用户简历使用示例
- Java导出Word文档使用iText操作
- iText的使用例子说明(利用Itext画模板导出word、pdf,纯java实现)
- Itext导出Word文档的例子
- 用itext导出word,pdf
- iText导出pdf、word、图片