1

我处于两难境地。我一直在尝试通过 Java 中的 hssf/xssf 将数据写入现有的 excel 文件。我没有错误,但是当我运行它时仍然无法对 excel 表进行更改。谁能帮我解决这个问题?我的代码是:

try {
    FileInputStream inp = new FileInputStream("C:/Users/Training/Desktop/saurav.xlsx");  


            Workbook wb = null;
            try {
                wb = WorkbookFactory.create(inp);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            }  
            Sheet sheet = wb.getSheetAt(0);  
            Row row = sheet.getRow(3);  
            Cell cell = row.getCell(4);  
            if (cell == null)  
                cell = row.createCell(4);  
            cell.setCellType(Cell.CELL_TYPE_STRING);  
            cell.setCellValue("a test");  

            // Write the output to a file  
            FileOutputStream fileOut = new FileOutputStream("C:/Users/Training/Desktop/saurav.xlsx");  
            wb.write(fileOut);  
            fileOut.close();  
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

有人请帮我解决这个问题。自从过去 2 天以来一直在尝试。

4

1 回答 1

3

而不是使用这个,它在我的系统中工作。

try {
FileInputStream inp = new FileInputStream("C:/Users/Training/Desktop/saurav.xlsx");  


        Workbook wb = null;
        try {
            wb = WorkbookFactory.create(inp);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }  
        Sheet sheet = wb.getSheetAt(0);  
        Row row = sheet.getRow(3);  
        Cell cell = row.getCell(4);  
            cell = row.createCell(4);  
        cell.setCellType(Cell.CELL_TYPE_STRING);  
        cell.setCellValue("a test");  

        // Write the output to a file  
        FileOutputStream fileOut = new FileOutputStream("C:/Users/Training/Desktop/saurav.xlsx");  
        wb.write(fileOut);  
        fileOut.close();  
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

还要确保在每次运行后刷新 excel 文件。

于 2014-03-20T09:06:12.790 回答