导出数据库数据生成word【2】--itext直接写word中写入内容

引用第三方jar包:

   iText-2.1.3.jar

   iText-rtf-2.1.3.jar

// 从数据库里面查询出来了数据
try{
if (dataList != null && dataList.size() > 0) {
DataRow dr = dataList.get(0);
String name = dr.getString("name");// 应聘人姓名
String academic = dr.getString("academic");// 学历
String school = dr.getString("school");// 毕业院校
String profession = dr.getString("profession");// 所学专业
String work_address = dr.getString("work_address");// 工作地点
String recruit_name = dr.getString("recruit_name");// 职位名称
String birthdate = dr.getString("birthdate");// 生日
String telephone = dr.getString("telephone");// 手机号码
String email = dr.getString("email");// 电子邮箱
String expected_salary = dr.getString("expected_salary");// 期望薪资
String personal_img = dr.getString("personal_img");// 个人照片
String skillStr = dr.getString("skill");
String rootPath = Application.getRootPath();
String newRootPath = rootPath.replace("\\", "/");
newRootPath = newRootPath
.substring(0, newRootPath.length() - 1);
logger.info("name:" + name + "academic:" + academic + "school:"
+ school + "profession:" + profession);
logger.info("work_address:" + work_address + "recruit_name:"
+ recruit_name + "birthdate:" + birthdate);
logger.info("telephone:" + telephone + "email:" + email
+ "expected_salary:" + expected_salary);
logger.info("skillStr:" + skillStr + "experienceStr:"
+ experienceStr + "educationStr:" + educationStr
+ "describeStr:" + describeStr);
// 输出文件
String filename = getUuid();
try {
/** 创建Document对象(word文档) author:yyli Sep 15, 2010 */
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
// 创建word文档,并设置纸张的大小
Document doc = new Document(PageSize.A4);
String fileName = newRootPath + "/exportDocs/" + name
+ filename + ".doc";
                   //fileName = new String(fileName.getBytes("utf-8"),"iso-8859-1");
                    logger.info("fileName:  " + fileName);
/**
 * 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 
 */
RtfWriter2.getInstance(doc, new FileOutputStream(fileName));
doc.open();
/** 标题字体 author:yyli Sep 15, 2010 */
RtfFont titleFont = new RtfFont("仿宋_GB2312", 15, Font.BOLD,
Color.BLACK);
/** 正文字体 author:yyli Sep 15, 2010 */
RtfFont contextFont = new RtfFont("仿宋_GB2312", 9,
Font.NORMAL, Color.BLACK);
/** 表格设置 author:yyli Sep 15, 2010 */
Table table = new Table(5, 12);
int[] withs = { 20, 35, 20, 35, 35 };
/** 设置每列所占比例 author:yyli Sep 15, 2010 */
table.setWidths(withs);
/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
table.setWidth(100);
/** 居中显示 author:yyli Sep 15, 2010 */
table.setAlignment(Element.ALIGN_CENTER);
/** 自动填满 author:yyli Sep 15, 2010 */
table.setAutoFillEmptyCells(true);
table.setBorderWidth(5); // 边框宽度
table.setBorderColor(new Color(0, 125, 255)); // 边框颜色
table.setPadding(12);// 衬距,看效果就知道什么意思了
table.setSpacing(0);// 即单元格之间的间距
table.setBorder(5);// 边框
/** 第一行(标题) */
String titleString = "建信期货应聘人员简历";
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
doc.add(title);
/** 第二行(正文) */
@SuppressWarnings("deprecation")
String contextString = "应聘岗位:" + recruit_name
+ "       工作地点:" + work_address;
Paragraph context = new Paragraph(contextString);
// 正文格式对齐方式
context.setAlignment(Element.ALIGN_RIGHT);
context.setFont(contextFont);
// 与上一段落(标题)的行距
context.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context);
Cell cell = null;
cell = new Cell("姓名");
cell.setHeader(true);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(name);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell("学历");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(academic);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
// 在列中添加图片
cell = new Cell("");
Image png = null;
try {
png = Image.getInstance(newRootPath+ personal_img);
cell = new Cell(png);
} catch (Exception e) {
e.printStackTrace();
logger.info("-------------图片异常--------------");
logger.info(newRootPath);
png= Image.getInstance(newRootPath+ "/upload/txsc.png");
png.scalePercent(80);
cell = new Cell(png);
}finally{
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(4);
table.addCell(cell);
cell = new Cell("毕业院校");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(school);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell("联系电话");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(telephone);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell("所学专业");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(profession);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell("电子邮箱");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(email);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell("出生日期");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(birthdate);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell("期望薪资");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new Cell(expected_salary);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new Cell(cleanHTML(educationStr));
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setColspan(5);
table.addCell(cell);
cell = new Cell("专业技能");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setColspan(5);
table.addCell(cell);
cell = new Cell(cleanHTML(skillStr));
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setColspan(5);
table.addCell(cell);
doc.add(table);
doc.close();
}
} catch (Exception e) {
e.printStackTrace();
logger.info("异常:  " + e.getMessage());
}finally{
/**
 * 下载
 */
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader(
"Content-disposition",
"attachment; filename="+ new String(new StringBuilder(String
.valueOf(name)).append(filename)
.append(".doc").toString()
.getBytes("gbk"), "iso-8859-1"));
/*response.setContentType("application/octet-stream; charset=gb2312");
response.setHeader(
"Content-disposition",
"attachment; filename="+ name+filename+".doc");*/
// 文件流输出
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(newRootPath+ "/exportDocs/"+ name+filename+".doc");
byte[] c = new byte[1024];
int i = 0;
while ((i = in.read(c)) > 0) {
logger.info("in.read:" + i);
out.write(c, 0, i);
}
out.flush();
out.close();
in.close();
}
}
} catch (Exception e) {
logger.info(e.getMessage());
e.printStackTrace();
}


注意:图片必须以流的方式写入

本文出自 “奋斗中的菜鸟……” 博客,谢绝转载!

你可能感兴趣的:(itext,生成Word)