Extjs上传导入数据(三)

36 篇文章 1 订阅

	/**
	 * 上传文件
	 * 
	 * @return
	 * @throws Exception
	 */
	public String uploadSensitiveWords() throws Exception {
		String suffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length());
		if (suffix.equals(".xls") || suffix.equals(".xlsx") || suffix.equals(".Xlsx")) {
			String savePath = createTemporaryFile();
			String realPath = savePath + "/" + fileName;
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(docmentFile));
			BufferedOutputStream bos = null;
			try {
				bos = new BufferedOutputStream(new FileOutputStream(realPath));
				byte[] buff = new byte[8192];
				for (int len = -1; (len = bis.read(buff)) != -1;) {
					bos.write(buff, 0, len);
				}
				bos.flush();
			} catch (IOException ie) {
				ie.printStackTrace();
			} finally {
				if (bis != null)
					bis.close();
				if (bos != null)
					bos.close();
			}
			ReadDemo(savePath);
		}
		return "none";
	}

	/**
	 * 读取文件
	 * 
	 * @return
	 * @throws Exception
	 */
	public void ReadDemo(String savePath) throws Exception {
		InputStream inputStream = new FileInputStream(savePath+"/" + fileName);
		Workbook workbook = WorkbookFactory.create(inputStream);
		int sheetCount = workbook.getNumberOfSheets();
		for (int i = 0; i < sheetCount; i++) {
			Sheet sheet = workbook.getSheetAt(i);
			int rows = sheet.getLastRowNum() + 1;
			Row tmp = sheet.getRow(0);
			if (tmp == null) {
				continue;
			}
			for (int row = 1; row < rows; row++) {
				Row r = sheet.getRow(row);
		        Domain domain = new Domain();
				if (r.getCell(0) != null) {
					r.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
					 
                    domain.setId(Integer.valueOf(r.getCell(0).getStringCellValue()));
				} else {
					domain.setSensitiveId(null);
				}
				
				dao.insert(domain);
			}

		}
		deleteTemporaryFile();
	}
	/**
	 * 创建临时文件
	 */
	public String createTemporaryFile() {
		String savePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
		File file = new File(savePath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdir();
		}
		return savePath;

	}

	/**
	 * 删除临时文件
	 */
	public void deleteTemporaryFile() {
		String savePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
		File fileDelete = new File(savePath);
		for (File fileDeletes : fileDelete.listFiles()) {
			fileDeletes.delete();
		}
		fileDelete.delete();
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值