Ho avuto lo stesso problema con 800.000 celle e 3 milioni di caratteri in cui XSSF alloca 1 GB di heap!
Ho usato Python con openpyxl
e numpy
per leggere il file xlsx (dal codice Java) e convertirlo prima in un testo normale. Quindi ho caricato il file di testo in java. Potrebbe sembrare che abbia un grande sovraccarico, ma è davvero veloce.
Lo script python sembra
import openpyxl as px
import numpy as np
# xlsx file is given through command line foo.xlsx
fname = sys.argv[1]
W = px.load_workbook(fname, read_only = True)
p = W.get_sheet_by_name(name = 'Sheet1')
a=[]
# number of rows and columns
m = p.max_row
n = p.max_column
for row in p.iter_rows():
for k in row:
a.append(k.value)
# convert list a to matrix (for example maxRows*maxColumns)
aa= np.resize(a, [m, n])
# output file is also given in the command line foo.txt
oname = sys.argv[2]
print (oname)
file = open(oname,"w")
mm = m-1
for i in range(mm):
for j in range(n):
file.write("%s " %aa[i,j] )
file.write ("\n")
# to prevent extra newline in the text file
for j in range(n):
file.write("%s " %aa[m-1,j])
file.close()
Poi nel mio codice Java, ho scritto
try {
// `pwd`\python_script foo.xlsx foo.txt
String pythonScript = System.getProperty("user.dir") + "\\exread.py ";
String cmdline = "python " + pythonScript +
workingDirectoryPath + "\\" + fullFileName + " " +
workingDirectoryPath + "\\" + shortFileName + ".txt";
Process p = Runtime.getRuntime().exec(cmdline);
int exitCode = p.waitFor();
if (exitCode != 0) {
throw new IOException("Python command exited with " + exitCode);
}
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (InterruptedException e) {
ReadInfo.append(e.getMessage());
}
Dopo di che, si otterrà foo.txt che è simile a foo.xlsx, ma in formato di testo
Dove si esegue questo codice? App interna/server Web o autonomo? – JSS
Lo sto eseguendo all'interno di Tomcat 6.0 – miah
Che cosa è la memoria di default assegnata a Tomcat all'avvio? – JSS