Invalid row number (65536) outside allowable range (0..65535)

问题描述:导出后,导出的表格没有任何内容
问题原因:数据量过大,导致后台报错Invalid row number (65536) outside allowable range (0..65535),Excel一张(或者说一页)默认行数是65535
解决思路:行数过多时,新建一页
代码:

response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(title+".xls", "UTF-8"));
			response.setContentType("application/octet-stream");
			  // 声明一个工作薄
		    HSSFWorkbook workbook = new HSSFWorkbook();
			/*
			* 每页数据量为65530,超过的换到下一页
			* */
			int page = dataset.size()%65530;
			int dataCount = dataset.size();
			int pageNo;
			if (page == 0) {
				pageNo = dataCount / 65530;
			} else {
				pageNo = dataCount / 65530 + 1;
			}
			for (int p = 1; p <= pageNo; p++) {
				// 生成一个表格
				HSSFSheet sheet = workbook.createSheet(title.split(",")[0]+""+p);
				// 设置表格默认列宽度为15个字节
				sheet.setDefaultColumnWidth((short) 25);
				//头部标题样式
				HSSFCellStyle style0 = workbook.createCellStyle();
				style0.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
				style0.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				style0.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				style0.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				style0.setBorderRight(HSSFCellStyle.BORDER_THIN);
				style0.setBorderTop(HSSFCellStyle.BORDER_THIN);
				style0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				HSSFFont font0 = workbook.createFont();
				font0.setColor(HSSFColor.BLACK.index);
				font0.setFontHeightInPoints((short) 20);
				font0.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				style0.setFont(font0);
				// 表头样式
				HSSFCellStyle style = workbook.createCellStyle();
				style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
				style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				style.setBorderRight(HSSFCellStyle.BORDER_THIN);
				style.setBorderTop(HSSFCellStyle.BORDER_THIN);
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				HSSFFont font = workbook.createFont();
				font.setColor(HSSFColor.BLACK.index);
				font.setFontHeightInPoints((short) 12);
				font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				style.setFont(font);
				//正文样式
				HSSFCellStyle style2 = workbook.createCellStyle();
				style2.setFillForegroundColor(HSSFColor.WHITE.index);
				style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
				style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
				style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
				style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
				style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
				HSSFFont font2 = workbook.createFont();
				font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
				font2.setFontHeightInPoints((short) 14);
				style2.setFont(font2);


				HSSFRow rowName = sheet.createRow((short) 0);
				//(列开始,行开始,列结束,行结束)
				sheet.addMergedRegion(new Region(0, (short)0, 0, (short) (headers.length-1)));
				HSSFCell ce = rowName.createCell((short) 0);
				ce.setCellValue(title);
				ce.setCellStyle(style0);

				//产生表格标题行
				HSSFRow row = sheet.createRow(1);
				for (short i = 0; i < headers.length; i++) {
					HSSFCell cell = row.createCell(i);
					cell.setCellStyle(style);
					HSSFRichTextString text = new HSSFRichTextString(headers[i]);
					cell.setCellValue(text+""+p);
				}

				for (int k = 65530 * p - 65530; k < 65530 * p && k < dataCount; k++) {
					row = sheet.createRow(k - 65530 * p + 65532);
					Object t = dataset.get(k);
					Field[] fields = t.getClass().getDeclaredFields();
					int j=0;
					List colsList = new ArrayList(list);

					for (short i = 0; i < fields.length; i++) {
						if(colsList.contains(cols[i])){
							HSSFCell cell = row.createCell(j);
							j++;
							Field field = fields[i];
							String fieldName = field.getName();

							String getMethodName = "get"
									+ fieldName.substring(0, 1).toUpperCase()
									+ fieldName.substring(1);
							Class tCls = t.getClass();
							Method getMethod = tCls.getMethod(getMethodName,
									new Class[] {});
							Object value = getMethod.invoke(t, new Object[] {});
							//判断值的类型后进行强制类型转换
							cell.setCellStyle(style2);
							String textValue = getValue(value);
							if(textValue!=null&&!"".equals(textValue)){
								cell.setCellValue(textValue);
							}else{
								cell.setCellValue("--");
							}
							colsList.remove(0);
						}

					}
				}
				/*//遍历集合数据,产生数据行
				Iterator<Object> it = dataset.iterator();
				int index = 1;
				while (it.hasNext()) {
					index++;
					row = sheet.createRow(index);
					Object t = it.next();
					//利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
					Field[] fields = t.getClass().getDeclaredFields();
					int j=0;
					List colsList = new ArrayList(list);

					for (short i = 0; i < fields.length; i++) {
						if(colsList.contains(cols[i])){
							HSSFCell cell = row.createCell(j);
							j++;
							Field field = fields[i];
							String fieldName = field.getName();

							String getMethodName = "get"
									+ fieldName.substring(0, 1).toUpperCase()
									+ fieldName.substring(1);
							Class tCls = t.getClass();
							Method getMethod = tCls.getMethod(getMethodName,
									new Class[] {});
							Object value = getMethod.invoke(t, new Object[] {});
							//判断值的类型后进行强制类型转换
							cell.setCellStyle(style2);
							String textValue = getValue(value);
							if(textValue!=null&&!"".equals(textValue)){
								cell.setCellValue(textValue);
							}else{
								cell.setCellValue("--");
							}
							colsList.remove(0);
						}

					}

				}
				if(totals!=null&&totals.size()>0){
					row = sheet.createRow(index+1);
					for (int i = 0; i < totals.size(); i++) {
						HSSFCell cell = row.createCell(i);
						cell.setCellStyle(style2);
						cell.setCellValue(totals.get(i));
					}
				}*/
			}

			OutputStream out = response.getOutputStream(); 
			workbook.write(out);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值