4 个解决方案
#1
//生成答辩成绩,带学生信息 2008.11.25
if (type.equals("生成答辩成绩Excel"))
{
try
{
log.info("生成答辩成绩Excel");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Content-disposition","attachment; filename="+ URLEncoder.encode("答辩成绩.xls", "UTF-8"));
response.setContentType("application/msexcel");
}
catch (IOException e)
{
log.info(e);
}
try
{
// 打开文件
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
// 生成名为"第一页"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("第一页", 0);
// 设置字体为宋体,16号字,加粗
WritableFont font1 = new WritableFont(WritableFont
.createFont("宋体"), 16, WritableFont.BOLD);
WritableCellFormat format1 = new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE);
format1
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
Label labelA = new Label(0, 0, "学号", format1);
Label labelB = new Label(1, 0, "姓名", format1);
Label labelC = new Label(2, 0, "性别", format1);
Label labelD = new Label(3, 0, "院系", format1);
Label labelE = new Label(4, 0, "电话", format1);
Label labelF = new Label(5, 0, "邮箱", format1);
Label labelG = new Label(6, 0, "班级", format1);
Label labelH = new Label(7, 0, "论文题目", format1);
Label labelI = new Label(8, 0, "指导教师", format1);
Label labelJ = new Label(9, 0, "指导教师打分", format1);
Label labelK = new Label(10, 0, "评阅教师", format1);
Label labelL = new Label(11, 0, "评阅教师打分", format1);
Label labelM = new Label(12, 0, "答辩委员会打分一", format1);
Label labelN = new Label(13, 0, "答辩委员会打分二", format1);
Label labelO = new Label(14, 0, "答辩委员会打分三", format1);
Label labelP = new Label(15, 0, "答辩委员会打分四", format1);
Label labelQ = new Label(16, 0, "答辩委员会打分五", format1);
Label labelR = new Label(17, 0, "论文成绩", format1);
// 将定义好的单元格添加到工作表中
sheet.addCell(labelA);
sheet.addCell(labelB);
sheet.addCell(labelC);
sheet.addCell(labelD);
sheet.addCell(labelE);
sheet.addCell(labelF);
sheet.addCell(labelG);
sheet.addCell(labelH);
sheet.addCell(labelI);
sheet.addCell(labelJ);
sheet.addCell(labelK);
sheet.addCell(labelL);
sheet.addCell(labelM);
sheet.addCell(labelN);
sheet.addCell(labelO);
sheet.addCell(labelP);
sheet.addCell(labelQ);
sheet.addCell(labelR);
String sql = "select s.sno,s.sname,s.sex,s.yuanxi,s.tel,s.email,s.class," +
"d.lwname,d.tname,d.score,d.ytname,d.yscore,d.dscore1,d.dscore2,d.dscore3,d.dscore4,d.dscore5, d.endscore from student s,duelw d where s.sname = d.sname ";
ResultSet rs = stmt.executeQuery(sql);
String sid = null;
String sname = null;
String ssex = null;
String syuanxi = null;
String stel = null;
String semail = null;
String sclass = null;
String dlwname = null;
String dtname = null;
String dscore = null;
String dytname = null;
String dyscore = null;
String dscore1 = null;
String dscore2 = null;
String dscore3 = null;
String dscore4 = null;
String dscore5 = null;
String endscore = null;
int i = 1;
while (rs.next())
{
sid = rs.getString("sno");
sname = rs.getString("sname");
ssex = rs.getString("sex");
syuanxi = rs.getString("yuanxi");
stel = rs.getString("tel");
semail = rs.getString("email");
sclass = rs.getString("class");
dlwname = rs.getString("lwname");
dtname = rs.getString("tname");
dscore = rs.getString("score");
dytname = rs.getString("ytname");
dyscore = rs.getString("yscore");
dscore1 = rs.getString("dscore1");
dscore2 = rs.getString("dscore2");
dscore3 = rs.getString("dscore3");
dscore4 = rs.getString("dscore4");
dscore5 = rs.getString("dscore5");
endscore = rs.getString("endscore");
Label labelA1 = new Label(0, i, sid);
Label labelB1 = new Label(1, i, sname);
Label labelC1 = new Label(2, i, ssex);
Label labelD1 = new Label(3, i, syuanxi);
Label labelE1 = new Label(4, i, stel);
Label labelF1 = new Label(5, i, semail);
Label labelG1 = new Label(6, i, sclass);
Label labelH1 = new Label(7, i, dlwname);
Label labelI1 = new Label(8, i, dtname);
Label labelJ1 = new Label(9, i, dscore);
Label labelK1 = new Label(10, i, dytname);
Label labelL1 = new Label(11, i, dyscore);
Label labelM1 = new Label(12, i, dscore1);
Label labelN1 = new Label(13, i, dscore2);
Label labelO1 = new Label(14, i, dscore3);
Label labelP1 = new Label(15, i, dscore4);
Label labelQ1 = new Label(16, i, dscore5);
Label labelR1 = new Label(17, i, endscore);
sheet.addCell(labelA1);
sheet.addCell(labelB1);
sheet.addCell(labelC1);
sheet.addCell(labelD1);
sheet.addCell(labelE1);
sheet.addCell(labelF1);
sheet.addCell(labelG1);
sheet.addCell(labelH1);
sheet.addCell(labelI1);
sheet.addCell(labelJ1);
sheet.addCell(labelK1);
sheet.addCell(labelL1);
sheet.addCell(labelM1);
sheet.addCell(labelN1);
sheet.addCell(labelO1);
sheet.addCell(labelP1);
sheet.addCell(labelQ1);
sheet.addCell(labelR1);
i++;
}
// 写入数据并关闭文件
book.write();
book.close();
forward = mapping.findForward("ok");
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (type.equals("生成答辩成绩Excel"))
{
try
{
log.info("生成答辩成绩Excel");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Content-disposition","attachment; filename="+ URLEncoder.encode("答辩成绩.xls", "UTF-8"));
response.setContentType("application/msexcel");
}
catch (IOException e)
{
log.info(e);
}
try
{
// 打开文件
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
// 生成名为"第一页"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("第一页", 0);
// 设置字体为宋体,16号字,加粗
WritableFont font1 = new WritableFont(WritableFont
.createFont("宋体"), 16, WritableFont.BOLD);
WritableCellFormat format1 = new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE);
format1
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
Label labelA = new Label(0, 0, "学号", format1);
Label labelB = new Label(1, 0, "姓名", format1);
Label labelC = new Label(2, 0, "性别", format1);
Label labelD = new Label(3, 0, "院系", format1);
Label labelE = new Label(4, 0, "电话", format1);
Label labelF = new Label(5, 0, "邮箱", format1);
Label labelG = new Label(6, 0, "班级", format1);
Label labelH = new Label(7, 0, "论文题目", format1);
Label labelI = new Label(8, 0, "指导教师", format1);
Label labelJ = new Label(9, 0, "指导教师打分", format1);
Label labelK = new Label(10, 0, "评阅教师", format1);
Label labelL = new Label(11, 0, "评阅教师打分", format1);
Label labelM = new Label(12, 0, "答辩委员会打分一", format1);
Label labelN = new Label(13, 0, "答辩委员会打分二", format1);
Label labelO = new Label(14, 0, "答辩委员会打分三", format1);
Label labelP = new Label(15, 0, "答辩委员会打分四", format1);
Label labelQ = new Label(16, 0, "答辩委员会打分五", format1);
Label labelR = new Label(17, 0, "论文成绩", format1);
// 将定义好的单元格添加到工作表中
sheet.addCell(labelA);
sheet.addCell(labelB);
sheet.addCell(labelC);
sheet.addCell(labelD);
sheet.addCell(labelE);
sheet.addCell(labelF);
sheet.addCell(labelG);
sheet.addCell(labelH);
sheet.addCell(labelI);
sheet.addCell(labelJ);
sheet.addCell(labelK);
sheet.addCell(labelL);
sheet.addCell(labelM);
sheet.addCell(labelN);
sheet.addCell(labelO);
sheet.addCell(labelP);
sheet.addCell(labelQ);
sheet.addCell(labelR);
String sql = "select s.sno,s.sname,s.sex,s.yuanxi,s.tel,s.email,s.class," +
"d.lwname,d.tname,d.score,d.ytname,d.yscore,d.dscore1,d.dscore2,d.dscore3,d.dscore4,d.dscore5, d.endscore from student s,duelw d where s.sname = d.sname ";
ResultSet rs = stmt.executeQuery(sql);
String sid = null;
String sname = null;
String ssex = null;
String syuanxi = null;
String stel = null;
String semail = null;
String sclass = null;
String dlwname = null;
String dtname = null;
String dscore = null;
String dytname = null;
String dyscore = null;
String dscore1 = null;
String dscore2 = null;
String dscore3 = null;
String dscore4 = null;
String dscore5 = null;
String endscore = null;
int i = 1;
while (rs.next())
{
sid = rs.getString("sno");
sname = rs.getString("sname");
ssex = rs.getString("sex");
syuanxi = rs.getString("yuanxi");
stel = rs.getString("tel");
semail = rs.getString("email");
sclass = rs.getString("class");
dlwname = rs.getString("lwname");
dtname = rs.getString("tname");
dscore = rs.getString("score");
dytname = rs.getString("ytname");
dyscore = rs.getString("yscore");
dscore1 = rs.getString("dscore1");
dscore2 = rs.getString("dscore2");
dscore3 = rs.getString("dscore3");
dscore4 = rs.getString("dscore4");
dscore5 = rs.getString("dscore5");
endscore = rs.getString("endscore");
Label labelA1 = new Label(0, i, sid);
Label labelB1 = new Label(1, i, sname);
Label labelC1 = new Label(2, i, ssex);
Label labelD1 = new Label(3, i, syuanxi);
Label labelE1 = new Label(4, i, stel);
Label labelF1 = new Label(5, i, semail);
Label labelG1 = new Label(6, i, sclass);
Label labelH1 = new Label(7, i, dlwname);
Label labelI1 = new Label(8, i, dtname);
Label labelJ1 = new Label(9, i, dscore);
Label labelK1 = new Label(10, i, dytname);
Label labelL1 = new Label(11, i, dyscore);
Label labelM1 = new Label(12, i, dscore1);
Label labelN1 = new Label(13, i, dscore2);
Label labelO1 = new Label(14, i, dscore3);
Label labelP1 = new Label(15, i, dscore4);
Label labelQ1 = new Label(16, i, dscore5);
Label labelR1 = new Label(17, i, endscore);
sheet.addCell(labelA1);
sheet.addCell(labelB1);
sheet.addCell(labelC1);
sheet.addCell(labelD1);
sheet.addCell(labelE1);
sheet.addCell(labelF1);
sheet.addCell(labelG1);
sheet.addCell(labelH1);
sheet.addCell(labelI1);
sheet.addCell(labelJ1);
sheet.addCell(labelK1);
sheet.addCell(labelL1);
sheet.addCell(labelM1);
sheet.addCell(labelN1);
sheet.addCell(labelO1);
sheet.addCell(labelP1);
sheet.addCell(labelQ1);
sheet.addCell(labelR1);
i++;
}
// 写入数据并关闭文件
book.write();
book.close();
forward = mapping.findForward("ok");
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
#2
用jacob解决
#3
// 生成教师信息PDF文件
if (type.equals("生成教师信息pdf"))
{
try
{
response.setContentType("text/html; charset=GBK");
response.setHeader("Content-disposition",
"attachment; filename="
+ URLEncoder.encode("教师信息.pdf", "UTF-8"));
response.setContentType("application/pdf");
}
catch (IOException e)
{
log.info(e);
}
try
{
OutputStream os = response.getOutputStream();
// (1)实例化文档对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A3, 2, 2, 2, 2);
// (2)创建写入器
// 第一个参数是对文档对象的引用,第二个参数是输出的文件,将out和document连接起来
PdfWriter writer = PdfWriter.getInstance(document, os);
// 打开文档准备写入内容
document.open();
// (3)下面创建章节对象
// 首先创建段落对象,作为章节的标题。FontFactory用于指定段落的字体。
BaseFont bfChinese = BaseFont.createFont(
"C:/WINDOWS/Fonts/simkai.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
// 中文大小为25,加粗
Font font = new Font(bfChinese, 25, Font.BOLD);
Paragraph chapter1_title = new Paragraph("教 师 信 息", font);
// 创建了一个章节对象,标题为"Chapter 1"
chapter1_title.setAlignment(Paragraph.ALIGN_CENTER);
Chapter chapter1 = new Chapter(chapter1_title, 1);
// 将编号级别设为 0 就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
// Section section1 = chapter1.addSection("");
// section1.add(arg0)
String sql = "select * from teacher";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int j = rs.getRow();
// (6)往小节中写表格
// 创建表格对象(列,行)
Table table = new Table(11, j + 1);
// 设置表格边框颜色
table.setBorderColor(new Color(220, 255, 100));
// 设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
table.setWidth(90);
float[] widths = { 50f, 120f, 80f, 40f, 110f, 110f, 110f, 100f,
70f, 100f, 230f };// 设置表格的列宽
table.setWidths(widths);
// 单元格对象
// 添加表头信息
Cell cell1 = new Cell(new Paragraph("编号", new Font(bfChinese,
10, Font.BOLD)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell2 = new Cell(new Paragraph("密码", new Font(bfChinese,
10, Font.BOLD)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell3 = new Cell(new Paragraph("姓 名", new Font(bfChinese,
10, Font.BOLD)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell4 = new Cell(new Paragraph("性别", new Font(bfChinese,
10, Font.BOLD)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell5 = new Cell(new Paragraph("出生日期", new Font(bfChinese,
10, Font.BOLD)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell6 = new Cell(new Paragraph("学 院", new Font(bfChinese,
10, Font.BOLD)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell7 = new Cell(new Paragraph("研究方向", new Font(bfChinese,
10, Font.BOLD)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell8 = new Cell(new Paragraph("职 称", new Font(bfChinese,
10, Font.BOLD)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell9 = new Cell(new Paragraph("籍 贯", new Font(bfChinese,
10, Font.BOLD)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell10 = new Cell(new Paragraph("电 话", new Font(
bfChinese, 10, Font.BOLD)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell11 = new Cell(new Paragraph("邮 箱", new Font(
bfChinese, 10, Font.BOLD)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell1.setHeader(true);
cell2.setHeader(true);
cell3.setHeader(true);
cell4.setHeader(true);
cell5.setHeader(true);
cell6.setHeader(true);
cell7.setHeader(true);
cell8.setHeader(true);
cell9.setHeader(true);
cell10.setHeader(true);
cell11.setHeader(true);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
table.endHeaders();
rs.first();
String tid = null;
String pwd = null;
String tname = null;
String sex = null;
String brith = null;
String xueyuan = null;
String title = null;
String zhicheng = null;
String jiguan = null;
String tel = null;
String email = null;
for (int rowNum = 1; rowNum <= j; rowNum++)
{
while (rs.next())
{
tid = rs.getString("tid");
pwd = rs.getString("pwd");
tname = rs.getString("tname");
sex = rs.getString("sex");
brith = rs.getString("birth");
xueyuan = rs.getString("xueyuan");
title = rs.getString("title");
zhicheng = rs.getString("zhicheng");
jiguan = rs.getString("jiguan");
tel = rs.getString("tel");
email = rs.getString("email");
cell1 = new Cell(new Paragraph(tid, new Font(bfChinese,
10)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell2 = new Cell(new Paragraph(pwd, new Font(bfChinese,
10)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell3 = new Cell(new Paragraph(tname, new Font(
bfChinese, 10)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell4 = new Cell(new Paragraph(sex, new Font(bfChinese,
10)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell5 = new Cell(new Paragraph(brith, new Font(
bfChinese, 10)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell6 = new Cell(new Paragraph(xueyuan, new Font(
bfChinese, 10)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell7 = new Cell(new Paragraph(title, new Font(
bfChinese, 10)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell8 = new Cell(new Paragraph(zhicheng, new Font(
bfChinese, 10)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell9 = new Cell(new Paragraph(jiguan, new Font(
bfChinese, 10)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell10 = new Cell(new Paragraph(tel, new Font(
bfChinese, 10)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell11 = new Cell(new Paragraph(email, new Font(
bfChinese, 10)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
}
}
// 将表格对象添加到小节对象中
chapter1.add(table);
// (9)将章节对象加入到文档中
document.add(chapter1);
// (10)关闭文档
document.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (type.equals("生成教师信息pdf"))
{
try
{
response.setContentType("text/html; charset=GBK");
response.setHeader("Content-disposition",
"attachment; filename="
+ URLEncoder.encode("教师信息.pdf", "UTF-8"));
response.setContentType("application/pdf");
}
catch (IOException e)
{
log.info(e);
}
try
{
OutputStream os = response.getOutputStream();
// (1)实例化文档对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A3, 2, 2, 2, 2);
// (2)创建写入器
// 第一个参数是对文档对象的引用,第二个参数是输出的文件,将out和document连接起来
PdfWriter writer = PdfWriter.getInstance(document, os);
// 打开文档准备写入内容
document.open();
// (3)下面创建章节对象
// 首先创建段落对象,作为章节的标题。FontFactory用于指定段落的字体。
BaseFont bfChinese = BaseFont.createFont(
"C:/WINDOWS/Fonts/simkai.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
// 中文大小为25,加粗
Font font = new Font(bfChinese, 25, Font.BOLD);
Paragraph chapter1_title = new Paragraph("教 师 信 息", font);
// 创建了一个章节对象,标题为"Chapter 1"
chapter1_title.setAlignment(Paragraph.ALIGN_CENTER);
Chapter chapter1 = new Chapter(chapter1_title, 1);
// 将编号级别设为 0 就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
// Section section1 = chapter1.addSection("");
// section1.add(arg0)
String sql = "select * from teacher";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int j = rs.getRow();
// (6)往小节中写表格
// 创建表格对象(列,行)
Table table = new Table(11, j + 1);
// 设置表格边框颜色
table.setBorderColor(new Color(220, 255, 100));
// 设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
table.setWidth(90);
float[] widths = { 50f, 120f, 80f, 40f, 110f, 110f, 110f, 100f,
70f, 100f, 230f };// 设置表格的列宽
table.setWidths(widths);
// 单元格对象
// 添加表头信息
Cell cell1 = new Cell(new Paragraph("编号", new Font(bfChinese,
10, Font.BOLD)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell2 = new Cell(new Paragraph("密码", new Font(bfChinese,
10, Font.BOLD)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell3 = new Cell(new Paragraph("姓 名", new Font(bfChinese,
10, Font.BOLD)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell4 = new Cell(new Paragraph("性别", new Font(bfChinese,
10, Font.BOLD)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell5 = new Cell(new Paragraph("出生日期", new Font(bfChinese,
10, Font.BOLD)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell6 = new Cell(new Paragraph("学 院", new Font(bfChinese,
10, Font.BOLD)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell7 = new Cell(new Paragraph("研究方向", new Font(bfChinese,
10, Font.BOLD)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell8 = new Cell(new Paragraph("职 称", new Font(bfChinese,
10, Font.BOLD)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell9 = new Cell(new Paragraph("籍 贯", new Font(bfChinese,
10, Font.BOLD)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell10 = new Cell(new Paragraph("电 话", new Font(
bfChinese, 10, Font.BOLD)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell11 = new Cell(new Paragraph("邮 箱", new Font(
bfChinese, 10, Font.BOLD)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell1.setHeader(true);
cell2.setHeader(true);
cell3.setHeader(true);
cell4.setHeader(true);
cell5.setHeader(true);
cell6.setHeader(true);
cell7.setHeader(true);
cell8.setHeader(true);
cell9.setHeader(true);
cell10.setHeader(true);
cell11.setHeader(true);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
table.endHeaders();
rs.first();
String tid = null;
String pwd = null;
String tname = null;
String sex = null;
String brith = null;
String xueyuan = null;
String title = null;
String zhicheng = null;
String jiguan = null;
String tel = null;
String email = null;
for (int rowNum = 1; rowNum <= j; rowNum++)
{
while (rs.next())
{
tid = rs.getString("tid");
pwd = rs.getString("pwd");
tname = rs.getString("tname");
sex = rs.getString("sex");
brith = rs.getString("birth");
xueyuan = rs.getString("xueyuan");
title = rs.getString("title");
zhicheng = rs.getString("zhicheng");
jiguan = rs.getString("jiguan");
tel = rs.getString("tel");
email = rs.getString("email");
cell1 = new Cell(new Paragraph(tid, new Font(bfChinese,
10)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell2 = new Cell(new Paragraph(pwd, new Font(bfChinese,
10)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell3 = new Cell(new Paragraph(tname, new Font(
bfChinese, 10)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell4 = new Cell(new Paragraph(sex, new Font(bfChinese,
10)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell5 = new Cell(new Paragraph(brith, new Font(
bfChinese, 10)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell6 = new Cell(new Paragraph(xueyuan, new Font(
bfChinese, 10)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell7 = new Cell(new Paragraph(title, new Font(
bfChinese, 10)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell8 = new Cell(new Paragraph(zhicheng, new Font(
bfChinese, 10)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell9 = new Cell(new Paragraph(jiguan, new Font(
bfChinese, 10)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell10 = new Cell(new Paragraph(tel, new Font(
bfChinese, 10)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell11 = new Cell(new Paragraph(email, new Font(
bfChinese, 10)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
}
}
// 将表格对象添加到小节对象中
chapter1.add(table);
// (9)将章节对象加入到文档中
document.add(chapter1);
// (10)关闭文档
document.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
#4
这个只能导出一个表格形式的,我的报表有flash啊,这怎么导出pdf?
#1
//生成答辩成绩,带学生信息 2008.11.25
if (type.equals("生成答辩成绩Excel"))
{
try
{
log.info("生成答辩成绩Excel");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Content-disposition","attachment; filename="+ URLEncoder.encode("答辩成绩.xls", "UTF-8"));
response.setContentType("application/msexcel");
}
catch (IOException e)
{
log.info(e);
}
try
{
// 打开文件
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
// 生成名为"第一页"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("第一页", 0);
// 设置字体为宋体,16号字,加粗
WritableFont font1 = new WritableFont(WritableFont
.createFont("宋体"), 16, WritableFont.BOLD);
WritableCellFormat format1 = new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE);
format1
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
Label labelA = new Label(0, 0, "学号", format1);
Label labelB = new Label(1, 0, "姓名", format1);
Label labelC = new Label(2, 0, "性别", format1);
Label labelD = new Label(3, 0, "院系", format1);
Label labelE = new Label(4, 0, "电话", format1);
Label labelF = new Label(5, 0, "邮箱", format1);
Label labelG = new Label(6, 0, "班级", format1);
Label labelH = new Label(7, 0, "论文题目", format1);
Label labelI = new Label(8, 0, "指导教师", format1);
Label labelJ = new Label(9, 0, "指导教师打分", format1);
Label labelK = new Label(10, 0, "评阅教师", format1);
Label labelL = new Label(11, 0, "评阅教师打分", format1);
Label labelM = new Label(12, 0, "答辩委员会打分一", format1);
Label labelN = new Label(13, 0, "答辩委员会打分二", format1);
Label labelO = new Label(14, 0, "答辩委员会打分三", format1);
Label labelP = new Label(15, 0, "答辩委员会打分四", format1);
Label labelQ = new Label(16, 0, "答辩委员会打分五", format1);
Label labelR = new Label(17, 0, "论文成绩", format1);
// 将定义好的单元格添加到工作表中
sheet.addCell(labelA);
sheet.addCell(labelB);
sheet.addCell(labelC);
sheet.addCell(labelD);
sheet.addCell(labelE);
sheet.addCell(labelF);
sheet.addCell(labelG);
sheet.addCell(labelH);
sheet.addCell(labelI);
sheet.addCell(labelJ);
sheet.addCell(labelK);
sheet.addCell(labelL);
sheet.addCell(labelM);
sheet.addCell(labelN);
sheet.addCell(labelO);
sheet.addCell(labelP);
sheet.addCell(labelQ);
sheet.addCell(labelR);
String sql = "select s.sno,s.sname,s.sex,s.yuanxi,s.tel,s.email,s.class," +
"d.lwname,d.tname,d.score,d.ytname,d.yscore,d.dscore1,d.dscore2,d.dscore3,d.dscore4,d.dscore5, d.endscore from student s,duelw d where s.sname = d.sname ";
ResultSet rs = stmt.executeQuery(sql);
String sid = null;
String sname = null;
String ssex = null;
String syuanxi = null;
String stel = null;
String semail = null;
String sclass = null;
String dlwname = null;
String dtname = null;
String dscore = null;
String dytname = null;
String dyscore = null;
String dscore1 = null;
String dscore2 = null;
String dscore3 = null;
String dscore4 = null;
String dscore5 = null;
String endscore = null;
int i = 1;
while (rs.next())
{
sid = rs.getString("sno");
sname = rs.getString("sname");
ssex = rs.getString("sex");
syuanxi = rs.getString("yuanxi");
stel = rs.getString("tel");
semail = rs.getString("email");
sclass = rs.getString("class");
dlwname = rs.getString("lwname");
dtname = rs.getString("tname");
dscore = rs.getString("score");
dytname = rs.getString("ytname");
dyscore = rs.getString("yscore");
dscore1 = rs.getString("dscore1");
dscore2 = rs.getString("dscore2");
dscore3 = rs.getString("dscore3");
dscore4 = rs.getString("dscore4");
dscore5 = rs.getString("dscore5");
endscore = rs.getString("endscore");
Label labelA1 = new Label(0, i, sid);
Label labelB1 = new Label(1, i, sname);
Label labelC1 = new Label(2, i, ssex);
Label labelD1 = new Label(3, i, syuanxi);
Label labelE1 = new Label(4, i, stel);
Label labelF1 = new Label(5, i, semail);
Label labelG1 = new Label(6, i, sclass);
Label labelH1 = new Label(7, i, dlwname);
Label labelI1 = new Label(8, i, dtname);
Label labelJ1 = new Label(9, i, dscore);
Label labelK1 = new Label(10, i, dytname);
Label labelL1 = new Label(11, i, dyscore);
Label labelM1 = new Label(12, i, dscore1);
Label labelN1 = new Label(13, i, dscore2);
Label labelO1 = new Label(14, i, dscore3);
Label labelP1 = new Label(15, i, dscore4);
Label labelQ1 = new Label(16, i, dscore5);
Label labelR1 = new Label(17, i, endscore);
sheet.addCell(labelA1);
sheet.addCell(labelB1);
sheet.addCell(labelC1);
sheet.addCell(labelD1);
sheet.addCell(labelE1);
sheet.addCell(labelF1);
sheet.addCell(labelG1);
sheet.addCell(labelH1);
sheet.addCell(labelI1);
sheet.addCell(labelJ1);
sheet.addCell(labelK1);
sheet.addCell(labelL1);
sheet.addCell(labelM1);
sheet.addCell(labelN1);
sheet.addCell(labelO1);
sheet.addCell(labelP1);
sheet.addCell(labelQ1);
sheet.addCell(labelR1);
i++;
}
// 写入数据并关闭文件
book.write();
book.close();
forward = mapping.findForward("ok");
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (type.equals("生成答辩成绩Excel"))
{
try
{
log.info("生成答辩成绩Excel");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Content-disposition","attachment; filename="+ URLEncoder.encode("答辩成绩.xls", "UTF-8"));
response.setContentType("application/msexcel");
}
catch (IOException e)
{
log.info(e);
}
try
{
// 打开文件
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
// 生成名为"第一页"的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("第一页", 0);
// 设置字体为宋体,16号字,加粗
WritableFont font1 = new WritableFont(WritableFont
.createFont("宋体"), 16, WritableFont.BOLD);
WritableCellFormat format1 = new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE);
format1
.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
Label labelA = new Label(0, 0, "学号", format1);
Label labelB = new Label(1, 0, "姓名", format1);
Label labelC = new Label(2, 0, "性别", format1);
Label labelD = new Label(3, 0, "院系", format1);
Label labelE = new Label(4, 0, "电话", format1);
Label labelF = new Label(5, 0, "邮箱", format1);
Label labelG = new Label(6, 0, "班级", format1);
Label labelH = new Label(7, 0, "论文题目", format1);
Label labelI = new Label(8, 0, "指导教师", format1);
Label labelJ = new Label(9, 0, "指导教师打分", format1);
Label labelK = new Label(10, 0, "评阅教师", format1);
Label labelL = new Label(11, 0, "评阅教师打分", format1);
Label labelM = new Label(12, 0, "答辩委员会打分一", format1);
Label labelN = new Label(13, 0, "答辩委员会打分二", format1);
Label labelO = new Label(14, 0, "答辩委员会打分三", format1);
Label labelP = new Label(15, 0, "答辩委员会打分四", format1);
Label labelQ = new Label(16, 0, "答辩委员会打分五", format1);
Label labelR = new Label(17, 0, "论文成绩", format1);
// 将定义好的单元格添加到工作表中
sheet.addCell(labelA);
sheet.addCell(labelB);
sheet.addCell(labelC);
sheet.addCell(labelD);
sheet.addCell(labelE);
sheet.addCell(labelF);
sheet.addCell(labelG);
sheet.addCell(labelH);
sheet.addCell(labelI);
sheet.addCell(labelJ);
sheet.addCell(labelK);
sheet.addCell(labelL);
sheet.addCell(labelM);
sheet.addCell(labelN);
sheet.addCell(labelO);
sheet.addCell(labelP);
sheet.addCell(labelQ);
sheet.addCell(labelR);
String sql = "select s.sno,s.sname,s.sex,s.yuanxi,s.tel,s.email,s.class," +
"d.lwname,d.tname,d.score,d.ytname,d.yscore,d.dscore1,d.dscore2,d.dscore3,d.dscore4,d.dscore5, d.endscore from student s,duelw d where s.sname = d.sname ";
ResultSet rs = stmt.executeQuery(sql);
String sid = null;
String sname = null;
String ssex = null;
String syuanxi = null;
String stel = null;
String semail = null;
String sclass = null;
String dlwname = null;
String dtname = null;
String dscore = null;
String dytname = null;
String dyscore = null;
String dscore1 = null;
String dscore2 = null;
String dscore3 = null;
String dscore4 = null;
String dscore5 = null;
String endscore = null;
int i = 1;
while (rs.next())
{
sid = rs.getString("sno");
sname = rs.getString("sname");
ssex = rs.getString("sex");
syuanxi = rs.getString("yuanxi");
stel = rs.getString("tel");
semail = rs.getString("email");
sclass = rs.getString("class");
dlwname = rs.getString("lwname");
dtname = rs.getString("tname");
dscore = rs.getString("score");
dytname = rs.getString("ytname");
dyscore = rs.getString("yscore");
dscore1 = rs.getString("dscore1");
dscore2 = rs.getString("dscore2");
dscore3 = rs.getString("dscore3");
dscore4 = rs.getString("dscore4");
dscore5 = rs.getString("dscore5");
endscore = rs.getString("endscore");
Label labelA1 = new Label(0, i, sid);
Label labelB1 = new Label(1, i, sname);
Label labelC1 = new Label(2, i, ssex);
Label labelD1 = new Label(3, i, syuanxi);
Label labelE1 = new Label(4, i, stel);
Label labelF1 = new Label(5, i, semail);
Label labelG1 = new Label(6, i, sclass);
Label labelH1 = new Label(7, i, dlwname);
Label labelI1 = new Label(8, i, dtname);
Label labelJ1 = new Label(9, i, dscore);
Label labelK1 = new Label(10, i, dytname);
Label labelL1 = new Label(11, i, dyscore);
Label labelM1 = new Label(12, i, dscore1);
Label labelN1 = new Label(13, i, dscore2);
Label labelO1 = new Label(14, i, dscore3);
Label labelP1 = new Label(15, i, dscore4);
Label labelQ1 = new Label(16, i, dscore5);
Label labelR1 = new Label(17, i, endscore);
sheet.addCell(labelA1);
sheet.addCell(labelB1);
sheet.addCell(labelC1);
sheet.addCell(labelD1);
sheet.addCell(labelE1);
sheet.addCell(labelF1);
sheet.addCell(labelG1);
sheet.addCell(labelH1);
sheet.addCell(labelI1);
sheet.addCell(labelJ1);
sheet.addCell(labelK1);
sheet.addCell(labelL1);
sheet.addCell(labelM1);
sheet.addCell(labelN1);
sheet.addCell(labelO1);
sheet.addCell(labelP1);
sheet.addCell(labelQ1);
sheet.addCell(labelR1);
i++;
}
// 写入数据并关闭文件
book.write();
book.close();
forward = mapping.findForward("ok");
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
#2
用jacob解决
#3
// 生成教师信息PDF文件
if (type.equals("生成教师信息pdf"))
{
try
{
response.setContentType("text/html; charset=GBK");
response.setHeader("Content-disposition",
"attachment; filename="
+ URLEncoder.encode("教师信息.pdf", "UTF-8"));
response.setContentType("application/pdf");
}
catch (IOException e)
{
log.info(e);
}
try
{
OutputStream os = response.getOutputStream();
// (1)实例化文档对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A3, 2, 2, 2, 2);
// (2)创建写入器
// 第一个参数是对文档对象的引用,第二个参数是输出的文件,将out和document连接起来
PdfWriter writer = PdfWriter.getInstance(document, os);
// 打开文档准备写入内容
document.open();
// (3)下面创建章节对象
// 首先创建段落对象,作为章节的标题。FontFactory用于指定段落的字体。
BaseFont bfChinese = BaseFont.createFont(
"C:/WINDOWS/Fonts/simkai.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
// 中文大小为25,加粗
Font font = new Font(bfChinese, 25, Font.BOLD);
Paragraph chapter1_title = new Paragraph("教 师 信 息", font);
// 创建了一个章节对象,标题为"Chapter 1"
chapter1_title.setAlignment(Paragraph.ALIGN_CENTER);
Chapter chapter1 = new Chapter(chapter1_title, 1);
// 将编号级别设为 0 就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
// Section section1 = chapter1.addSection("");
// section1.add(arg0)
String sql = "select * from teacher";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int j = rs.getRow();
// (6)往小节中写表格
// 创建表格对象(列,行)
Table table = new Table(11, j + 1);
// 设置表格边框颜色
table.setBorderColor(new Color(220, 255, 100));
// 设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
table.setWidth(90);
float[] widths = { 50f, 120f, 80f, 40f, 110f, 110f, 110f, 100f,
70f, 100f, 230f };// 设置表格的列宽
table.setWidths(widths);
// 单元格对象
// 添加表头信息
Cell cell1 = new Cell(new Paragraph("编号", new Font(bfChinese,
10, Font.BOLD)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell2 = new Cell(new Paragraph("密码", new Font(bfChinese,
10, Font.BOLD)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell3 = new Cell(new Paragraph("姓 名", new Font(bfChinese,
10, Font.BOLD)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell4 = new Cell(new Paragraph("性别", new Font(bfChinese,
10, Font.BOLD)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell5 = new Cell(new Paragraph("出生日期", new Font(bfChinese,
10, Font.BOLD)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell6 = new Cell(new Paragraph("学 院", new Font(bfChinese,
10, Font.BOLD)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell7 = new Cell(new Paragraph("研究方向", new Font(bfChinese,
10, Font.BOLD)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell8 = new Cell(new Paragraph("职 称", new Font(bfChinese,
10, Font.BOLD)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell9 = new Cell(new Paragraph("籍 贯", new Font(bfChinese,
10, Font.BOLD)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell10 = new Cell(new Paragraph("电 话", new Font(
bfChinese, 10, Font.BOLD)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell11 = new Cell(new Paragraph("邮 箱", new Font(
bfChinese, 10, Font.BOLD)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell1.setHeader(true);
cell2.setHeader(true);
cell3.setHeader(true);
cell4.setHeader(true);
cell5.setHeader(true);
cell6.setHeader(true);
cell7.setHeader(true);
cell8.setHeader(true);
cell9.setHeader(true);
cell10.setHeader(true);
cell11.setHeader(true);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
table.endHeaders();
rs.first();
String tid = null;
String pwd = null;
String tname = null;
String sex = null;
String brith = null;
String xueyuan = null;
String title = null;
String zhicheng = null;
String jiguan = null;
String tel = null;
String email = null;
for (int rowNum = 1; rowNum <= j; rowNum++)
{
while (rs.next())
{
tid = rs.getString("tid");
pwd = rs.getString("pwd");
tname = rs.getString("tname");
sex = rs.getString("sex");
brith = rs.getString("birth");
xueyuan = rs.getString("xueyuan");
title = rs.getString("title");
zhicheng = rs.getString("zhicheng");
jiguan = rs.getString("jiguan");
tel = rs.getString("tel");
email = rs.getString("email");
cell1 = new Cell(new Paragraph(tid, new Font(bfChinese,
10)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell2 = new Cell(new Paragraph(pwd, new Font(bfChinese,
10)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell3 = new Cell(new Paragraph(tname, new Font(
bfChinese, 10)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell4 = new Cell(new Paragraph(sex, new Font(bfChinese,
10)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell5 = new Cell(new Paragraph(brith, new Font(
bfChinese, 10)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell6 = new Cell(new Paragraph(xueyuan, new Font(
bfChinese, 10)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell7 = new Cell(new Paragraph(title, new Font(
bfChinese, 10)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell8 = new Cell(new Paragraph(zhicheng, new Font(
bfChinese, 10)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell9 = new Cell(new Paragraph(jiguan, new Font(
bfChinese, 10)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell10 = new Cell(new Paragraph(tel, new Font(
bfChinese, 10)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell11 = new Cell(new Paragraph(email, new Font(
bfChinese, 10)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
}
}
// 将表格对象添加到小节对象中
chapter1.add(table);
// (9)将章节对象加入到文档中
document.add(chapter1);
// (10)关闭文档
document.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (type.equals("生成教师信息pdf"))
{
try
{
response.setContentType("text/html; charset=GBK");
response.setHeader("Content-disposition",
"attachment; filename="
+ URLEncoder.encode("教师信息.pdf", "UTF-8"));
response.setContentType("application/pdf");
}
catch (IOException e)
{
log.info(e);
}
try
{
OutputStream os = response.getOutputStream();
// (1)实例化文档对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A3, 2, 2, 2, 2);
// (2)创建写入器
// 第一个参数是对文档对象的引用,第二个参数是输出的文件,将out和document连接起来
PdfWriter writer = PdfWriter.getInstance(document, os);
// 打开文档准备写入内容
document.open();
// (3)下面创建章节对象
// 首先创建段落对象,作为章节的标题。FontFactory用于指定段落的字体。
BaseFont bfChinese = BaseFont.createFont(
"C:/WINDOWS/Fonts/simkai.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
// 中文大小为25,加粗
Font font = new Font(bfChinese, 25, Font.BOLD);
Paragraph chapter1_title = new Paragraph("教 师 信 息", font);
// 创建了一个章节对象,标题为"Chapter 1"
chapter1_title.setAlignment(Paragraph.ALIGN_CENTER);
Chapter chapter1 = new Chapter(chapter1_title, 1);
// 将编号级别设为 0 就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
// Section section1 = chapter1.addSection("");
// section1.add(arg0)
String sql = "select * from teacher";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int j = rs.getRow();
// (6)往小节中写表格
// 创建表格对象(列,行)
Table table = new Table(11, j + 1);
// 设置表格边框颜色
table.setBorderColor(new Color(220, 255, 100));
// 设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
table.setWidth(90);
float[] widths = { 50f, 120f, 80f, 40f, 110f, 110f, 110f, 100f,
70f, 100f, 230f };// 设置表格的列宽
table.setWidths(widths);
// 单元格对象
// 添加表头信息
Cell cell1 = new Cell(new Paragraph("编号", new Font(bfChinese,
10, Font.BOLD)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell2 = new Cell(new Paragraph("密码", new Font(bfChinese,
10, Font.BOLD)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell3 = new Cell(new Paragraph("姓 名", new Font(bfChinese,
10, Font.BOLD)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell4 = new Cell(new Paragraph("性别", new Font(bfChinese,
10, Font.BOLD)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell5 = new Cell(new Paragraph("出生日期", new Font(bfChinese,
10, Font.BOLD)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell6 = new Cell(new Paragraph("学 院", new Font(bfChinese,
10, Font.BOLD)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell7 = new Cell(new Paragraph("研究方向", new Font(bfChinese,
10, Font.BOLD)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell8 = new Cell(new Paragraph("职 称", new Font(bfChinese,
10, Font.BOLD)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell9 = new Cell(new Paragraph("籍 贯", new Font(bfChinese,
10, Font.BOLD)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell10 = new Cell(new Paragraph("电 话", new Font(
bfChinese, 10, Font.BOLD)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
Cell cell11 = new Cell(new Paragraph("邮 箱", new Font(
bfChinese, 10, Font.BOLD)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell1.setHeader(true);
cell2.setHeader(true);
cell3.setHeader(true);
cell4.setHeader(true);
cell5.setHeader(true);
cell6.setHeader(true);
cell7.setHeader(true);
cell8.setHeader(true);
cell9.setHeader(true);
cell10.setHeader(true);
cell11.setHeader(true);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
table.endHeaders();
rs.first();
String tid = null;
String pwd = null;
String tname = null;
String sex = null;
String brith = null;
String xueyuan = null;
String title = null;
String zhicheng = null;
String jiguan = null;
String tel = null;
String email = null;
for (int rowNum = 1; rowNum <= j; rowNum++)
{
while (rs.next())
{
tid = rs.getString("tid");
pwd = rs.getString("pwd");
tname = rs.getString("tname");
sex = rs.getString("sex");
brith = rs.getString("birth");
xueyuan = rs.getString("xueyuan");
title = rs.getString("title");
zhicheng = rs.getString("zhicheng");
jiguan = rs.getString("jiguan");
tel = rs.getString("tel");
email = rs.getString("email");
cell1 = new Cell(new Paragraph(tid, new Font(bfChinese,
10)));
cell1.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell2 = new Cell(new Paragraph(pwd, new Font(bfChinese,
10)));
cell2.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell3 = new Cell(new Paragraph(tname, new Font(
bfChinese, 10)));
cell3.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell4 = new Cell(new Paragraph(sex, new Font(bfChinese,
10)));
cell4.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell5 = new Cell(new Paragraph(brith, new Font(
bfChinese, 10)));
cell5.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell6 = new Cell(new Paragraph(xueyuan, new Font(
bfChinese, 10)));
cell6.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell7 = new Cell(new Paragraph(title, new Font(
bfChinese, 10)));
cell7.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell8 = new Cell(new Paragraph(zhicheng, new Font(
bfChinese, 10)));
cell8.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell9 = new Cell(new Paragraph(jiguan, new Font(
bfChinese, 10)));
cell9.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell10 = new Cell(new Paragraph(tel, new Font(
bfChinese, 10)));
cell10.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
cell11 = new Cell(new Paragraph(email, new Font(
bfChinese, 10)));
cell11.setHorizontalAlignment(Paragraph.ALIGN_CENTER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell10);
table.addCell(cell11);
}
}
// 将表格对象添加到小节对象中
chapter1.add(table);
// (9)将章节对象加入到文档中
document.add(chapter1);
// (10)关闭文档
document.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
#4
这个只能导出一个表格形式的,我的报表有flash啊,这怎么导出pdf?