如何把Excel导入mysql数据表的方法详解

2025-03-20 15:58:13
推荐回答(2个)
回答1:

引入上传文件插件
src="${pageContext.request.contextPath }/js/jquery.ocupload-1.1.2.js"
type="text/javascript">


Action中写法
public String importXls() throws FileNotFoundException, IOException{
String flage ="1";
try{
HSSFWorkbook workbook =
new HSSFWorkbook(new FileInputStream(myFile));
HSSFSheet sheet = workbook.getSheetAt(0);
List list = new ArrayList();
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;
}

回答2:

php 好几个phpexcel的 插件
比如
PhpExcel

你可以找一下相关的插件使用教程。

然后把excel按你自己的意思读取出来。
然后 一条一条插入到MySQL数据库里面即可