简体   繁体   中英

RowsExceededException when using big number of rows

I try to create a very large excel file with java jxl, a file of more than 20 gb, the problem is that when I try to make a loop a little bigger to generate more line, I have a error message

"jxl.write.biff.RowsExceededException: The maximum number of rows permitted on a worksheet been exceeded
  at jxl.write.biff.WritableSheetImpl.getRowRecord(WritableSheetImpl.java:975)
  at jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:951)
  at test2.Test2.main(Test2.java:66)

this is my code

try { 
        WritableWorkbook workbook = Workbook.createWorkbook(new File("sortie.xls")); 
        WritableSheet sheet = workbook.createSheet("Premier classeur", 0); 
        //Crée le format d’une cellule 
        WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD, true, UnderlineStyle.NO_UNDERLINE,Colour.BLACK, ScriptStyle.NORMAL_SCRIPT); 
        WritableCellFormat arial10format = new WritableCellFormat(arial10font); 
            //Crée un label à la ligne 0, colonne 0 avec le format spécifique 

            for (int i = 0; i < 100000; i++) {


                    String chars = "abcdefghijklmnopqrstuvwxyz" ;
                    String chars2 ="azertyuiopqsdfghjklmwxcvbn" ;
                    String chars3 ="aqwzsxedcrfvtgbyhnujikolpm" ;
                    String pass = "";
                    String pass2 = "";
                    String pass3 = "";
                    int alava = 6 ;
                    for (int x = 0; x < alava; x++) {
                        int p = (int)Math.floor(Math.random()*26);
                        pass +=chars.charAt(p);
                        pass2 +=chars2.charAt(p);
                        pass3 +=chars3.charAt(p);
                    }
                    Label label = new Label(0, i, pass); 
                    Label label2 = new Label(1, i, pass2); 
                    Label label3 = new Label(2, i, pass3); 
                    //Crée un label à la ligne 2, colonne 0 sans style prédéfini 
                    //Label label2 = new Label(0, 2, "Résultat"); 
                    //Ajout des cellules 
                    sheet.addCell(label); 
                    sheet.addCell(label2);
                    sheet.addCell(label3);


        }

            //Ajout d’une cellule ligne 2, colonne 1 
            //Number number = new Number(1, 2, 3.1459); 
            //sheet.addCell(number); 
            //Ajout d’une image ligne 4, colonne 0 
            //Taille de l’image : 6 lignes et 2 colonnes 
            //WritableImage image = new WritableImage(0, 4, 2, 6,new File("Logo-Labo-Sun.png")); 
            //sheet.addImage(image); 
            //Ecriture et fermeture du classeur 
            workbook.write(); 
            workbook.close(); 
            } catch (RowsExceededException e1) { 
            e1.printStackTrace(); 
            } catch (WriteException e1) { 
            e1.printStackTrace(); 
            } catch (IOException e) { 
            e.printStackTrace(); 
        }finally{ 
        System.out.println("Le fichier \"sortie.xls\" à été généré correctement."); 

    } 
    // TODO code application logic here
}

Given that you seem to have no use for any advanced Excel capability and to only want to generate tabulated data you can open in your spreasheet editor of choice, I think you should consider generating a CSV file.

The following code produces similar data to your own without the need for any additional library and I've been able to open the resulting file in Excel simply by double-clicking it from Window's file explorer.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TestCSV {

    private static final String chars = "abcdefghijklmnopqrstuvwxyz";
    private static final String chars2 ="azertyuiopqsdfghjklmwxcvbn";
    private static final String chars3 ="aqwzsxedcrfvtgbyhnujikolpm";
    private static final int alava = 6;

    public static void main(String[] args) throws IOException {
        try (FileWriter out = new FileWriter(new File("sortie.csv"))) {
            for (int i = 0; i < 100000; i++) {
                String pass="", pass2="", pass3="";
                for (int x=0; x < alava; x++) {
                    int p = (int)Math.floor(Math.random()*26);
                    pass +=chars.charAt(p);
                    pass2 +=chars2.charAt(p);
                    pass3 +=chars3.charAt(p);
                }
                out.write(String.format("\"%s\";\"%s\";\"%s\"%n", pass, pass2, pass3));
            }
        }
    }

}

The problem with your code is exceed the write limit on an XLS file. The limit is 65536 rows. The following code fragment illustrates how to fix

WritableWorkbook workbook = Workbook.createWorkbook(new File("file_test.xls")); 
WritableSheet sheet = workbook.createSheet("Test sheet", 0); 

for (int i = 0; i < 65536; i++) {
    ...
}

PS: To generate a 20 GB file, it is necessary to create several XLS files, when the limit of 65536 rows is reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM