二、java 对.xls 文件格式的操作
1
2
package
com.job5156.xlstodb;
3
4
import
java.io.FileInputStream;
5
import
java.io.InputStream;
6
7
import
jxl.Cell;
8
import
jxl.Workbook;
9
10
/**
11
* @author Alpha
12
* JAVA 操作 excel 中的 .xls文件格式
13
*/
14
public
class
ExcelUtil
15
{
16
public static void main(String[] args)
17
{
18
ExcelUtil eu = new ExcelUtil();
19
eu.run( " D:/alpha/ab.xls " );
20
}
21
22
private void run(String filename)
23
{
24
try
25
{
26
InputStream is = new FileInputStream(filename);
27
jxl.Workbook rwb = Workbook.getWorkbook(is);
28
// 获得总 Sheets
29
// Sheet[] sheets = rwb.getSheets();
30
// int sheetLen = sheets.length;
31
// 获得单个Sheets 含有的行数
32
jxl.Sheet rs = rwb.getSheet( 0 ); // 读取第一个工作表的数据
33
// Cell[] cell_domain = rs.getColumn(0); // 读取第一列的值
34
int num = rs.getRows(); // 得到此excel有多少行..
35
for ( int i = 0 ;i < num;i ++ )
36
{
37
Cell[] cell = rs.getRow(i); // 得到第i行的数据..返回cell数组
38
String name = cell[ 0 ].getContents(); // 得到第i行.第一列的数据.
39
String email = cell[ 1 ].getContents(); // 得到第i行.第二列的数据.
40
String tel = cell[ 2 ].getContents();
41
String number = cell[ 3 ].getContents();
42
43
System.out.println( " ===name: " + name);
44
System.out.println( " ===email: " + email);
45
System.out.println( " ===tel: " + tel);
46
System.out.println( " ===number: " + number);
47
System.out.println( " " );
48
}
49
}
50
catch (Exception ex)
51
{
52
ex.printStackTrace();
53
}
54
finally {
55
}
56
}
57
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
33
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
34
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
35
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
36
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
37
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
38
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
39
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
40
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
41
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
42
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
43
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
44
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
45
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
46
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
47
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
48
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
49
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
50
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
51
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
52
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
53
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
54
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
55
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
56
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
57
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
58
File f=new File("D:/MMC/TestReadXls.xls");
/*
try{
myUrl = new URL("http://202.123.189.11/import/155.xls");
}
catch (Exception e) {
out.println(new String(e.getMessage()));
}
HttpURLConnection http = (HttpURLConnection) myUrl.openConnection();
http.connect();
myUrlStream = http.getInputStream();
*/
try{
myUrl = new URL("http://202.123.189.11/import/155.xls");
}
catch (Exception e) {
out.println(new String(e.getMessage()));
}
HttpURLConnection http = (HttpURLConnection) myUrl.openConnection();
http.connect();
myUrlStream = http.getInputStream();
*/
try {
//FileInputStream fis=new FileInputStream(f);
BufferedReader br=new BufferedReader(new FileReader(f));
//Workbook w=Workbook.getWorkbook(myUrlStream);
Workbook w=Workbook.getWorkbook(f);
Sheet sh=w.getSheet("Sheet1");
int columns=sh.getColumns();//获取总共有几列
int rows=sh.getRows();//获取该表中总共有几行
System.out.println(columns+"---"+rows);
//循环行列单元格
for (int i = 0; i < rows; i++) {
//for(int j=0;j<columns;j++){
//获取单元格需要注意的是它的两个参数,第一个是列数,第二个是行数,这与通常的行、列组合有些不同。
Cell cell=sh.getCell(0, i);
//单元格值
String cellValue=cell.getContents();
System.out.print(cellValue+" ");
//}
System.out.println();
}
//或者
for (int ii = 0; ii < rows; ii++) {
//返回该行中所有列给cell数组
Cell[] c=sh.getRow(ii);
for (int jj = 0; jj < c.length; jj++) {
//获取单元格值
String value=c[jj].getContents();
System.out.print(value+"---- ");
}
System.out.println();
}
//关闭
w.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}catch (BiffException exx) {
// TODO Auto-generated catch block
exx.printStackTrace();
} catch (IOException exxx) {
// TODO Auto-generated catch block
exxx.printStackTrace();
}
}
//FileInputStream fis=new FileInputStream(f);
BufferedReader br=new BufferedReader(new FileReader(f));
//Workbook w=Workbook.getWorkbook(myUrlStream);
Workbook w=Workbook.getWorkbook(f);
Sheet sh=w.getSheet("Sheet1");
int columns=sh.getColumns();//获取总共有几列
int rows=sh.getRows();//获取该表中总共有几行
System.out.println(columns+"---"+rows);
//循环行列单元格
for (int i = 0; i < rows; i++) {
//for(int j=0;j<columns;j++){
//获取单元格需要注意的是它的两个参数,第一个是列数,第二个是行数,这与通常的行、列组合有些不同。
Cell cell=sh.getCell(0, i);
//单元格值
String cellValue=cell.getContents();
System.out.print(cellValue+" ");
//}
System.out.println();
}
//或者
for (int ii = 0; ii < rows; ii++) {
//返回该行中所有列给cell数组
Cell[] c=sh.getRow(ii);
for (int jj = 0; jj < c.length; jj++) {
//获取单元格值
String value=c[jj].getContents();
System.out.print(value+"---- ");
}
System.out.println();
}
//关闭
w.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}catch (BiffException exx) {
// TODO Auto-generated catch block
exx.printStackTrace();
} catch (IOException exxx) {
// TODO Auto-generated catch block
exxx.printStackTrace();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)