How to Convert Excel to Array in Java

This brief tutorial guides on how to convert Excel to array in Java by following a step-by-step process. Using this procedure you will learn that how does java convert excel to array that can further be used to store it in a database, use in your program logic or return it to some other calling function. Here we have loaded an XLSX file however, you can load any other Excel file format as well.

Steps to Convert Excel to Array in Java

  1. Using the Maven repository, add the reference to Aspose.Cells library in the application
  2. Add a reference to Workbook and Worksheet classes using Import statement in the program
  3. Load the source Workbook that is to be converted to an array
  4. Get a reference to the first sheet whose data is to be saved in an array
  5. Using the cells collection in sheet, export data to array from the desired range of cells
  6. Display dimensions of the output array for verification

In these steps, we load an Excel file and access one of its worksheets from where data is to be converted to an array. Each worksheet contains a collection of cells that provides a function to export data to an array from a particular range in the sheet. You can export data in single or two dimensions array.

Code to Read Values from Excel File and Store in Array in Java

In this sample code, we used Cells.exportArray() function that requires starting cell of the target range of cells along with the total rows and columns that are to be exported to an array. Here we just convert a range of cells to an array however, if you want to convert other types of data in the sheet say chart to image, you may refer to the article on how to convert Excel chart to JPG in Java. Here no other component or libraries like Interop or MS Excel is required for running the above code.

 English