1.xls一个sheet只能装65536行,多余则报错

poi包导出或写入excel超出65536报错: 
java.lang.IllegalArgumentException: Invalid row number (65536) outside allow

解决:每6w分一个sheet,关键代码红色部分

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; public class ExcelUtil {
public static HSSFWorkbook getHSSFWorkbook(String sheetName, JsonArray title,
String[][] values, HSSFWorkbook wb) { // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
} // -个sheet限制存65536条,此处仅存60000
System.out.println(values.length);
final int sheetNum = (int) Math.ceil((float) values.length / 60000);
for (int n = 1; n <= sheetNum; n++) { // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
final HSSFSheet sheet = wb.createSheet(sheetName + "_" + n);
System.out.println("sheetName" + sheetName + "_" + n);
sheet.setDefaultColumnWidth(12); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row; // 第四步,创建单元格,并设置值表头样式
final HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中
final Font fontStyle = wb.createFont(); // 字体样式
fontStyle.setBoldweight(Font.BOLDWEIGHT_BOLD); // 加粗
fontStyle.setFontName("黑体"); // 字体
fontStyle.setFontHeightInPoints((short) 11); // 大小
// 将字体样式添加到单元格样式中
headerStyle.setFont(fontStyle);
// 边框
headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
headerStyle.setBorderRight(CellStyle.BORDER_THIN);
headerStyle.setBorderTop(CellStyle.BORDER_THIN); // 普通单元格样式,边框,水平居中
final HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
// cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
final HSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式
cellStyle.setDataFormat(df.getFormat("#,#0.0")); // 小数点后保留两位,可以写contentStyle.setDataFormat(df.getFormat("#,#0.00"));
// 声明列对象
HSSFCell cell = null; // 创建标题
JsonArray cellArray = new JsonArray();
JsonObject object = new JsonObject();
JsonObject temobj = new JsonObject();
int rowSkip = 0, cellSkip = 0; String tempCell = "";
for (int rowId = 0; rowId < title.size(); rowId++) {
row = sheet.createRow(rowId);
object = title.get(rowId).getAsJsonObject();
cellArray = object.get("row").getAsJsonArray(); // colId为excel列索引,cellId为行标题值的数组索引,cellId遇到当前单元格已使用时,填充至下一个可使用的单元格
for (int colId = 0, cellId = 0; cellId < cellArray.size(); colId++) {
cell = row.createCell(colId);
cell.setCellStyle(headerStyle); if (isMergedRegion(sheet, rowId, colId)) {
continue;
}
temobj = cellArray.get(cellId).getAsJsonObject();
tempCell = temobj.get("cellvalue").toString().replace("\"", "");
try {
// System.out.println(tempCell);
tempCell = new String(tempCell.getBytes("UTF-8"), "ISO-8859-1");
// System.out.println(tempCell);
tempCell = new String(tempCell.getBytes("ISO-8859-1"), "UTF-8");
// System.out.println(tempCell);
} catch (final Exception e) {
e.printStackTrace();
}
cell.setCellValue(tempCell);
// System.out.println(cell);
// System.out.println(cell.getStringCellValue());
cell.setCellStyle(headerStyle);
cellId++; // 合并单元格
rowSkip = temobj.get("rowspan").getAsInt();
cellSkip = temobj.get("colspan").getAsInt();
// System.out.println(rowSkip + "=skip==" + cellSkip);
if (rowSkip > 1 && rowSkip-- > 0 || cellSkip > 1 && cellSkip-- > 0) {// 用于起始行列计算时需减1
final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,
colId, colId + cellSkip); // 起始行, 终止行, 起始列, 终止列 // 终止行,
sheet.addMergedRegion(cra);
}
}
}
// 创建内容 -个sheet只能存65536条
for (int i = 0; i < 60000 && i < values.length - (n - 1) * 60000; i++) {
row = sheet.createRow(title.size() + i);
for (int j = 0; j < values[i].length; j++) {
// 将内容按顺序赋给对应的列对象
cell = row.createCell(j);
cell.setCellValue(values[(n - 1) * 60000 + i][j]);
cell.setCellStyle(cellStyle); }
}
}
return wb;
} public static boolean isMergedRegion(HSSFSheet sheet, int row, int column) {
final int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
final CellRangeAddress range = sheet.getMergedRegion(i);
final int firstColumn = range.getFirstColumn();
final int lastColumn = range.getLastColumn();
final int firstRow = range.getFirstRow();
final int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
return true;
}
}
}
return false;
}
}

