引入上传文件插件
Action中写法
public String importXls() throws FileNotFoundException, IOException{
String flage ="1";
try{
HSSFWorkbook workbook =
new HSSFWorkbook(new FileInputStream(myFile));
HSSFSheet sheet = workbook.getSheetAt(0);
List
for (Row row : sheet) {
if(row.getRowNum()==0){
continue;
}
//根据你的字段来
String id = row.getCell(0).getStringCellValue();
String province = row.getCell(1).getStringCellValue();
String city = row.getCell(2).getStringCellValue();
String district = row.getCell(3).getStringCellValue();
String postcode = row.getCell(4).getStringCellValue();
Region region = new Region
(id, province, city, district, postcode, null, null, null);
list.add(region);
}
regionService.saveBatch(list);//保存到数据库,这个简单的保存会写吧?
}catch(Exception e){
flage = "0";
}
ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
ServletActionContext.getResponse().getWriter().write(flage);
return NONE;
}
php 好几个phpexcel的 插件
比如
PhpExcel
你可以找一下相关的插件使用教程。
然后把excel按你自己的意思读取出来。
然后 一条一条插入到MySQL数据库里面即可