java - 如何在android中使用apache POI在现有的xls或xlsx文件上写入数据

标签 java android apache apache-poi xls

我尝试使用 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/

相关文章:

node.js - 如何使用具有不同端口的单个 ssl 证书运行 Node js 和 apache?

android - 无法在 Android Market 中找到应用程序

java - com.github.bumptech.glide :glide making app crash

java - 如何使无效数字不添加到我的最终计算和计数器总计中?

java - 如何使用通用枚举实现 Action 监听器?

java - 在android中传输文件

php - .htaccess 错误文档重定向

Apache 404 错误记录到不同的日志文件

java - Java 中全文 MSSQL 表上的PreparedStatement 仅需要 3 个参数

Java:repaint() 不在 for 循环中调用paintComponent()