Reputation: 831
This is the code I used for writing two strings to an Excel sheet. But I am getting this error : java.lang.IllegalArgumentException: Sheet index (0) is out of range (no sheets)
FileOutputStream fout=new FileOutputStream(new File("C:\\Users\\151680\\workspace\\trial.xlsx"));
XSSFWorkbook wb=new XSSFWorkbook();
XSSFSheet sh1=wb.getSheetAt(0);
sh1.getRow(0).createCell(0).setCellValue("abc");
sh1.getRow(0).createCell(1).setCellValue("def");
wb.write(fout);
Currently there is no workbook called trial.xlsx in the given path. When this code is run its supposed to create the workbook right? Or do I have to create and keep one in the path? What am I doing wrong here?
Upvotes: 0
Views: 437
Reputation: 69495
No excelsheet is created because you get the exception. If you want to create an excel sheet you have to call wb.createSheet()
insteat of wb.getSheetAt(0);
FileOutputStream fout=new FileOutputStream(new File("C:\\Users\\151680\\workspace\\trial.xlsx"));
XSSFWorkbook wb=new XSSFWorkbook();
XSSFSheet sh1=wb.createSheet();
XSSFRow row = sh1.createtRow(0);
row.createCell(0).setCellValue("abc");
row.createCell(1).setCellValue("def");
wb.write(fout);
fout.close();
Upvotes: 4