¿Cómo descargo periódicamente las filas en Excel usando jxl mientras hago el cierre en el último?
Frecuentes
Visto 1,927 equipos
0
Below is my code. I first create headers in init method. Then in pushData I fill the rows. The problem is once I do write and flush in init method nothing else comes in my excel sheet.
The rows that I would be writing to excel could be huge. The idea of using flush is not free the memory periodically.
Please tell me what mistake I am doing here. How do i achieve what I intent?l
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExportTranscriptDetailsToExcel implements IExportToFormat {
private static final ILogger logger = ILabsPlatform.getLogger(ExportTranscriptDetailsToExcel.class.getName());
OutputStream outputStream;
List<String> labels;
WritableWorkbook workbook;
WritableSheet sheet0;
public ExportTranscriptDetailsToExcel() {
this.outputStream = null;
workbook = null;
sheet0 = null;
}
@Override
public void init(String sheetName, List<String> labels, OutputStream outputStream) throws IOException,
RowsExceededException, WriteException {
this.outputStream = outputStream;
this.labels = labels;
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setEncoding("Cp1252");
workbook = Workbook.createWorkbook(outputStream, workbookSettings);
sheet0 = workbook.createSheet(sheetName, 0);
for (int i = 0; i < labels.size(); ++i) {
Label label = new Label(i, 0, labels.get(i));
sheet0.addCell(label);
}
workbook.write();
outputStream.flush();
}
@Override
public void pushDataRows(Object listOfResultRow) {
if ((listOfResultRow == null)) {
return;
}
String fieldName = null;
String fieldValue = null;
@SuppressWarnings("unchecked")
ArrayList<Map<String, Object>> interactionMap = (ArrayList<Map<String, Object>>) listOfResultRow;
try {
int i = 1;// the data rows starts from row1
for (Map<String, Object> element : interactionMap) {
for (int j = 0; j < labels.size(); j++) {
fieldName = labels.get(j);
Object ob = element.get(fieldName);
if (ob != null) {
fieldValue = ob.toString();
}
if (fieldValue == null) {
fieldValue = "-";
}
System.out.println("***********************fieldName:" + fieldName);
System.out.println("***********************fieldValue:" + fieldValue);
Label label1 = new Label(j, i, fieldValue);
fieldValue = null;
sheet0.addCell(label1);
}
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
workbook.write();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void done() {
try {
workbook.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2 Respuestas
1
Como sugerido aquí
To get around the memory problem, you can signal jxl to use temporary files when writing. This will write data to a temporary file during execution rather than storing it in memory.
You need to adjust your WorkbookSettings:
workbookSettings.setUseTemporaryFileDuringWrite(true);
workbookSettings.setTemporaryFileDuringWriteDirectory(new File("your_temporary_directory"));
Replace your_temporary_directory above with a temporary directory you prefer
Also note that this feature is available in jxl version >= 2.6.9
contestado el 23 de mayo de 17 a las 11:05
0
I think I have found the problem with my code. This is just out of hit and trial and I would certainly need someone to tell if I am correct.
In the init method, if I only do outputStream.flush() the I don't see a problem. I think doing a workbook.write() kind of closes the stream for any further writing.
So basically do outputStream.flush() everytime you want to flush out of memory.
Do workbook.write() and workbook.close() in the last
Respondido el 12 de junio de 14 a las 12:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java excel jxl or haz tu propia pregunta.