gpt4 book ai didi

java - Android + POI Excel + 在现有文件中添加数据

转载 作者:行者123 更新时间:2023-11-29 04:45:58 24 4
gpt4 key购买 nike

我试图将数据附加到现有的 excel 文件中,但没有写入任何内容。我做错了什么?

    public static void writeSheet(String data) {
HSSFWorkbook workbook;

try {
String outFileName = "filebook.xls";

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File outFile = new File(path, outFileName);
workbook = (HSSFWorkbook) WorkbookFactory.create(outFile);

Row row = workbook.getSheetAt(0).createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("text");

OutputStream outputStream = new FileOutputStream(outFile,true);
workbook.write(outputStream);

outputStream.close();
workbook.close();

} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

代替

File outFile = new File(path, outFileName);
workbook = (HSSFWorkbook) WorkbookFactory.create(outFile);

尝试

FileInputStream outFile = new FileInputStream(new File(path));
workbook = new HSSFWorkbook(outFile);

所以你的代码

public static void writeSheet(String data) {
HSSFWorkbook workbook;

try {
String outFileName = "filebook.xls";

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

FileInputStream file = new FileInputStream(new File(path, outFileName))
workbook = new HSSFWorkbook(file);

Row row = workbook.getSheetAt(0).createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("text");

file.close();

FileOutputStream outFile =new FileOutputStream(new File(path, outFileName));
workbook.write(outFile);
outFile.close();

} catch (Exception e) {
e.printStackTrace();
}

关于java - Android + POI Excel + 在现有文件中添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205155/

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