業務作業系統(五)

來源:互聯網
上載者:User

標籤:

一、檔案非同步上傳

     傳統表單提交檔案上傳會重新整理頁面,使用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);}}}

業務作業系統(五)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.