页面列表导出cvs,excel、pdf报表.(三)

2014-11-24 10:26:43 · 作者: · 浏览: 2
disposition", "attachment;filename="
// + fname + ".xls");
try {
//os = response.getOutputStream();
Label l = null;
WritableWorkbook wbook = Workbook.createWorkbook(new File(
"d://a.xls"));
// 写sheet名称
WritableSheet sheet = wbook.createSheet("my excel file", 0);
jxl.write.WritableFont wfc4 = new jxl.write.WritableFont(
WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFC4 = new jxl.write.WritableCellFormat(
wfc4);
wcfFC4.setBackground(jxl.format.Colour.LIGHT_GREEN);
int col = 0;
sheet.setColumnView(col, 12);
l = new Label(col, 0, "id", wcfFC4);
sheet.addCell(l);
col++;
sheet.setColumnView(col, 12);
l = new Label(col, 0, "name", wcfFC4);
sheet.addCell(l);
col++;
sheet.setColumnView(col, 12);
l = new Label(col, 0, "sex", wcfFC4);
sheet.addCell(l);
col++;
sheet.setColumnView(col, 12);
l = new Label(col, 0, "age", wcfFC4);
sheet.addCell(l);

// 设置字体样式
jxl.write.WritableFont wfc5 = new jxl.write.WritableFont(
WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFC5 = new jxl.write.WritableCellFormat(
wfc5);
for (int i = 0; i < list.size(); i++) {
Person p = (Person) list.get(i);
int j = 0;
l = new Label(j, i + 1, p.getId(), wcfFC5);
sheet.addCell(l);
j++;
l = new Label(j, i + 1, p.getName(), wcfFC5);
sheet.addCell(l);
j++;
l = new Label(j, i + 1, p.getSex(), wcfFC5);
sheet.addCell(l);
j++;
l = new Label(j, i + 1, String.valueOf(p.getAge()), wcfFC5);
sheet.addCell(l);
j++;
}
// 写入流中
wbook.write();
wbook.close();

} catch (Exception e) {
e.printStackTrace();
} www.2cto.com
}
return SUCCESS;
}
}


操作很简单。选择好要导出的报表格式。点击导出按钮。我们在d盘相应的位置就会生成相应的报表。