gpt4 book ai didi

java - Apache POI SXSSF 和 XSSF

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:55:15 24 4
gpt4 key购买 nike

我有一个问题。我是对的,如果我有一个通过 xssf 构造函数创建的工作簿,那么将构造函数更改为 sxssf 工作簿就足够了(使用 xssf wb 作为参数传递)以使其在 流模式 下工作?非常感谢您的回答。

解决方案:这完全取决于您用于流式传输的类。如果您的类(class)收集的流缓冲区多于它可以容纳的数量,那么这个东西将无法工作。否则会

最佳答案

是的,你是对的。这两种实现方式的区别在于,stream 版本直接将数据写入stream 并在内存中存储最多指定行数(默认值为100,存储在SXSSFWorkbook.DEFAULT_WINDOW_SIZE 中)。因此,在写入输出流后您将无法获得一些行数据。使用流实现的一大好处是更少的内存使用。如果您需要导出大量数据,只需使用 SXSSFWorkbook。

例子:

public static void main(String[] args) throws IOException {
FileOutputStream inMemoryOut = new FileOutputStream(new File("inMemoryWorkbook.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook();
WorkbookExample example = new WorkbookExample(workbook, inMemoryOut);
example.export();

FileOutputStream streamOut = new FileOutputStream(new File("streamWorkbook.xlsx"));
SXSSFWorkbook streamWorkbook = new SXSSFWorkbook();
WorkbookExample streamExample = new WorkbookExample(streamWorkbook, streamOut);
streamExample.export();
}

public class WorkbookExample {

private Logger logger = Logger.getLogger(WorkbookExample.class.getName());
private Workbook workbook;
private OutputStream out;

public WorkbookExample(Workbook workbook, OutputStream out) {
this.workbook = workbook;
this.out = out;
}

public void export() throws IOException {
logger.info("export start for " + workbook.getClass().getName());

List<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 1000; i++) {
persons.add(new Person(String.valueOf("user_" + i)));
}

Sheet sheet = workbook.createSheet();
for (int i = 0; i < persons.size(); i++) {
Person p = persons.get(i);
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(p.getName());
}
workbook.write(out);
logger.info("Is row 1 accessible after writing to output stream? " + String.valueOf(sheet.getRow(1) != null));
out.close();
workbook.close();

logger.info("export finished for " + workbook.getClass().getName());
}

public static class Person {

private String name;

public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

}

输出:

kwi 21, 2015 7:56:14 PM pepuch.html2pdf.WorkbookExample export
INFO: export start for org.apache.poi.xssf.usermodel.XSSFWorkbook
kwi 21, 2015 7:56:15 PM pepuch.html2pdf.WorkbookExample export
INFO: Is row 1 accessible after writing to output stream? true
kwi 21, 2015 7:56:15 PM pepuch.html2pdf.WorkbookExample export
INFO: export finished for org.apache.poi.xssf.usermodel.XSSFWorkbook
kwi 21, 2015 7:56:15 PM pepuch.html2pdf.WorkbookExample export
INFO: export start for org.apache.poi.xssf.streaming.SXSSFWorkbook
kwi 21, 2015 7:56:15 PM pepuch.html2pdf.WorkbookExample export
INFO: Is row 1 accessible after writing to output stream? false
kwi 21, 2015 7:56:15 PM pepuch.html2pdf.WorkbookExample export
INFO: export finished for org.apache.poi.xssf.streaming.SXSSFWorkbook

如您所见,在使用 SXSSFWorkbook 写入输出流后,第 1 行不再可访问。

关于java - Apache POI SXSSF 和 XSSF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29778630/

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