1. 程式人生 > >Java程式碼:用jxl實現excel的讀取和寫入

Java程式碼:用jxl實現excel的讀取和寫入

建立的實體類

package com.jxl;


public class Users {
private Integer id;
private String name;
private String pwd;
public Users() {
super();
// TODO Auto-generated constructor stub
}
public Users(Integer id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}

}

寫入讀取的核心程式碼

package com.jxl;


import java.io.File;
import java.util.ArrayList;
import java.util.List;


import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class XlsxTest {


public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "C://book.xls";
// 要寫入的資料


List<Users> list = new ArrayList<Users>();


Users us1 = new Users(1, "張三", "123");
Users us2 = new Users(2, "李四", "456");
Users us3 = new Users(3, "趙武", "789");


list.add(us1);
list.add(us2);
list.add(us3);
//新增資料
addExcel(path, list);


//讀取資料
getAllByExcel(path);


}


/**
* 寫入excel表格

* @param path
* @param list
*/
public static void addExcel(String path, List<Users> list) {
try {
WritableWorkbook wb = null;


// 建立可寫入的Excel工作簿


File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
// 以fileName為檔名來建立一個Workbook
wb = Workbook.createWorkbook(file);


// 建立工作表
WritableSheet ws = wb.createSheet("Test Shee 1", 0);


// 要插入到的Excel表格的行號,預設從0開始
Label labelId = new Label(0, 0, "編號");// 表示第
Label labelName = new Label(1, 0, "姓名");
Label labelPwd = new Label(2, 0, "密碼");


ws.addCell(labelId);
ws.addCell(labelName);
ws.addCell(labelPwd);


for (int i = 0; i < list.size(); i++) {


Label labelId_i = new Label(0, i + 1, list.get(i).getId() + "");
Label labelName_i = new Label(1, i + 1, list.get(i).getName());
Label labelSex_i = new Label(2, i + 1, list.get(i).getPwd());


ws.addCell(labelId_i);
ws.addCell(labelName_i);
ws.addCell(labelSex_i);


}


// 寫進文件
wb.write();
// 關閉Excel工作簿物件
wb.close();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


/**
* 讀取資料

* @param file
*            檔案路徑
* @return
*/
public static List<Users> getAllByExcel(String file) {
List<Users> list = new ArrayList<Users>();
try {
Workbook wb = Workbook.getWorkbook(new File(file));
Sheet rs = wb.getSheet("Test Shee 1");// 或者rwb.getSheet(0)
int clos = rs.getColumns();// 得到所有的列
int rows = rs.getRows();// 得到所有的行


System.out.println(clos + " rows:" + rows);
for (int i = 1; i < rows; i++) {
for (int j = 0; j < clos; j++) {
// 第一個引數列數,第二個引數是行數
String id = rs.getCell(j++, i).getContents();// 預設最左邊編號也算一列
// 所以這裡j++
String name = rs.getCell(j++, i).getContents();
String pwd = rs.getCell(j++, i).getContents();


System.out.println("id:" + id + " name:" + name + " pwd:"
+ pwd);
list.add(new Users(Integer.parseInt(id), name, pwd));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;


}


}