//一:jsp页面 :表单
<form id="form1" action="<%=path%>/custInfo/readReport"method="post" enctype="multipart/form-data">
上传文件:<input id="fileExl" class="inputbox" type="file" name="file" size="200" /> <input type="submit" class="btn" value="上传" name="btnSave"onClick="return limitAttach()" />
</form>
//后台 代码
Controller 中:
// 读取Excel中的数据,插入数据库
@RequestMapping(value = "/readReport", method = RequestMethod.POST)
public ModelAndView getReadRepost(@RequestParam MultipartFile file,
HttpServletRequest request, HttpServletResponse response,
Model model) {
Custinfo custinfo = new Custinfo();
try {
List<Custinfo> list = this.custinfoService.readReport(file
.getInputStream());
this.custinfoService.insertCustinfo(list);
request.setAttribute("resultMessage0", "导入数据成功!");
} catch (IOException e) {
request.setAttribute("resultMessage0", "导入数据失败!可能是Excel表格数据不正确");
} catch (Exception ex) {
request.setAttribute("resultMessage0", "导入数据失败!可能是Excel表格数据不正确");
}
return new ModelAndView("custInfo/addCustInfo", "custinfo", custinfo);
}
Service 中:
/**
* 读取报表
*/
public List<Custinfo> readReport(InputStream inp) {
List<Custinfo> custList = new ArrayList<Custinfo>();
try {
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);// 取得第一个sheets
// 从第 yi行开始读取数据
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Custinfo cust = new Custinfo();
Custinfo custAdd = new Custinfo();
Row row = sheet.getRow(i);
if (row == null) {
// row 为空,不处理
continue;
}
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);// 获得单元格(cell)对象
// 转换接收的单元格
String cellStr = null;
cellStr = ConvertCellStr(cell, cellStr);
// 将一个单元格的数据添加至一个对象
custAdd = addingCust(j, cust, cellStr);
}
custList.add(custAdd);
}
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inp != null) {
try {
inp.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有数据流!");
}
}
return custList;
}
/**
* 把单元格内的类型转换至String类型
*/
private String ConvertCellStr(Cell cell, String cellStr) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
// 读取String
cellStr = cell.getStringCellValue().toString();
break;
case Cell.CELL_TYPE_BOOLEAN:
// 得到Boolean对象的方法
cellStr = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
// 先看是否是日期格式
if (DateUtil.isCellDateFormatted(cell)) {
// 读取日期格式
// cellStr = cell.getDateCellValue().toString();
long year = cell.getDateCellValue().getYear();
long mon = cell.getDateCellValue().getMonth()+1;
long day = cell.getDateCellValue().getDate();
long hour = cell.getDateCellValue().getHours();
long min = cell.getDateCellValue().getMinutes();
int s = cell.getDateCellValue().getSeconds();
year = 1900 + year;
cellStr = year + "-" + mon + "-" + day + " " + hour + ":" + min
+ ":" + s;
} else {
// 读取数字
cellStr = String.valueOf(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA:
// 读取公式
cellStr = cell.getCellFormula().toString();
break;
}
return cellStr;
}
/**
* 读取报表的数据后批量插入
*/
public void insertCustinfo(List<Custinfo> list) {
this.custinfoDao.saveAll(list);
}
/**
* 获得单元格的数据添加至Custinfo
*
* @param j
* 列数
* @param Custinfo
* 添加对象
* @param cellStr
* 单元格数据
* @return
*/
private Custinfo addingCust(int j, Custinfo cust, String cellStr) {
switch (j) {
case 0:
if (cellStr != null && !cellStr.equals("")) {
cust.setCustname(cellStr);
}
break;
case 1:
if (cellStr != null && !cellStr.equals("")) {
cust.setIdentitycard(cellStr);
}
break;
case 2:
if (cellStr != null && !cellStr.equals("")) {
cust.setRprno(cellStr);
}
break;
case 3:
if (cellStr != null && !cellStr.equals("")) {
cust.setBankacc(cellStr);
}
break;
case 4:
if (cellStr != null && !cellStr.equals("")) {
cust.setBankname(cellStr);
}
break;
case 5:
if (cellStr != null && !cellStr.equals("")) {
cust.setAddress(cellStr);
}
break;
case 6:
if (cellStr != null && !cellStr.equals("")) {
cust.setAggregate(BigDecimal.valueOf(Double
.parseDouble(cellStr)));
}
break;
case 7:
if (cellStr != null && !cellStr.equals("")) {
cust.setIntegall(BigDecimal.valueOf(Double.parseDouble(cellStr)));
}
break;
case 8:
if (cellStr != null && !cellStr.equals("")) {
cust.setFoodsubsidy(BigDecimal.valueOf(Double
.parseDouble(cellStr)));
}
break;
case 9:
if (cellStr != null && !cellStr.equals("")) {
SimpleDateFormat df1 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = df1.parse(cellStr);
Timestamp ts = new Timestamp(date.getTime());
cust.setCashtme(ts);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
return cust;
}
注:读取excel 数据添加到数据库中,和excel 的格式有关!!!
|