Home >Java >javaTutorial >How to read excel file data in java
Java programs can read Excel file data in two ways: using the Apache POI framework: importing the required libraries. Instantiate the Workbook object. Traverse worksheets and cells. Using JXL libraries: Import the required libraries. Instantiate the Workbook object. Traverse worksheets and cells.
Java reads Excel file data
How to read Excel file data?
Java programs can read Excel file data through the Apache POI framework and JXL library.
Read Excel data using Apache POI
import org.apache.poi.ss.usermodel.*
Workbook workbook = WorkbookFactory.create(new FileInputStream("excel_file.xlsx"));
for (Sheet sheet : workbook) { ... }
String cellValue = sheet.getRow(rowNumber).getCell(columnNumber).toString();
Read Excel data using JXL
import jxl.*
Workbook workbook = Workbook.getWorkbook(new FileInputStream("excel_file.xls"));
for (Sheet sheet : workbook .getSheets()) { ... }
String cellValue = sheet.getCell(columnNumber, rowNumber).getContents();
Sample code (using Apache POI)
<code class="java">import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import java.io.FileInputStream; import java.io.IOException; public class ReadExcel { public static void main(String[] args) throws IOException { Workbook workbook = WorkbookFactory.create(new FileInputStream("excel_file.xlsx")); for (Sheet sheet : workbook) { for (Row row : sheet) { for (Cell cell : row) { System.out.println(cell.toString()); } } } } }</code>
The above is the detailed content of How to read excel file data in java. For more information, please follow other related articles on the PHP Chinese website!