Use like jxl.jar (JExcel)

 

JExcel API

Introduction:

  In most cases, the application needs to generate some reports. Generating these reports in excel can be a good approach as most people know how to use excel, besides that it provides the flexibility to share reports. In this short series of tutorials, we will see how to use the JExcel API to work with Excel files in Java.

read excel file

  In this tutorial, we will use the JExcel API to read Excel files in Java language. Here is the Excel file we will read:

  In this tutorial, make sure you download the latest version of jexcelapi and put it on your computer or eclipse project's classpath. Alternatively, you can download the eclipse project attached at the end of this page, which already includes the API.

  JavaExcelExport.zip

  test.xls

 

  Here is the Java source code of a program that reads an excel file and displays its contents:

 

public  static  void main(String[] args) {
         try {
             // Create a workbook object from the file at the specified location; change the file's path based on the location on the computer.            
            Workbook wrk1 = Workbook.getWorkbook( new File("C:/test.xls" )); 
// Get the reference of the first worksheet in the workbook Sheet sheet1 = wrk1.getSheet(0 );
// Use the worksheet's getCel(int col, int row) method gets a reference to the cell Cell colArow1 = sheet1.getCell(0, 0 ); Cell colBrow1 = sheet1.getCell(1, 0); Cell colArow2 = sheet1.getCell(0, 1 );
// Use the getContents() method of the cell reference to get the contents of the cell, the return value is a String str_colArow1 = colArow1.getContents(); String str_colBrow1 = colBrow1.getContents(); String str_colArow2 = colArow2.getContents(); // Display cell content System.out.println("Contents of cell Col A Row 1: \"" + str_colArow1 + "\"" ); System.out.println("Contents of cell Col B Row 1: \"" + str_colBrow1 + "\""); System.out.println("Contents of cell Col A Row 2: \"" + str_colArow2 + "\"");
}
catch (BiffException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } }

  1) The following line creates a Workbook object that encapsulates the Excel file. In this example, the getWorkbook() method accepts a File object pointing to the Excel file located at "C:/test.xls".

Workbook wrk1 =  Workbook.getWorkbook(new File("C:/test.xls"));

  2) The line below is used to get the first sheet in the Excel file. In a similar way, if the Excel file you're working on has multiple sheets, you can get any other sheet.

Sheet sheet1 = wrk1.getSheet(0);

  3) The code below gives us a reference to a specific cell in the worksheet based on the parameter passed to the getCell method. The first parameter represents the column and the second parameter represents the row. Make sure to only access non-empty cells, otherwise this method will throw an ArrayIndexOutOfBoundsException.

Cell colArow1 = sheet1.getCell(0, 0);

  4) The following line calls the getContents() method to extract the contents of the Cell. The content is returned as a string.

String str_colArow1 = colArow1.getContents();

  operation result:

 

Read Excel file with different data types

   As pointed out in the previous tutorial, in a real application you may have to deal with Excel file data of multiple data types other than String. In this tutorial, we will use different data types to read excel files in Java.

  We will try to read the excel below as it can be seen that excel has Date, Number, Boolean and Label (regular string) data in the first four cells.

 

  Here is the Java source code of a program that reads an excel file and displays its contents:

 public  static  void main(String[] args) {     
         // Create a workbook object from the file at the specified location; change the file's path based on the location on the computer. 
        Workbook wrk1;
         try {
            wrk1 = Workbook.getWorkbook(new File("C:/test.xls"));

            // Get the reference of the first sheet in the workbook 
            Sheet sheet1 = wrk1.getSheet(0 );

           // Use the worksheet's getCel(int col, int row) method to get a reference to the 
            cell Cell cell1 = sheet1.getCell(0, 0 );
            Cell cell2 = sheet1.getCell(1, 0);
            Cell cell3 = sheet1.getCell(2, 0);
            Cell cell4 = sheet1.getCell(3, 0);

            DateCell dCell = null;
            NumberCell nCell = null;
            BooleanCell bCell = null;
            LabelCell lCell = null;

            // Check the type of the cell content and convert the object to the appropriate reference type 
            if (cell1.getType() == CellType.DATE)
                dCell = (DateCell) cell1;

            if (cell2.getType() == CellType.NUMBER)
                nCell = (NumberCell) cell2;

            if (cell3.getType() == CellType.BOOLEAN)
                bCell = (BooleanCell) cell3;

            if (cell4.getType() == CellType.LABEL)
                lCell = (LabelCell) cell4;

            // Display cell content 
            System.out.println("Value of Date Cell is: " + dCell.getDate());
            System.out.println("Value of Number Cell is: " + nCell.getValue());
            System.out.println("Value of Boolean Cell is: " + bCell.getValue());
            System.out.println("Value of Label Cell is: " + lCell.getString());

        } catch (BiffException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }

    }

  1) The following line creates a Workbook object that encapsulates the Excel file. In this example, the getWorkbook() method accepts a File object pointing to the Excel file located at "C:/test.xls".

Workbook wrk1 =  Workbook.getWorkbook(new File("C:/test.xls"));

  2) The line below is used to get the first sheet in the Excel file. In a similar way, if the Excel file you're working on has multiple sheets, you can get any other sheet.

Sheet sheet1 = wrk1.getSheet(0);

  3) The code below gives us a reference to a specific cell in the worksheet based on the parameter passed to the getCell method. The first parameter represents the column and the second parameter represents the row. Make sure to only access non-empty cells, otherwise this method will throw an ArrayIndexOutOfBoundsException.

Cell cell1 = sheet1.getCell(0, 0);

  4) The following lines create DateCell (date type data), NumberCell (numeric type data), BooleanCell (Boolean type data),: LabelCell (string data)

DateCell dCell=null;
NumberCell nCell=null;
BooleanCell bCell=null;
LabelCell lCell=null;

  5) In the following line, we call the getType() method of the Cell object. This method returns an enumeration of the data type representing the contents of the Cell. and convert it to the corresponding type of Cell object.

if(cell1.getType() == CellType.DATE)
     dCell = (DateCell)cell1;     
if(cell2.getType() == CellType.NUMBER)
     nCell = (NumberCell)cell2;     
if(cell3.getType() == CellType.BOOLEAN)
     bCell = (BooleanCell)cell3;     
if(cell4.getType() == CellType.LABEL)
     lCell = (LabelCell)cell4;

  6) Finally we print the value of the cell. The getDate() method of the DateCell object returns the date content of the Cell. The getValue() methods of NumberCell and BooleanCell objects return the cell's numeric (double) and boolean content, respectively. The getString() method of the LabelCell object returns the String content on the Cell.

System.out.println("Value of Date Cell is: " + dCell.getDate());
System.out.println("Value of Number Cell is: " + nCell.getValue());
System.out.println("Value of Boolean Cell is: " + bCell.getValue());
System.out.println("Value of Label Cell is: " + lCell.getString());

  Output result:

 

  In conclusion, in this tutorial, we saw how to use jexcelapi to read Excel files containing different data types in Java. In the next tutorial in this series, we will see how to write to an excel file.

  Appendix:

  JavaExcelReadDataTypes.zip (659KB)

  test.xls (6.5KB)   

 

Write to Excel

  

 

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324907869&siteId=291194637