データベースデータをエクスポートしてwordを生成【2】--itext直接wordに書き込む内容
サードパーティjarパッケージを参照:
iText-2.1.3.jar
iText-rtf-2.1.3.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",
"p_w_upload; 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",
"p_w_upload; 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();
}
注意:画像はストリームで書き込む必要があります.