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>>()
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就行了。
BufferedReader inputtry {
String s = new String()
input = new BufferedReader(new FileReader("f:\\123.txt"))
while ((s = input.readLine()) != null) { // 判断是否读到了最后一行
String info[] = s.split(" ")
System.out.println( info[0] + " " + info[1] + " " + info[2] )
}
input.close()
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
把info[0] + " " + info[1] + " " + info[2] 这三个值放在insert语句里就行了 经过测试
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)