使用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/**//*
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 */
7package com.job5156.xlstodb;
8
9import java.io.BufferedReader;
10import java.io.FileReader;
11import java.io.IOException;
12import java.util.ArrayList;
13import java.util.Iterator;
14import java.util.List;
15
16/** *//**
17 * @author Alpha
18 * JAVA 操作 excel 中的 .csv文件格式
19 */
20public class CsvUtil {
21
22 private String filename = null;
23
24 private BufferedReader bufferedreader = null;
25
26 private List list =new ArrayList();
27
28 public CsvUtil() {
29
30 }
31
32 public static void main(String[] args) throws IOException {
33 CsvUtil test = new CsvUtil();
34 test.run("D:/alpha/abc.csv");
35 }
36
37 public CsvUtil(String filename) throws IOException{
38 this.filename = filename;
39 bufferedreader = new BufferedReader(new FileReader(filename));
40 String stemp;
41 while((stemp = bufferedreader.readLine()) != null){
42 list.add(stemp);
43 }
44 }
45
46 public List getList() throws IOException {
47 return list;
48 }
49
50 public int getRowNum(){
51 return list.size();
52 }
53
54 public int getColNum(){
55 if(!list.toString().equals("[]")) {
56 if(list.get(0).toString().contains(",")) {
57 return list.get(0).toString().split(",").length;
58 }else if(list.get(0).toString().trim().length() != 0) {
59 return 1;
60 }else{
61 return 0;
62 }
63 }else{
64 return 0;
65 }
66 }
67
68 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 public String getCol(int index){
76 if (this.getColNum() == 0){
77 return null;
78 }
79 StringBuffer scol = new StringBuffer();
80 String temp = null;
81 int colnum = this.getColNum();
82 if (colnum > 1){
83 for (Iterator it = list.iterator(); it.hasNext();) {
84 temp = it.next().toString();
85 scol = scol.append(temp.split(",")[index] + ",");
86 }
87 }else{
88 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 public String getString(int row, int col) {
99 String temp = null;
100 int colnum = this.getColNum();
101 if(colnum > 1){
102 temp = list.get(row).toString().split(",")[col];
103 }else if(colnum == 1) {
104 temp = list.get(row).toString();
105 }else{
106 temp = null;
107 }
108 return temp;
109 }
110
111 public void CsvClose() throws IOException {
112 this.bufferedreader.close();
113 }
114
115 public void run(String filename) throws IOException {
116 CsvUtil cu = new CsvUtil(filename);
117 /**//* List tt = cu.getList();
118 for (Iterator itt = tt.iterator(); itt.hasNext();){
119 System.out.println("==="+itt.next().toString());
120 }*/
121 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 /**//*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
144}
145
二、java 对.xls 文件格式的操作
1
2package com.job5156.xlstodb;
3
4import java.io.FileInputStream;
5import java.io.InputStream;
6
7import jxl.Cell;
8import jxl.Workbook;
9
10/** *//**
11 * @author Alpha
12 * JAVA 操作 excel 中的 .xls文件格式
13 */
14public 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}
58
posted on 2007-11-21 10:29
Alpha 阅读(6439)
评论(2) 编辑 收藏 所属分类:
Java J2EE JSP