jxl excel向数据库导入数据

第一步:

/**
* 得到导入的excel数据并进行判断,不允许有重复的身份证号
*
* @param filePath
* 上传的文件的路径
* @param tableName
* 导入数据库的表名
*/
public List excelImport(String filePath, String tableName,
String[] tableColumm) {
// 得到excel文件
File file = new File(filePath);
List list = new ArrayList();
try {
Workbook book = Workbook.getWorkbook(file);
// 得到工作簿中的第一个表索引即为excel下的sheet1,sheet2,sheet3...
Sheet sheet = book.getSheet(0);
list = excelSj(sheet, tableName, tableColumm);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}



第二步:

/**
* 导入excel表数据
*
* @param sheet
* 单元表
* @param tableName
* 导入的数据库表名
*/
public List excelSj(Sheet sheet, String tableName, String[] tableColumm) {
int rows = sheet.getRows();
List list = new ArrayList();
for (int i = 1; i < rows; i++) {
Cell[] cells = sheet.getRow(i);
if (cells.length == 0) {
continue;
} else {
// 转换excel中的数据
String[] strs = changeCells(cells, tableColumm);
// 验证excel里的内容(身份证号的位数和身份证号不允许重复)
Map map = validateCells(strs);
if (!map.isEmpty()) {
// 如果map为空则执行插入操作
addData(tableName, tableColumm, strs);
} else {
list.add(map);
}
}
}
return list;
}

第三步:

// 转换excel中的数据
public String[] changeCells(Cell[] cells, String[] tableColumm) {
int colummLength = tableColumm.length;
String[] strs = new String[colummLength];
for (int i = 0; i < cells.length; i++) {
// 看看cell为空时怎么显示的
String str = cells[i].getContents();
if (str == null || "''".equals(str)) {
strs[i] = null;
} else {
strs[i] = cells[i].getContents().trim();
}
}
return strs;
}

第四步:

// 验证excel里的内容(身份证号的位数和身份证号不允许重复)
public Map validateCells(String[] strs) {
Map map = new HashMap();
// 暂时这里写死了,实际长度为2位
if (strs[0] == null || "".equals(strs[0])) {
map.put(strs[0], strs[1]);
}
if (strs[0] != null && !"".equals(strs[0])) {
if (strs[1] == null || "".equals(strs[1])) {
map.put(strs[0], strs[1]);
} else {
// 验证身份证号是否存在
boolean flag = validateCid(strs[1]);
if (!flag) {
map.put(strs[0], strs[1]);
}
}
}
return map;
}
第五步

// 做插入数据的操作
// tableColumm 数据库中的属性 strs excel表中转换的数据
public boolean addData(String tableName, String[] tableColumm, String[] strs) {
boolean flag = false;
String sql = "";
String atrrSql = "";
String excelSql = "";
String cols = "";
String values = "";
if (strs.length > 0) {
for (int i = 0; i < tableColumm.length - 1; i++) {
atrrSql += tableColumm[i] + ",";
excelSql += "'"+strs[i] +"'"+ ",";
}
cols = atrrSql + tableColumm[tableColumm.length - 1];
values = excelSql + "'"+strs[strs.length - 1]+"'";
sql = " insert into " + tableName + "(" + cols + ") values ("
+ values + ")";
}
try {
flag = baseDao.insert(sql);
} catch (ToolException e) {
e.printStackTrace();
}
return flag;
}



导入的包


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.LabelCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import sdzn.util.page.PageData;
import dao.jdbc.databasedao.IBaseDao;
import dao.jdbc.databasedao.ToolException;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值