How to write an array data into excel using java

4.6k Views Asked by At

I have an array of Strings that needs to be inserted into an Excel sheet

I tried with the below code snippet, but only the first value is inserting and for the rest, I am getting null in my console.

String[] excelData = {"first", "second", "third", "fourth", "fifth"};

int rowStart = 1;

for (int count = 0; count <= excelData.length; count++) {   

    writeExcel(fileName, excelData[count], rowStart, 0);
    rowStart++;

}

UPDATE

Added writeExcel code.

public void writeExcel(String excelFile, String userNumber, int rows, int cols) {

   try{
       //Create an object of File class to open xlsx file
       File file = new File(excelFile);

       //Create an object of FileInputStream class to read excel file
       FileInputStream inputStream = new FileInputStream(file);
       Workbook requestsFile = null;

       //Find the file extension by splitting  file name in substring and getting only extension name
       String fileExtensionName = excelFile.substring(excelFile.indexOf("."));

       //Check condition if the file is xlsx file
       if(fileExtensionName.equals(".xlsx")){

           //If it is xlsx file then create object of XSSFWorkbook class
           requestsFile = new XSSFWorkbook(inputStream);

       } else if(fileExtensionName.equals(".xls")){

           //If it is xls file then create object of XSSFWorkbook class
           requestsFile = new HSSFWorkbook(inputStream);
       } else {
            System.out.println("File format is invalid");
       }

       //Read excel sheet by sheet name
       Sheet sheet = requestsFile.getSheetAt(0);

       Cell val1 = sheet.getRow(rows).getCell(cols);
       val1.setCellValue(userNumber);

       //Close input stream
       inputStream.close();

       //Create an object of FileOutputStream class to create write data in excel file
       FileOutputStream outputStream = new FileOutputStream(file);

       //write data in the excel file
       requestsFile.write(outputStream);

       //close output stream
       outputStream.close();
       } catch(Exception e){
           System.out.println(e.getMessage());
       }
}
2

There are 2 best solutions below

1
Shubham Jain On

First of all we all don't know what your function writeExcel do and what parameters accepts , as you havn't post here

taking the example as below of poi:

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

in above code, your loop must select one raw and increment the cellNum till you have data in array

0
frianH On

You are getting null because at the line:

Cell val1 = sheet.getRow(rows).getCell(cols);

This is get cell value from your workbook, not create. The null appear indicate the cell blank, and this causes your code flow to:

} catch(Exception e){
    System.out.println(e.getMessage());
}

So please try change this line with:

Cell val1 = sheet.createRow(rows).createCell(cols);

And change your loop count:

count <= excelData.length

It will cause an error out of range like: java.lang.ArrayIndexOutOfBoundsException: 5

Change with:

count < excelData.length

Hope this helps.