近段要实现一个上传excel文件到服务器,并把excel文件的数据导入到数据库表中的功能。
excel文件有两种格式,对应数据库两张表。我先实现了其中一种excel格式式的上传和导入数据。上传的功能是一样的,没什么好说的。不同的是两种excel文件格式不一样,对应的数据库表的字段也不一样。怎么办呢?
开始的时候,我先做了一个excel文件的功能。做另一个很相似了,运用c/p大法就搞定了:)。
代码是这样的.
action中:
| CityDayService cds = new CityDayServiceImpl(); ImpExcelData impExcelData = new ImpExcelData(filePath, 0); //这里从excel文件中取出数据 try { List list = impExcelData.getData(); //放进list中.用的是jxl List<CityDay> sameList = cds.sameList(list);//这里有不同 //boolean isOk = cds.checkExcel(list); cds.add(list);//这里有不同 |
另一种格式的excel文件对应的action是这样的.
/* 导入表 */
| ZhbTCityDayForecastService cds = new ZhbTCityDayForecastServiceImpl();///////////////// ImpExcelData impExcelData = new ImpExcelData(filePath, 0); try { List list = impExcelData.getData(); List<ZhbTCityDayForecast> sameList = cds.sameList(list);//////////////////// if (sameList.size()>0) { request.setAttribute("sameList", sameList); request .setAttribute("echo", "数据有重复,是否覆盖"); return mapping.findForward("upload"); } else { cds.add(list);/////////////////////////////// |

