package homework.dao;
import homework.model.UserInfo;
import java.io.*;
import java.text.DateFormat;
import java.util.*;
import org.apache.poi.*;
import org.apache.poi.ss.usermodel.C
package homework.dao;
import homework.model.UserInfo;
import java.io.*;
import java.text.DateFormat;
import java.util.*;
import org.apache.poi.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/*
* 讀取Excel文件實現(xiàn)批量導(dǎo)入用戶信息
* JasonHu 03-20
*/
public class BatchImport {
// 定義EXCEL模板文件中列名位置信息
private final int iPosUserName = 0; // 用戶名
private final int iPosPwd = 1; //密碼
private final int iPosName = 2; // 真實姓名
private final int iPosSex = 3; // 性別
private final int iPosClass = 4; //班級
private final int iPosRole = 5; //用戶類別
private final int iPosPhone = 6; // 手機號碼
//用戶鏈表
List<UserInfo> list = new ArrayList<UserInfo> ();
public boolean debug=true;
public BatchImport() {
}
/*
* fileName 要讀取的文件名稱
* 讀取EXCEL文件
* jason 03-20
*/
public List getInstance(String fileName) {
List lst = this.readFile(fileName);
String errorMsg=this.getError();
if(!debug)
{
lst=null;
}
return lst;
}
/*
* 對獲取的Excel數(shù)據(jù)進行類型判斷
* JasonHu 03-20
*/
private String getValue(Cell cell) {
String ret = null;
if (cell == null)
return ret;
try {
switch (cell.getCellType()) {
//字符串
case Cell.CELL_TYPE_STRING:
ret = cell.getRichStringCellValue().getString();
break;
//日期
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
ret = cell.getDateCellValue().toLocaleString();
}
// 其他格式的數(shù)據(jù)
else {
double temp = cell.getNumericCellValue();
Double dt = new Double(temp);
ret = String.format("%d", dt.longValue());
}
break;
}
}
catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/*
* 錯誤代碼提示
* JasonHu 03-20
*/
public String getError() {
StringBuffer errorMsg = new StringBuffer("Error: \n");
for (UserInfo user : list) {
//判斷用戶姓名
if (user.getUserName() == null) {
errorMsg.append("\n 姓名 不可為空! \n");
debug=false;
}
else {
if (user.getUserName().length() > 32 || user.getUserName().length() < 1) {
errorMsg.append("姓名 長度應(yīng)在0-32位之間! \n");
debug=false;
}
}
//判斷用戶密碼
if (user.getPsw() == null) {
errorMsg.append("密碼 不可為空! \n");
debug=false;
}
else {
if (user.getPsw().length() < 6) {
errorMsg.append("密碼 長度應(yīng)大于6! \n");
debug=false;
}
}
//判斷性別
if (user.getSex() == null) {
errorMsg.append("性別 不可為空! \n");
debug=false;
}
else {
if (!user.getSex().trim().equals("男") && !user.getSex().trim().equals("女")) {
errorMsg.append("性別 錯誤!(男/女) \n");
debug=false;
}
}
//判斷班級
if (user.getClassName() == null) {
errorMsg.append("班級 不可為空! \n");
debug=false;
}
//判斷類別
if (user.getRole() == null) {
errorMsg.append("類別 不可為空! \n");
debug=false;
}else
{
if(!user.getRole().trim().equals("教師") && !user.getRole().trim().equals("學(xué)生"))
{
errorMsg.append("類別 錯誤! \n");
debug=false;
}
}
//判斷電話號碼
if (user.getPhone() != null) {
if ( (user.getPhone().length() != 7) && (user.getPhone().length() != 8) &&
(user.getPhone().length() != 11)) {
errorMsg.append("電話號碼 長度錯誤! \n");
debug=false;
}
}
}
return errorMsg.toString();
}
/*
* 讀取Excel文件
* JasonHu 03-20
*/
public List readFile(String fileName) {
try {
InputStream is = new FileInputStream(fileName);
Workbook workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(0);
// 遍歷
for (Row row : sheet) {
if (row.getRowNum() < 2) {
continue;
}
UserInfo user = new UserInfo();
Cell cell = row.getCell(iPosName);
if(cell==null)
{
break;
}
// 用戶名
user.setUserName(this.getValue(cell));
//密碼
cell = row.getCell(iPosPwd);
if(cell==null)
{
break;
}
user.setPsw(this.getValue(cell));
//真實姓名
cell = row.getCell(iPosName);
if(cell==null)
{
break;
}
user.setName(this.getValue(cell));
//性別
cell = row.getCell(iPosSex);
if(cell==null)
{
break;
}
user.setSex(this.getValue(cell));
//班級
cell = row.getCell(iPosClass);
if(cell==null)
{
break;
}
user.setClassName(this.getValue(cell));
//類別
cell = row.getCell(iPosRole);
if(cell==null)
{
break;
}
user.setRole(this.getValue(cell));
//電話
cell = row.getCell(iPosPhone);
if(cell==null)
{
break;
}
user.setPhone(this.getValue(cell));
//System.out.println(user.getUserName()+"\t"+user.getPsw()+"\t"+user.getName()+"\t"+user.getSex()+"\t"+user.getClassName()
// +"\t"+user.getRole()+"\t"+user.getPhone());
//添加用戶至List
list.add(user);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
if (list.size() == 0) {
list = null;
}
return list;
}
}
本篇文章來源于:開發(fā)學(xué)院
http://edu.codepub.com 原文鏈接:
http://edu.codepub.com/2009/1201/18349.php