To read column data from an Excel file in Java, you can use the Apache POI library. Make sure you have the library added to your project's dependencies.
Here's an example code snippet that demonstrates how to extract column data from an Excel file:
import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ExcelColumnReader { public static void main(String[] args) { try { // Load the Excel file FileInputStream file = new FileInputStream(new File("path_to_your_excel_file.xlsx")); // Create an instance of the workbook Workbook workbook = WorkbookFactory.create(file); // Get the first sheet from the workbook Sheet sheet = workbook.getSheetAt(0); // Specify the column index you want to read (0-based index) int columnIndex = 0; // Iterate over each row in the column and retrieve the cell value for (Row row : sheet) { Cell cell = row.getCell(columnIndex); if (cell != null) { // Get the cell value and print it String cellValue = cell.getStringCellValue(); System.out.println(cellValue); } } // Close the workbook and file streams workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } }
Replace "path_to_your_excel_file.xlsx" with the actual file path of your Excel file. Also, make sure to handle exceptions appropriately in your application.
Remember to include the Apache POI library in your project's dependencies. You can download it from the Apache POI website (https://poi.apache.org/).