**
ssm框架的导入功能
**
大神勿喷,但请多多提提意见,我只是一个小白!
思路如下:
前端:
a.需要有选择文件的插件窗口
b.绑定点击事件
在绑定事件中先是第一层的校验 获取文件的的后缀名称是不是自己规定的文件格式
C.同过ajax的请求将获取通过的文件发送到控制层
后端:
a.控制层调用service的导入方法,将获取的文件传入service层中做处理
b.在service层中将文件里的数据转换为输入流,在通过excel工具包里面的各种循环拿到所有页数中每行数据,将拿到的数据在进行格式的转换,再将这些数据存入创建好的组装对象里面(List<List>),最终通过for循环将拿到的数据错入到数据库中.
前端代码如下:
后端代码如下:
@Override
public String importList(MultipartFile excelFile) {
if (excelFile.isEmpty()) {
try {
throw new Exception(“文件不存在!”);
} catch (Exception e) {
e.printStackTrace();
}
}
InputStream in = null;
try {
in = excelFile.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
List<List<Object>> lists = null;
try {
lists = new ExcelUtils().getBankListByExcel(in, excelFile.getOriginalFilename());
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < lists.size(); i++) {
List<Object> lo = lists.get(i);
Post post = new Post();
Post j = null;
Integer userId = Integer.parseInt(String.valueOf(lo.get(0)));
post.setUserId(userId);
String title = String.valueOf(lo.get(1));
post.setTitle(title);
int introduce = Integer.parseInt(String.valueOf(lo.get(2)));
post.setIntroduce(introduce);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String postDateStar = String.valueOf(lo.get(3));
post.setPostDateStar(postDateStar);
String postDateEnd = String.valueOf(lo.get(4));
post.setPostDateEnd(postDateEnd);
int postState = Integer.parseInt(String.valueOf(lo.get(5)));
post.setPostState(postState);
Date date = new Date();
String value = simpleDateFormat.format(date);
post.setCreateTime(value);
post.setUpdateTime(value);
postDao.insert(post);
}
return "1";
}
以下是工具类
public class ExcelUtils {
private final static String excel2003L = ".xls";
private final static String excel2007U = ".xlsx";
public List<List<Object>> getBankListByExcel(InputStream in, String fileName) throws Exception {
List<List<Object>> list = null;
Workbook work = this.getWorkbook(in, fileName);
if (work == null) {
throw new Exception("创建Excel工作簿为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
list = new ArrayList<List<Object>>();
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if (sheet == null) {
continue;
}
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if (row == null || row.getFirstCellNum() == j) {
continue;
}
List<Object> li = new ArrayList<Object>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
//li.add(cell);
li.add(this.getValue(cell));
}
list.add(li);
}
}
return list;
}
public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (excel2003L.equals(fileType)) {
wb = new HSSFWorkbook(inStr);
} else if (excel2007U.equals(fileType)) {
wb = new HSSFWorkbook(inStr);
} else {
throw new Exception("解析的文件格式有误!");
}
return wb;
}
public String getValue(Cell cell) {
String value = "";
if (null == cell) {
return value;
}
switch (cell.getCellType()) {
//数值型
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
//如果是date类型则 ,获取该cell的date值
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = format.format(date);
} else {// 纯数字
BigDecimal big = new BigDecimal(cell.getNumericCellValue());
value = big.toString();
//解决1234.0 去掉后面的.0
if (null != value && !"".equals(value.trim())) {
String[] item = value.split("[.]");
value = item[0];
}
}
break;
//字符串类型
case Cell.CELL_TYPE_STRING:
value = cell.getStringCellValue().toString();
break;
// 公式类型
case Cell.CELL_TYPE_FORMULA:
//读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
value = cell.getStringCellValue().toString();
}
break;
// 布尔类型
case Cell.CELL_TYPE_BOOLEAN:
value = " " + cell.getBooleanCellValue();
break;
default:
value = cell.getStringCellValue().toString();
}
if ("null".endsWith(value.trim())) {
value = "";
}
return value;
}
}
}