Search code examples
asposeaspose-cells

How to copy a Excel column to a Java list


I would like to extract the entire column from an excel worksheet to a Java array using the Aspose library. How do I do that? In many of the examples, I see how to copy a column/row from one worksheet to another only. Can Aspose extract data?


Solution

  • Yes, data can be easily exported to an Array using the exportArray method, see the sample code for your reference:

    Example

    // Instantiating a Workbook object
    Workbook workbook = new Workbook("Book1.xlsx");
    
    // Accessing the first worksheet in the Excel file
    Worksheet worksheet = workbook.getWorksheets().get(0);
    
    // Exporting the contents of 100 rows and 1 column starting from 1st cell (i.e., A1:A100)
    // to Array. you may update the method parameters for your needs
    Object dataTable[][] = worksheet.getCells().exportArray(0, 0, 100, 1);
    
    // Printing number of rows after exporting worksheet data
    System.out.println("No. Of Rows Imported: " + dataTable.length);
    
    .....
    

    PS. I am working as Support developer/ Evangelist at Aspose.