简体   繁体   中英

I am not able to write data in Excel sheet

My code looks like this:

public void method(String value,int row,int column) throws Exception {  
    try {    
        FileInputStream inp = new FileInputStream("C:\\Users\\sit2autouser1\\Desktop\\Newdatasheet.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheet("Sheet2");
        Cell cell = sheet.getRow(row).getCell(column);
        String cellContents = value;
        // Modify the cellContents here
        // Write the output to a file
        cell.setCellValue(cellContents);
        FileOutputStream fileOut = new FileOutputStream("C:\\Users\\sit2autouser1\\Desktop\\Newdatasheet.xlsx");
        wb.write(fileOut);
        fileOut.close();
    } catch (Exception e) {
        throw (e);
    }
}   

It is throwing error just before close and it is also corrupting the Excel file. I am getting the same error, as soon as it reaches .write function it opens up a new tab which says "Source not found","Edit Source Look up path". And the heading of the tab is Integer.decode(String) line: not available.

Try below code snippet for creating workbook instance.

FileInputStream fis = new FileInputStream(new File("C:\\Users\\sit2autouser1\\Desktop\\Newdatasheet.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);

I think you are not setting value properly. see the third line of code

Below is my working code:-

        FileInputStream fis=new FileInputStream(xlpath);

        Workbook wb=WorkbookFactory.create(fis);

        wb.getSheet(sheetName).getRow(rowNum).createCell(cellNum).setCellValue(input);

        FileOutputStream fos=new FileOutputStream(xlpath);

        wb.write(fos);

        fos.close();

OR

Make sure that in the remote debug configuration your eclipse project is selected.

Below is the reference, you can try that also:-

http://stackoverflow.com/questions/6174550/eclipse-java-debugging-source-not-found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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