使用Windows操作系统的朋友对Excel(电子表格)一定不会陌生,但是要使用Java语言来操纵Excel文件并不是一件容易的事。在Web应用日益盛行的今天,通过Web来操作Excel文件的需求越来越强烈,目前较为流行的操作是在 JSP或Servlet 中创建一个CSV (comma separated values)文件,并将这个文件以MIME,text/csv类型返回给浏览器,接着浏览器调用Excel并且显示CSV文件。这样只是说可以访问到Excel文件,但是还不能真正的操纵Excel 文件,本文将给大家一个惊喜,向大家介绍一个开放源码项目 Java Excel API,使用它大家就可以方便地操纵Excel文件了。
总结了两种主要文件格式操作实例
一:java 对.csv 文件格式的操作
1![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
2
* Created on 2007-11-21
3
*
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
6
*/
7
package com.job5156.xlstodb;
8![](/Images/OutliningIndicators/None.gif)
9
import java.io.BufferedReader;
10
import java.io.FileReader;
11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15![](/Images/OutliningIndicators/None.gif)
16![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
17
* @author Alpha
18
* JAVA 操作 excel 中的 .csv文件格式
19
*/
20![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class CsvUtil
{
21![](/Images/OutliningIndicators/InBlock.gif)
22
private String filename = null;
23
24
private BufferedReader bufferedreader = null;
25
26
private List list =new ArrayList();
27
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public CsvUtil()
{
29
30
}
31
32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) throws IOException
{
33
CsvUtil test = new CsvUtil();
34
test.run("D:/alpha/abc.csv");
35
}
36
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public CsvUtil(String filename) throws IOException
{
38
this.filename = filename;
39
bufferedreader = new BufferedReader(new FileReader(filename));
40
String stemp;
41![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while((stemp = bufferedreader.readLine()) != null)
{
42
list.add(stemp);
43
}
44
}
45
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public List getList() throws IOException
{
47
return list;
48
}
49
50![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int getRowNum()
{
51
return list.size();
52
}
53
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int getColNum()
{
55![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!list.toString().equals("[]"))
{
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(list.get(0).toString().contains(","))
{
57
return list.get(0).toString().split(",").length;
58![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(list.get(0).toString().trim().length() != 0)
{
59
return 1;
60![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
61
return 0;
62
}
63![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
64
return 0;
65
}
66
}
67
68![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getRow(int index)
{
69
if (this.list.size() != 0)
70
return (String) list.get(index);
71
else
72
return null;
73
}
74
75![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getCol(int index)
{
76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this.getColNum() == 0)
{
77
return null;
78
}
79
StringBuffer scol = new StringBuffer();
80
String temp = null;
81
int colnum = this.getColNum();
82![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (colnum > 1)
{
83![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (Iterator it = list.iterator(); it.hasNext();)
{
84
temp = it.next().toString();
85
scol = scol.append(temp.split(",")[index] + ",");
86
}
87![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
88![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (Iterator it = list.iterator(); it.hasNext();)
{
89
temp = it.next().toString();
90
scol = scol.append(temp + ",");
91
}
92
}
93
String str=new String(scol.toString());
94
str = str.substring(0, str.length() - 1);
95
return str;
96
}
97
98![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getString(int row, int col)
{
99
String temp = null;
100
int colnum = this.getColNum();
101![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(colnum > 1)
{
102
temp = list.get(row).toString().split(",")[col];
103![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(colnum == 1)
{
104
temp = list.get(row).toString();
105![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
106
temp = null;
107
}
108
return temp;
109
}
110
111![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void CsvClose() throws IOException
{
112
this.bufferedreader.close();
113
}
114
115![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void run(String filename) throws IOException
{
116
CsvUtil cu = new CsvUtil(filename);
117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* List tt = cu.getList();
118
for (Iterator itt = tt.iterator(); itt.hasNext();){
119
System.out.println("==="+itt.next().toString());
120
}*/
121![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0;i<cu.getRowNum();i++)
{
122
123
String name = cu.getString(i,0);//得到第i行.第一列的数据.
124
String email = cu.getString(i,1);;//得到第i行.第二列的数据.
125
String tel = cu.getString(i,2);;
126
String number = cu.getString(i,3);;
127
128
System.out.println("===name:"+name);
129
System.out.println("===email:"+email);
130
System.out.println("===tel:"+tel);
131
System.out.println("===number:"+number);
132
System.out.println(" ");
133
}
134
135![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*System.out.println("aaa:"+cu.getRowNum());
136
System.out.println("bbb:"+cu.getColNum());
137
System.out.println("ccc:"+cu.getRow(0));
138
System.out.println("ddd:"+cu.getCol(0));
139
System.out.println("eee:"+cu.getString(0, 0));*/
140
141
cu.CsvClose();
142
}
143![](/Images/OutliningIndicators/InBlock.gif)
144
}
145![](/Images/OutliningIndicators/None.gif)
二、java 对.xls 文件格式的操作
1![](/Images/OutliningIndicators/None.gif)
2
package com.job5156.xlstodb;
3![](/Images/OutliningIndicators/None.gif)
4
import java.io.FileInputStream;
5
import java.io.InputStream;
6![](/Images/OutliningIndicators/None.gif)
7
import jxl.Cell;
8
import jxl.Workbook;
9![](/Images/OutliningIndicators/None.gif)
10![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
11
* @author Alpha
12
* JAVA 操作 excel 中的 .xls文件格式
13
*/
14
public class ExcelUtil
15![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
16
public static void main(String[] args)
17![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
ExcelUtil eu = new ExcelUtil();
19
eu.run("D:/alpha/ab.xls");
20
}
21
22
private void run(String filename)
23![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
try
25![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
ex.printStackTrace();
53
}
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{
55
}
56
}
57
}
58![](/Images/OutliningIndicators/None.gif)
posted on 2007-11-21 10:29
Alpha 阅读(6444)
评论(2) 编辑 收藏 所属分类:
Java J2EE JSP