问题描述
- JSP导出Excel表是点击取消时报异常
-
public boolean export2(OutputStream os, String[] titles, List list, String[] keys, String reportDate) {
WritableWorkbook wwb = null;
//wwh20130327_add导出大量数据优化
WorkbookSettings wbs = new WorkbookSettings();
wbs.setUseTemporaryFileDuringWrite(true);
//wwh20130327_add_end
try {
wwb = Workbook.createWorkbook(os,wbs);
// 创建EXCEL工作表
WritableSheet ws = wwb.createSheet("车辆日报表", 0);
for (int i = 0; i < titles.length; i++) {
jxl.write.Label title = new jxl.write.Label(i, 0, titles[i]);
ws.addCell(title);
}
// 先判断是否传入原始数据
if (list == null || list.size() == 0 || os == null) {
// System.out.println("no data");
return false;
}
// System.out.println("have some data");
WritableCellFormat cellFormatDate = new WritableCellFormat(new DateFormat("yyyy-MM-dd hh:mm:ss"));
WritableCellFormat cellFormatNumber = new WritableCellFormat(NumberFormats.DEFAULT);
WritableCellFormat cellFormatText = new WritableCellFormat(NumberFormats.TEXT);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == null || list.get(i) instanceof Map == false) {
continue;
}
Map<?, ?> map = (Map<?, ?>) list.get(i);
int colIndex = 0;
Object value = null;
int colWidth[] = new int[keys.length];
for (String key : keys) {
value = map.get(key);if (value != null) { if (value instanceof Timestamp) { jxl.write.DateTime dateCell = new jxl.write.DateTime(colIndex, i + 1, new Date(((Timestamp) value).getTime()),cellFormatDate); ws.addCell(dateCell); if (colWidth[colIndex] == 0) { ws.setColumnView(colIndex, 19); colWidth[colIndex] = 19; } } else if (value instanceof java.sql.Date) { jxl.write.DateTime dateCell = new jxl.write.DateTime(colIndex, i + 1, new Date(((java.sql.Date) value).getTime()),cellFormatDate); ws.addCell(dateCell); if (colWidth[colIndex] == 0) { ws.setColumnView(colIndex, 19); colWidth[colIndex] = 19; } } else if (value instanceof Double || value instanceof Integer || value instanceof Float || value instanceof Short || value instanceof BigDecimal) { jxl.write.Number number = new jxl.write.Number(colIndex, i + 1, Double.parseDouble(String .valueOf(value)), cellFormatNumber); ws.addCell(number); } else { Label labelCFC2 = new Label(colIndex, i + 1, String.valueOf(value), cellFormatText); ws.addCell(labelCFC2); } } colIndex++; } } Label label = new Label(0, list.size() + 2, "生成时间:" + TimeUtil.formatYMDHMS(new Date())); ws.addCell(label); label = new Label(0, list.size() + 3, "统计时间:" + reportDate); ws.addCell(label); } catch (Exception e) { log.error("导出EXCEL出错1", e); } finally { try { if (null != wwb) { wwb.write(); wwb.close(); } if (os != null) { os.close(); } } catch (Exception e) { log.error("导出EXCEL出错2", e); } } return true;![图片说明](http://img.ask.csdn.net/upload/201509/09/1441782386_403767.png) }
报错:
解决方案
这些东西有时候我也不会
解决方案二:
jsp导出excel表
jsp怎么导出Excel表
----------------------
时间: 2025-01-20 09:36:34