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 */
7 package com.job5156.xlstodb;
8
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
16 /**
17 * @author Alpha
18 * JAVA 操作 excel 中的 .csv文件格式
19 */
20 public 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}
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
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
16 /**
17 * @author Alpha
18 * JAVA 操作 excel 中的 .csv文件格式
19 */
20 public 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}