在action里执行下面这个方法的效果,就是在页面上弹出一个下载窗口, //导出用户信息
private void exportPriceFundTaxPayers(Collection col, HttpServletResponse response) throws Exception {
String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String fileName = "%E4%BA%BA" + currentDate + ".xls";
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("application/vnd.ms-excel;charset=GBK");
WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = book.createSheet("sheet1", 0);
WritableCellFormat wcf = new WritableCellFormat();
wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
int counter = 0;
int idx_sheet = 1;
if (col != null && col.size() > 0) {
for (Iterator it = col.iterator(); it.hasNext(); counter++) {
if (counter == 10000) {
sheet = book.createSheet("sheet" + (idx_sheet + 1), idx_sheet);
idx_sheet++;
counter = 0;
}
User user = (User) it.next();
sheet.addCell(new Label(0, counter, user.getName(), wcf));
sheet.addCell(new Label(1, counter, user.getAge(), wcf));
sheet.addCell(new Label(2, counter, user.getSex(), wcf));
}
book.write();
book.close();
} else {
sheet.addCell(new Label(2, 3, "没有找到用户信息!", wcf));
book.write();
book.close();
}
}网上有不少类似的例子,具体的你可以搜索一下。上面代码可以参考jxl.jar的API
private void exportPriceFundTaxPayers(Collection col, HttpServletResponse response) throws Exception {
String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String fileName = "%E4%BA%BA" + currentDate + ".xls";
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("application/vnd.ms-excel;charset=GBK");
WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
WritableSheet sheet = book.createSheet("sheet1", 0);
WritableCellFormat wcf = new WritableCellFormat();
wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
int counter = 0;
int idx_sheet = 1;
if (col != null && col.size() > 0) {
for (Iterator it = col.iterator(); it.hasNext(); counter++) {
if (counter == 10000) {
sheet = book.createSheet("sheet" + (idx_sheet + 1), idx_sheet);
idx_sheet++;
counter = 0;
}
User user = (User) it.next();
sheet.addCell(new Label(0, counter, user.getName(), wcf));
sheet.addCell(new Label(1, counter, user.getAge(), wcf));
sheet.addCell(new Label(2, counter, user.getSex(), wcf));
}
book.write();
book.close();
} else {
sheet.addCell(new Label(2, 3, "没有找到用户信息!", wcf));
book.write();
book.close();
}
}网上有不少类似的例子,具体的你可以搜索一下。上面代码可以参考jxl.jar的API
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货