导入Excel文件

 <s:file  id="uploadFileId" theme="simple" cssStyle="width:250px;" name="uploadExcel"/>

public String importExcelDataList(){
		try{
			StringBuffer uniqueFileName = new StringBuffer(this.getUniqueValue());//得到文件名
			//得到文件名的后缀
			String fileSuffix = this.uploadExcelFileName.substring(this.uploadExcelFileName.lastIndexOf("."),this.uploadExcelFileName.length());
			if(uploadExcel!=null && !"".equals(uploadExcel)){//判断文件是否为空
				//判断文件是否是03版的excel还是07版的excel
				if(fileSuffix!=null && (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix))){
					//得到上传文件提具体路径
					File saveFile = new File(new File(this.getContextUploadPath()),this.getCurrentUser()+"_"+uniqueFileName.toString()+fileSuffix);
					if(!saveFile.getParentFile().exists()){//如果上传文件路径不为空
						saveFile.getParentFile().mkdirs();//得到文件文件路径,并创建文件夹
					}
					FileUtils.copyFile(uploadExcel, saveFile);//把得到的文件copy
//					service.importDataList(provinceList, saveFile.toString(), this.getCurrentUser());//导入省会
//					service.importCityList(cityList, saveFile.toString(), this.getCurrentUser());//导入市
//					service.importCountyList(countyList, saveFile.toString(), this.getCurrentUser());//导入区
					service.importTownList(saveFile.toString(), this.getCurrentUser());//导入街道
				}
			}
		}catch(Throwable e){
			logger.error(e);
			e.printStackTrace();
		}
		System.out.println("prefect...................................");
		return this.goImportPage();
	}

//获取文件的唯一名称
	public String getUniqueValue(){
		Random random = new Random();
		long currentTime = System.currentTimeMillis();
		StringBuffer stringBuffer = new StringBuffer(String.valueOf(currentTime));
		stringBuffer.append(random.nextInt(100));
		return stringBuffer.toString();
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章