gpt4 book ai didi

java - 如何使用 jxl 定期刷新 excel 中的行,同时最后关闭

转载 作者:行者123 更新时间:2023-12-01 12:51:44 26 4
gpt4 key购买 nike

下面是我的代码。我首先在 init 方法中创建 header 。然后在pushData 中我填充行。问题是,一旦我在 init 方法中写入并刷新,我的 Excel 工作表中就没有其他内容了。

我要写入 Excel 的行可能会很大。使用flush的思想并不是定期释放内存。

请告诉我我在这里犯了什么错误。我如何实现我的意图?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();
}

}
}

最佳答案

按照建议here

为了解决内存问题,您可以在写入时通知 jxl 使用临时文件。这将在执行期间将数据写入临时文件,而不是将其存储在内存中。

您需要调整您的 WorkbookSettings:

workbookSettings.setUseTemporaryFileDuringWrite(true);
workbookSettings.setTemporaryFileDuringWriteDirectory(new File("your_temporary_directory"));

将上面的 your_temporary_directory 替换为您喜欢的临时目录

另请注意,此功能在 jxl 版本 >= 2.6.9 中可用

关于java - 如何使用 jxl 定期刷新 excel 中的行,同时最后关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com