How to Export List Data to Excel in Java

This quick tutorial will teach you how to export list data to Excel in Java. First, a list of strings is required that can be either filled with a set of constant strings or you may fill it from some external source like reading data from some file on disk, a database, or some data stream over the network. Ultimately create a new workbook and call importArrayList function to convert list to Excel in Java which will be saved as an XLSX file on disk.

Steps to Export List Data to Excel in Java

  1. Add a reference to Aspose.Cells from the Maven Repository to export the list to Excel
  2. Declare an array list to hold the list data for exporting to Excel
  3. Fill the list with strings using constant strings or fill from some external source
  4. Create a Workbook class object which will be filled with the list data
  5. Get the reference to a Worksheet from the newly created workbook
  6. Call the importArrayList function along with list reference, destination cell, and text direction
  7. Save the output workbook containing the exported list of strings

Here first, initialize a list of strings that can be filled in a variety of ways. Then a workbook is created and a reference to a particular sheet is obtained from this workbook where you will write list to Excel in Java. Finally, you can save the output Excel file to any of the desired formats like XLSX, XLS, XLSM or XLSB.

Code to Create Excel From List in Java

This code uses the ArrayList class to declare and initialize the list of strings however, you can create a list of other types of data as well. Similarly, the first worksheet reference is obtained to write the list however you can refer to other sheets also by providing the zero-based index or the name of the sheet. The last argument in the importArrayList() function defines the direction of text i.e. either the list will be inserted in a column-wise manner or in a row.

This tutorial guides you to export a list to Excel however if you want to write Excel to an array, refer to the article on how to convert Excel to array in Java. Note that this conversion of the list to Excel does not require MS Excel or Interop to be available on the system.

 English