steady_progress
steady_progress

Reputation: 3731

Putting result of Java program into Excel cell

I simply want to put the result of a Java program into an Excel cell. Let's assume the result is 5, then my code in main() would look like this:

    XSSFWorkbook wb = C:\Users\Username\Desktop\java-programs\results.xlsm;
    XSSFSheet ExcelSheet = wb.getSheet("numbers");
    XSSFRow row = ExcelSheet.getRow(0);
    XSSFCell cell = row.getCell(0);
    cell.setCellValue(5);

However, this code doesn't compile.

I read some examples on this topic but the code given simply refers to the Excel sheet as (e.g.) "sheet" and then goes on with sheet.getRow(some number). The examples don't explain how I tell Java which workbook to write to.

Upvotes: 1

Views: 216

Answers (1)

ergonaut
ergonaut

Reputation: 7057

Your first line is incorrect.. try this:

 String fileName = "C:/Users/Username/Desktop/java-programs/results.xlsm";
 FileInputStream fileIn = new FileInputStream(fileName);
 Workbook wb = WorkbookFactory.create(fileIn);

 // update the workbook
 wb.getSheet("numbers").getRow(0).getCell(0).setCellValue(5);

 fileIn.close();

 FileOutputStream fileOut = new FileOutputStream(fileName);
 wb.write(fileOut);
 fileOut.close();

Upvotes: 1

Related Questions