java - 通过hssf/xssf向现有excel单元格写入数据。

标签 java eclipse eclipse-plugin

我进退两难了。我一直在尝试通过 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 天以来一直在尝试这个。

最佳答案

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

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 文件。

关于java - 通过hssf/xssf向现有excel单元格写入数据。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888716/

相关文章:

重启后 Eclipse 语法着色重置

java - Eclipse 4.2 共享插件

java - Maven Java Fx 示例无法运行 - 不重复

java - 我们必须将 .class 文件放在 Tomcat 目录中的位置

java - 查找所有单行循环和 If 并添加括号

Android SDK 问题 Ubuntu 10.04lts

java - 如何让 Eclipse 识别动态 Web 服务代码中的更改

java - 为什么插件导出为 jar 并集成到 IDE 后,插件的所有图像都会消失?

java - 在hadoop中运行此程序时出错

java - 为什么我的插件无法加载到我的 Minecraft spigot 服务器上?