日期:2014-05-16 浏览次数:20404 次
String download_path = request.getSession().getServletContext().getRealPath("/") + "***.excel"; File file = new File(download_path); //生成个应的文件 WritableWorkbook book = Workbook.createWorkbook(file); WritableSheet sheet = book.createSheet("sheet", 0); //配置第一行的头 Label cardNoLabel = new Label(0, 0, "卡号"); Label cardTypeLabel = new Label(1, 0, "ICCID"); Label iccIdLabel = new Label(2, 0, "卡类型"); Label version = new Label(3, 0, "版本号"); Label m1NoLabel = new Label(4,0,"M1编号"); Label errorLabel = new Label(5, 0, "错误原因"); sheet.addCell(cardTypeLabel); sheet.addCell(cardNoLabel); sheet.addCell(iccIdLabel); sheet.addCell(errorLabel); sheet.addCell(version); sheet.addCell(m1NoLabel); //遍历生成相应数据 for (int i = 0; i < size; i++) { String[] objects = cardList.get(i); String cardType = objects[0]; String cardNo = objects[1].startsWith("0000") ? objects[1].substring(4) : objects[1]; String iccId = objects[2]; String m1No = objects[3]; Object cardNoExists = existsQuery.setString(0, cardNo).uniqueResult(); Object iccIdExists = iccIdQuery.setString(0, iccId).uniqueResult(); if (cardNoExists != null) { sheet.addCell(new Label(0, i - successNum + 1, cardNo)); sheet.addCell(new Label(1, i - successNum + 1, iccId)); sheet.addCell(new Label(2, i - successNum + 1, cardType)); sheet.addCell(new Label(3, i - successNum + 1, apVersion)); sheet.addCell(new Label(4, i - successNum + 1, m1No)); sheet.addCell(new Label(5, i - successNum + 1, "卡号已存在")); } else if (iccIdExists != null) { sheet.addCell(new Label(0, i - successNum + 1, cardNo)); sheet.addCell(new Label(1, i - successNum + 1, iccId)); sheet.addCell(new Label(2, i - successNum + 1, cardType)); sheet.addCell(new Label(3, i - successNum + 1, apVersion)); sheet.addCell(new Label(4, i - successNum + 1, m1No)); sheet.addCell(new Label(5, i - successNum + 1, "iccid已存在")); } } book.write(); book.close();
// 1.设定好response的相关属性: response.setContentType("application/vnd.ms-excel"); // URLEncoder.encode(qList.get("FUNCTION_NAME").toString(), "gbk") String downloadFileName = new String(qList.get("FUNCTION_NAME").toString().getBytes("GBK"), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadFileName + ".xls" + "\""); // 2.取到response的OutputStream实例,并用这个实例化一个WritableWorkbook对象 OutputStream os = response.getOutputStream(); WritableWorkbook wwb = Workbook.createWorkbook(os); int ncout = resultList.size(); int maxnum = 50000; // 一次最多写入量 int times = (ncout + maxnum - 1) / maxnum; // 大循环 for (int t = 0; t < times; t++) { // 新建一张表 WritableSheet wsheet = wwb.createSheet("members_" + (t + 1), t); // 设置表头 Map<String, Object> tableNameMap = null; Label label; for (int i = 0; i < tableNameList.size(); i++) { tableNameMap = tableNameList.get(i); label = new Label(i, 0, tableNameMap.get("COLUMN_CHIN").toString()); wsheet.addCell(label); }