How to Read Excel file using Java

Normally, to read a data in excel, first we should have access to workbook, sheet which we want to read as workbook contains multiple sheets and if you want to read a particular cell we need location of a Cell.
  • In this article, we will discuss how to access workbook, sheet and a Cell using Jxl library Download jxl jar and add it to build path.
  • You can also consider usingApache Poi Library to perform read and write operations with excel sheets because of its better documentation, more features, active development, and Excel 2007+ format support.
  • As we know JXL doesn't support Excel 2007 ".xlsx" file format. It only supports the old BIFF (binary) ".xls" format. Where as Apache POI supports both Excel 2003 - xls and Excel 2007 - xlsx file formats.
  • To start with gaining access to Workbook, we should always remember the below command:
  • In order to write anything we need to first create a writable workbook as below which creates the workbook object.
  • Or You can also directly send the file as below
  • Now to get the access to the particular sheet, we should use the below command:
  • If you want to get the access to sheet2, you should specify as below:
  • You can also get the sheet access by sheet name, you should specify as below:
  • Now we will get the content in particular location, which will return contents as a string:
  • We can also write it as :
  • There is an other style to get the cell contents as below:
  • The below is the input sheet for the example program:
  • Please find the below code in which we will read a data from excel sheet and print using for loop
  • The output of the below program is: Username password
    testuser1 testpassword1
    testuser2 testpassword2
    testuser3 testpassword3
    testuser4 testpassword4