我试图将数据附加到现有的 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/