public String uploadExcel(HttpServletRequest request) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request
System.out.println("通过传统方式form表单提交方式导入excel文件!")
InputStream in =null
List<List<Object>>listob = null
MultipartFile file = multipartRequest.getFile("upfile")
if(file.isEmpty()){
throw new Exception("文件不存在!")
}
in = file.getInputStream()
listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename())
in.close()
//该处可调用service相应方法进行数据保存到数据库中,现只对数据输出
for (int i = 0i <listob.size()i++) {
List<Object>lo = listob.get(i)
System.out.println(lo.get(0))
System.out.println(lo.get(1))
Word word = new Word()
word.setId(UUIDTools.getUUID())
word.setChinese(lo.get(1).toString())
word.setEnglish(lo.get(0).toString())
wordService.saveEnglishWord(word)
/* InfoVo vo = new InfoVo()
vo.setCode(String.valueOf(lo.get(0)))
vo.setName(String.valueOf(lo.get(1)))
vo.setDate(String.valueOf(lo.get(2)))
vo.setMoney(String.valueOf(lo.get(3)))
System.out.println("打印信息-->机构:"+vo.getCode()+" 名称:"+vo.getName()+" 时间:"+vo.getDate()+" 资产:"+vo.getMoney()) */
}
return "result"
}
ImportExcelUtil类的getBankListByExcel方法:
public List<List<Object>>getBankListByExcel(InputStream in,String fileName) throws Exception{
List<List<Object>>list = null
//创建excel工作簿
Workbook work = this.getWorkbook(in,fileName)
Sheet sheet = null
Row row = null
Cell cell = null
list = new ArrayList<List<Object>>()
//遍历Excel中所有的sheet
for (int i = 0i <work.getNumberOfSheets()i++) {
sheet = work.getSheetAt(i)
if(sheet==null){continue}
//遍历当前sheet中的所有行
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(this.getCellValue(cell))
}
list.add(li)
}
}
// work.close()
return list
}
前台弄个上传标签 访问这个action就行了。
//从excle文档中,将值导入至list数组//xlsPath 路径 从前台获取
//Excle导入
public List<TblUser> loadScoreInfo(String xlsPath) throws IOException{
List temp = new ArrayList()
FileInputStream fileIn = new FileInputStream(xlsPath)
//根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook wb0 = new HSSFWorkbook(fileIn)
//获取Excel文档中的第一个表单
Sheet sht0 = wb0.getSheetAt(0)
//对Sheet中的每一行进行迭代
for (Row r : sht0) {
//如果当前行的行号(从0开始)未达到2(第三行)则从新循环
if(r.getRowNum()<1){
continue
}
//创建实体类
TblUser info=new TblUser()
//取出当前行第1个单元格数据,并封装在info实体stuName属性上
if(r.getCell(0)!=null){
r.getCell(0).setCellType(Cell.CELL_TYPE_STRING)
info.setId(Integer.parseInt(r.getCell(0).getStringCellValue()))
}
//同上
if(r.getCell(1)!=null){
r.getCell(1).setCellType(Cell.CELL_TYPE_STRING)
info.setUsername(r.getCell(1).getStringCellValue())
}
if(r.getCell(2)!=null){
r.getCell(2).setCellType(Cell.CELL_TYPE_STRING)
info.setPassword(r.getCell(2).getStringCellValue())
}
if(r.getCell(3)!=null){
r.getCell(3).setCellType(Cell.CELL_TYPE_STRING)
info.setState(r.getCell(3).getStringCellValue())
}
if(r.getCell(4)!=null){
r.getCell(4).setCellType(Cell.CELL_TYPE_STRING)
info.setRename(r.getCell(4).getStringCellValue())
}
if(r.getCell(5)!=null){
r.getCell(5).setCellType(Cell.CELL_TYPE_STRING)
info.setEmail(r.getCell(5).getStringCellValue())
}
temp.add(info)
}
fileIn.close()
return temp
}
public void ztree()
{
}
//导出当前页的数据
public void exportPage(){
List<TblUser> list=new ArrayList<TblUser>()
String[] slist=daor.split(",")
//将页面获取到的id集合遍历,循环添加到list中,进行本页数据到导出
for (int i = 0 i < slist.length i++) {
list.add(tus.findById(java.lang.Integer.parseInt(slist[i])))
}
tus.export(list)
}
/**
* 导入Excle到数据库
* @return null 不然有可能报错!
* @throws IOException
*/
public void importExcle() throws IOException{
//调用导入文件方法并存入数组中
int s=0//得到成功插入的条数
int i=0//得到共有多少条
/**
* 将不符合格式的数据错误信息存入数组中!
* 格式要求:
* 用户名,密码不能为空!
* 用户名不能和已存在的用户名重复,长度在5-18位之间
* 密码长度在6-18位之间
*/
String errors=""//保存导入失败信息
try {
System.out.println("进入方法!")
lists=this.loadScoreInfo(path)
System.out.println(path)
for (TblUser u : lists) {
i++
if(u.getUsername()==""){
errors+="第"+i+"条数据的用户名为空,导入失败!"//^^:分割符
continue
}
if(u.getPassword()==""){
errors+="第"+i+"条数据的密码为空,导入失败!"//^^:分割符
continue
}
if(tus.findByName(u.getUsername())){
errors+="第"+i+"条数据的用户名 已存在,导入失败!"//^^:分割符
continue
}
if(u.getUsername().length()<5 || u.getUsername().length()>20){
errors+="第"+i+"条数据的用户名格式错误,导入失败!"//^^:分割符
continue
}
if(u.getPassword().length()<6 || u.getPassword().length()>20){
errors+="第"+i+"条数据的密码格式错误,导入失败!"//^^:分割符
continue
}
s++
tus.save(u)//将数组中的数据添加到数据库中!
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}finally{
System.out.println("错误:"+errors)
}
}
excel有行和列,可以对应数据库表的行和字段。先获取你excel中的数据,如果你的数据是和java中实体对应的话,循环获取每一行数据存放进实体对象中,然后进行数据库保存就好了。读取excel数据可以使用poi。