Java Data Import feature read Excel file instance _java

Source: Internet
Author: User

The process of using a table (report) frequently in programming is mainly based on Excel tables. Here's how to read Excel tables in Java:

1. Add Jar file

Java Import Export Excel file to introduce the Jxl.jar package, the most critical is that the API is pure Java, does not rely on the Windows system, even if running under Linux, it can also correctly handle Excel files. Download Address: http://www.andykhan.com/jexcelapi/

 2.JXL Understanding of Excel tables

(1) The position of each cell is assumed to be given by a two-dimensional coordinate (i,j), where I represents the column, J represents the row, and increments from top to bottom, incrementing from left to right.

(2) The coordinates of the leftmost cell in the merged cell. The following figure T.xls, Class list (0,0), Chen Yin (1,2), Chen Kaixian (1,6).

3.java code reads to T.xls

Import Java.io.File;
Import jxl.*; 
public class read_excel{public
  static void Main (string[] args) {
    int i;
    Sheet Sheet;
    Workbook book;
    Cell Cell1,cell2,cell3;
    try { 
      //t.xls for the Excel filename to read
      book= workbook.getworkbook (New file ("T.xls")); 
      
      Gets the first worksheet object (the number sheet in Ecxel starts at 0, 0,1,2,3,....)
      Sheet=book.getsheet (0); 
      Gets the cell
      Cell1=sheet.getcell (0,0) in the upper-left corner;
      System.out.println ("title:" +cell1.getcontents ()); 
      
      I=1;
      while (true)
      {
        //Get cell 
        Cell1=sheet.getcell (0,i) for each row;//(columns, rows)
        Cell2=sheet.getcell (1,i);
        Cell3=sheet.getcell (2,i);
        if ("". Equals (Cell1.getcontents ()) ==true)  //If the data read is an empty break
          ;
        System.out.println (cell1.getcontents () + "T" +cell2.getcontents () + "T" +cell3.getcontents ()); 
        i++;
      }
      Book.close (); 
    }
    catch (Exception e) {} 
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.