tdfont.setStyle(Font.NORMAL);
// 设置水平对齐方式
table.setHorizontalAlignment(Element.ALIGN_MIDDLE);
// 设置table的header
table.addCell(new Paragraph("id", thfont));
table.addCell(new Paragraph("name", thfont));
table.addCell(new Paragraph("sex", thfont));
table.addCell(new Paragraph("age", thfont));
// 循环设置table的每一行
for (int i = 0; i < list.size(); i++) {
Person p = (Person) list.get(i);
table.addCell(new Paragraph(p.getId(), tdfont));
table.addCell(new Paragraph(p.getName(), tdfont));
table.addCell(new Paragraph(p.getSex(), tdfont));
table.addCell(new Paragraph(String.valueOf(p.getAge()),
tdfont));
}
document.add(table);
document.close();
// baos.writeTo(response.getOutputStream());
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if ("CSV".equals(downType)) {
// response.reset();
// 生成csv文件
//response.setHeader("Content-disposition", "attachment;filename="
// + fname + ".csv");
//response.setContentType("text/csv");
//response.setCharacterEncoding("UTF-8");
FileOutputStream out ;
String sep = ",";
try {
//out = response.getOutputStream();
out.write("id".getBytes());
out.write(sep.getBytes());
out.write("name".getBytes());
out.write(sep.getBytes());
out.write("sex".getBytes());
out.write(sep.getBytes());
out.write("age".getBytes());
out.write(sep.getBytes());
out.write(System.getProperty("line.separator").getBytes());
for (int i = 0; i < list.size(); i++) {
Person p = (Person) list.get(i);
out.write(p.getId().getBytes());
out.write((sep + "/t").getBytes());
out.write(p.getName().getBytes());
out.write((sep + "/t").getBytes());
out.write(p.getSex().getBytes());
out.write((sep + "/t").getBytes());
out.write(String.valueOf(p.getAge()).getBytes());
out.write((sep + "/t").getBytes());
out.write(sep.getBytes());
out.write(System.getProperty("line.separator").getBytes());
}
out.flush();
//out.cloute();
} catch (Exception e) {
e.printStackTrace();
}
} else if (downType.equals("Excel")) {
//response.reset();
// 生成xls文件
//response.setContentType("application/vnd.ms-excel");
//response.setHeader("Content-