导出多个工作表格sheet

当数据量比较大的时候,如果超过了65536条的时候,导出的时候就会如下的错误:

jxl.write.biff.RowsExceededException: The maximum number of rows permitted on a worksheet been exceeded

(解释:jxl.write.biff.RowsExceededException:允许在工作表的最大行数已经超过)

所以,我的解决方案是导出多个工作表格。

public static void outputExcelData() throws IOException, WriteException {

        /**给List存值*/

        List result = new ArrayList();

        User user = new User();

        user.setId("1");

        user.setName("yfli");

        result.add(user);

        User user2 = new User();

        user2.setId("1");

        user2.setName("zhangjie");

        result.add(user2);

        User user3 = new User();

        user3.setId("1");

        user3.setName("lzhang");

        result.add(user3);   

        String fileName = "F:\\sfData.xls";

        //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象      

        WritableWorkbook wwb = Workbook.createWorkbook(new File(fileName));

        File dbfFile = new File(fileName);

        if (!dbfFile.exists() || dbfFile.isDirectory()) {

            dbfFile.createNewFile();

        }

        int totle = result.size();//获取List集合的size

        int mus = 2;//每个工作表格最多存储2条数据(注:excel表格一个工作表可以存储65536条)

        int avg = totle / mus;

        for (int i = 0; i < avg + 1; i++) {

            WritableSheet ws = wwb.createSheet("列表" + (i + 1), i);  //创建一个可写入的工作表  

            //添加表头

            ws.addCell(new Label(0, 0, "序号")); 

            ws.addCell(new Label(1, 0, "姓名"));

            int num = i * mus; 

            int index = 0; 

            for (int m = num; m < result.size(); m++) {

                if (index == mus) {//判断index == mus的时候跳出当前for循环

                    break;

                }

                User use = (User) result.get(m);

//将生成的单元格添加到工作表中 

//(这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行)     

                ws.addCell(new Label(0, index + 1, use.getId()));

                ws.addCell(new Label(1, index + 1, use.getName()));

                index++;

            }

        }

        wwb.write();//从内存中写入文件中   

        wwb.close();//关闭资源,释放内存      

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值