简体   繁体   English

通过hssf / xssf将数据写入现有excel中的单元格。

[英]Writing data to cell in existing excel through hssf/xssf.

I am in a dilemna. 我陷入了困境。 I have been trying to write data into an existing excel file through hssf/xssf in Java. 我一直在尝试通过Java中的hs​​sf / xssf将数据写入现有的excel文件。 I have no errors but still when I run it can't get change made to the excel sheet. 我没有错误,但是当我运行它时,无法对excel表进行更改。 Can anyone help me out with this? 任何人都可以帮我解决这个问题吗? My code is: 我的代码是:

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();
    }

Someone please help me out with this.Trying this out since last 2 days. 有人请帮助我解决这个问题。从最近2天开始就这样做了。

Instead of that use this, its working in my system. 而不是使用它,它在我的系统中工作。

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();
}

also make sure that you refresh the excel file after every run. 还要确保每次运行后刷新excel文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM