我尝试使用java jxl创建一个非常大的excel文件,该文件大小超过20 gb,问题是当我尝试将一个循环做得更大一点以生成更多行时,我收到一条错误消息>
"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)
这是我的代码
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
}
答案 0 :(得分:0)
鉴于您似乎没有使用任何高级Excel功能,并且只想生成可以在所选的电子表格编辑器中打开的列表数据,我认为您应该考虑生成CSV文件。
以下代码可生成与您自己相似的数据,而无需任何其他库,而且我只需在Window的文件浏览器中双击它即可在Excel中打开结果文件。
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));
}
}
}
}
答案 1 :(得分:0)
您的代码问题超出了XLS文件的写限制。限制为65536行。以下代码片段说明了解决方法
WritableWorkbook workbook = Workbook.createWorkbook(new File("file_test.xls"));
WritableSheet sheet = workbook.createSheet("Test sheet", 0);
for (int i = 0; i < 65536; i++) {
...
}
PS:要生成20 GB的文件,必须达到65536行的限制,然后创建几个XLS文件。