我尝试使用 apache POI 将数据放入模板 .xls 文件(如果需要,可以是 xlsx),但我无法弄清楚并且该文件仍然未更改。 PrintStackTrace 中没有抛出异常。您能给我提供一个工作代码吗?我读了很多文档,所以请帮助我编写工作代码。谢谢
我的代码:
final Button but = (Button) findViewById(R.id.bk);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
writeXLSFile(3, 3);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
public static void writeXLSFile(int row, int col) throws IOException {
try {
FileInputStream file = new FileInputStream(Environment.getExternalStorageDirectory().toString() +"telegran/"+ "ex.xls");
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell = null;
//Update the value of cell
cell = sheet.getRow(row).getCell(col);
cell.setCellValue("changed");
file.close();
FileOutputStream outFile =new FileOutputStream(new File(Environment.getExternalStorageDirectory().toString() +"telegran/"+ "ex.xls"));
workbook.write(outFile);
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最佳答案
尝试 file.close();在workbook.write(outFile)之后; outFile.close();
关于java - 如何在android中使用apache POI在现有的xls或xlsx文件上写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36179927/