I have a java program which reads in a csv file and converts it into a xls file:
String file2 = "myFile.csv";
FileInputStream fis2 = new FileInputStream(file2);
DataInputStream myInput2 = new DataInputStream(fis2);
String thisLine;
arList = new ArrayList();
while ((thisLine = myInput2.readLine()) != null) {
al = new ArrayList();
String strar[] = thisLine.split(",");
for (int j = 0; j < strar.length; j++) {
al.add(strar[j]);
}
arList.add(al);
i++;
}
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for (int k = 0; k < arList.size(); k++) {
ArrayList ardata = (ArrayList) arList.get(k);
HSSFRow row = sheet.createRow((short) 0 + k);
for (int p = 0; p < ardata.size(); p++) {
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
FileOutputStream fileOut = new FileOutputStream(file2.replace("csv", "xls"));
hwb.write(fileOut);
fileOut.close();
myInput2.close();
fis2.close();
However, I have a CSV with more then 65536 lines and thus above code is resulting in error.
What could I do to above code to handle csv’s with more then 65536 lines.
Thanks for reading!
You problem is probably in the HSSFSheet class. In the this line
You are calling a method with short argument. The short type is only 2 bytes long so, the class can store only up to 65,535 rows.
If you have access to the class, you can refactor it, to use int or even long.