繁体   English   中英

我无法在 Excel 工作表中写入数据

[英]I am not able to write data in Excel sheet

我的代码如下所示:

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

它在关闭之前抛出错误,并且还损坏了 Excel 文件。 我遇到了同样的错误,一旦它到达 .write 函数,它就会打开一个新选项卡,上面写着“找不到源”、“编辑源查找路径”。 选项卡的标题是 Integer.decode(String) line: not available。

尝试下面的代码片段来创建工作簿实例。

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

我认为您没有正确设置值。 见第三行代码

以下是我的工作代码:-

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

或者

确保在远程调试配置中选择了您的 eclipse 项目。

以下是参考,您也可以尝试:-

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

暂无
暂无

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

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