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) {}
}
}