poi excel超出65536行数限制自动扩展Invalid row number (65536) outside allow的更多相关文章

  1. C# Excel导出超出65536行报错 Invalid row number (65536) outside allowable range (0..65535)

    C# Excel导出超出65536行报错 Invalid row number (65536) outside allowable range (0..65535) 一:报错 Invalid row ...

  2. 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法

    用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count Ac ...

  3. Excel的最大行数

    使用Excel2007或Excel2010,在“另存为” 菜单中可以选择为“Excel 07-2003 工作薄”,从中我们可以看出,到了2007版以后,存储格式变了,简单一点从扩展名便可以看出,一个是 ...

  4. Office EXCEL VBA如何取得EXCEL中的行数和列数

    VBA取得EXCEL表格中的行数和列数 请注意不要使用Columus等关键字作为变量,例如"Columus = ActiveSheet.UsedRange.Columns.Count&quo ...

  5. css文字超出指定行数显示省略号

    display: -webkit-box; overflow: hidden; word-break: break-all; /* break-all(允许在单词内换行.) */ text-overf ...

  6. CSS文本超出指定行数省略显示

    单行: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 2行: font-size: 17px;overflow: hi ...

  7. Excel 2010 统计行数

    1. 首先选择一个空行 2.然后点击如下:公式 --- 3. 第一行:填写“103”,当然我也不能明白为啥填写103.照做就是了. 4.鼠标定位到 第二行 “Ref1”位置,然后 鼠标+shfit 按 ...

  8. css 超出规定行数自动隐藏

     单行overflow: hidden;text-overflow: ellipsis;white-space: nowrap;  多行(兼容各个浏览器)//通过覆盖最后几个字的形式p{positio ...

  9. excel运行最多行数

    2003及以前版本为65536(即6万多)行,256列2007版:1048576(即1百零多万)行,16384(即1千多)列

随机推荐

  1. Inno Setup 脚本

    给你个我用的例子: Delphi/Pascal code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...

  2. noi2017 day2t2

    设a[i]为当前方案中第 1..i 天变质的蔬菜有几个,b[i]为前i天至少能卖出几个,方案可行的条件是对任意i有a[i]<=b[i],用线段树维护b[i]-a[i]. 从小到大枚举天数,枚举到 ...

  3. Linux CentOS 下关闭防火墙

    永久关闭(重启后生效) 开启: chkconfig iptables on 关闭: chkconfig iptables off 临时关闭(重启后失效) 开启: service iptables st ...

  4. DP 01背包 七夕模拟赛

    问题 D: 七夕模拟赛 时间限制: 1 Sec  内存限制: 128 MB提交: 60  解决: 23[提交][状态][讨论版] 题目描述 " 找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手 ...

  5. 【ZZ】终于有人把云计算、大数据和人工智能讲明白了!

    终于有人把云计算.大数据和人工智能讲明白了! https://mp.weixin.qq.com/s/MqBP0xziJO-lPm23Bjjh9w 很不错的文章把几个概念讲明白了...图片拷不过来... ...

  6. Linux系统安装(centos6.8)符破解码

    1.安装 VMware VMware 是一个虚拟 PC 的软件,可以在现有的操作系统上虚拟出一个新的硬件环境,相当于模拟出一台新的 PC,我们可以在上面构造出一个或多个别的系统,以此来实现在一台机器上 ...

  7. Jmeter(三十二)Jmeter Question 之 “自定义函数开发”

    “技术是业务的支撑”,已经不是第一次听到这句话,因为有各种各样的需求,因此衍生了许多各种各样的技术.共勉! 前面有提到提到过Jmeter的安装目录结构,也提到Jmeter的常用函数功能,有部分工作使用 ...

  8. 使用fakeroot模拟root权限执行程序(转)

    Hack #57: 使用fakeroot模拟root权限执行程序 fakeroot是什么 例如Debian在生成package的时候,编译完之后,不能立刻在当前环境执行make install,需要执 ...

  9. POJ3635 Full Tank?

    [题解] 用dijkstra算法求最短路.同时考虑在每个节点加油(一单位)与否. [代码] #include <iostream> #include <map> #includ ...

  10. sqoop导出mysql数据进入hive错误

    看mr的运行显示:sqoop job可以获得的select max(xxx)结果,但是当mr开始时却显示大片错误,就是连接超时,和连接重置等问题, 最后去每个节点ping mysql的ip地址,发现 ...