標籤:
一、檔案非同步上傳
傳統表單提交檔案上傳會重新整理頁面,使用OCUpload外掛程式可以實現非同步上傳。
(1)頁面部分
(2)Action
@Controller@Scope("prototype")public class RegionAction extends BaseAction<Region>{@Autowiredprivate RegionService regionService;private File myFile;private String myFileFileName;public String importExcel() throws IOException{if(myFile != null){//是否是Excelif(myFileFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){String flag = "1";try{regionService.importExcel(myFile,myFileFileName);}catch(Exception e){e.printStackTrace();flag = "0";}ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");ServletActionContext.getResponse().getWriter().print(flag);}}return NONE;}public void setMyFile(File myFile){this.myFile = myFile;}public String getMyFileFileName(){return myFileFileName;}public void setMyFileFileName(String myFileFileName){this.myFileFileName = myFileFileName;}}
(3)POI讀取Excel
@Service@Transactionalpublic class RegionServiceImpl implements RegionService{@Autowiredprivate RegionDao regionDao;@Overridepublic void importExcel(File myFile,String fileName) throws Exception{List<Region> list = new ArrayList<Region>();boolean is03Excel = fileName.matches("^.+\\.(?i)(xls)$");FileInputStream inputStream = new FileInputStream(myFile);// 1、讀取活頁簿Workbook workbook = is03Excel ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);// 2、讀取第一個工作表Sheet sheet = workbook.getSheetAt(0);// 遍曆擷取每行for(Row row : sheet){// 擷取行號int num = row.getRowNum();// 第一行不儲存到資料庫if(num != 0){row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);// 擷取省市縣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);}}regionDao.saveBatch(list);}}
@Repositorypublic class RegionDaoImpl extends BaseDaoImpl<Region> implements RegionDao{@Overridepublic void saveBatch(List<Region> list){for(Region region : list){saveOrUpdate(region);}}}
業務作業系統(五)