excel 上传读写到数据库

《HTML》

<div class="input-group">
<form id="abc" action="http://hbj.51jimihua.com/api/excel/excleToMysql" method="post" enctype="multipart/form-data">
<input width="20px;" hidden="10px;" name="file" value="选择文件" type="file" />
<input class="search-input form-control btn btn-default" type="submit" value="确认"/>
</form>
</div>

<java>

@RequestMapping(value = "/excleToMysql", method = RequestMethod.POST)
public ModelAndView excleToMysql(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request)
throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream());
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
List list = new ArrayList();

for (int i = 1, j = sheet.getLastRowNum(); i <= j; i++) {
HSSFCell c_name = sheet.getRow(i).getCell(0);
String name="无名氏";
String score="未知";
if (null != c_name){
name = c_name.toString();
}
HSSFCell c_score = sheet.getRow(i).getCell(1);
if (null != c_score){
c_score.setCellType(Cell.CELL_TYPE_STRING);
score = c_score.getStringCellValue();
}
WhiteList whiteList = new WhiteList();
whiteList.setName(name);
whiteList.setPhone(score);
list.add(whiteList);
}
whiteListService.insertWhite(list);

return null;
}

posted on 2018-11-06 14:12  寂寞一沙洲  阅读(173)  评论(0编辑  收藏  举报

导航