1、RegionAction完成POI处理
@Controller
public class RegionAction {
+ @Autowired
+ private RegionService regionService;
+
//属性驱动接受上传的文件
private File regionFile;
public void setRegionFile(File regionFile) {
@@ -24,8 +40,39 @@ public void setRegionFile(File regionFile) {
* @Description: 导入表格
* @date: 2018年7月12日 下午1:31:31
* @return
+ * @throws IOException
+ * @throws FileNotFoundException
*/
- public String importXls(){
+ public String importXls() throws FileNotFoundException, IOException{
+ //创建集合存储region对象
+ List<Region> regionList = new ArrayList<Region>();
+
+ //使用POI解析Excel文件
+ HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(regionFile));
+ //根据名称获得第一个sheet
+ HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
+ //获得每行对象
+ for(Row row:sheetAt){
+ //标题行去掉
+ if(row.getRowNum() == 0){
+ continue;
+ }else{
+ //遍历行内的每个单元格
+ for(Cell cell:row){
+ 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);
+ regionList.add(region);
+ }
+ }
+ }
+ //批量保存,不要频繁地开事务
+ regionService.saveBatch(regionList);
+
return "toRegion";
}
2、RegionServiceImp
@Service
+public class RegionServiceImp implements RegionService {
+ @Autowired
+ private RegionDao regionDao;
+ /**
+ * 批量保存Region对象
+ */
+ @Override
+ public void saveBatch(List<Region> regionList) {
+ for(Region region:regionList){
+ regionDao.saveOrUpdate(region);
+ }
+
+ }
+
+}
3、Region.jsp页面相关(OCUpload的使用)
//导入数据表格
+ $("#button-import").upload({
+ action:'${pageContext.request.contextPath}/regionAction_importXls.action',
+ name:'regionFile'
+ });