ADF 的 导入导出 和 别的框架的思想差不多。
唯一的区别就是在调用公共导入文件的时候传递到公共taskflow的 groovy语句 的语句使用方法。
我学到的做Excel导入模版的思想是:
一 数据库:
1.首先要在数据库中建立一个表。 用来存储会调用这个公共EXCEL导入方法的事务码和EXCEL需要导入的字段名,标签名,字段类型;如果自己想额外再加点什么比如时间戳也可以。如图:
2.建立一张用例对应存储过程映射信息表。即在导入后用存储过程进行校验导入的数据是否规范。如图:
3.建立一张用来存储导入数据的表。
二 JAVA中的操作:
1.在会调用公共方法的页面编写导入Excel 方法的actionEvent:
inputfile控件对象.setValue(null) ;//清空载入文件控件对象的值
清空文件上传对象(uploadfile对象)
弹出popUp方法:
RichPopup poPup =获取组件对象;
RichPopup.PopupHints ph = new RichPopup.PopupHints();
poPup.show(ph);
2.选择好要导入的Excel文件后,设置一个valueChangeListener ,如果新旧值不一样,则把新值赋给uploadfile对象,如果新值=空则则uploadfile对象=null
3.前两步搞定后进入导入之前的各种校验,文件名是不是xls,xlsx结尾啦,把要导入的Excel事先部署到中间件的工程文件夹下是否成功啦。在本用例的pageFlowScope域下,设置准备要传递的用例事务码,文件路径,查询起点啦。
上传文件到部署工程的代码如下:
if (!(new File(uploadPath).exists())) {
(new File(uploadPath)).mkdirs(); //创建上传文件夹
}
File targetFile = new File(uploadPath, uploadFile.getFilename());
targetFile.delete(); // 如果存在同名文件,将旧文件删除
if (uploadFile != null && uploadFile.getLength() > 0) {
try {
try {
InputStream inputStream = uploadFile.getInputStream(); //文件输入流
OutputStream outputStream = new FileOutputStream(targetFile); //文件输出流
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
int size = 8192; //读写缓冲区大小
byte buffer[] = new byte[size];
int bytesRead = 0;
while ((bytesRead = bufferedInputStream.read(buffer, 0, size)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead); //每次读写8192字节
}
bufferedOutputStream.flush();
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
uploadFile.dispose();
} catch (IOException e) {
throw new QMLMPException("捕获异常:文件上传失败");
}
} catch (QMLMPException e) {
QMMessage.addError(e.getMessage(), "");
return -1; //上传失败
}
} else {
QMMessage.addError("文件上传失败", "");
return -1; //上传失败
}
return 1; //上传成功
而ADF获取部署目录的方法:
public static String getFilePath() {
FacesContext fctx = FacesContext.getCurrentInstance();
String realPath = ((HttpServletRequest)fctx.getExternalContext().getRequest()).getRealPath("/");
return realPath;
}
主调用的用例要在taskFlow设置Parameters传递ManageBean的用例事务码,文件路径,查询起点。
.
这样就可以把三个参数传递给 公共方法的ManageBean
String activeType = (String)JSFUtils.resolveExpression("#{pageFlowScope.vtranscode}");
String filePath = (String)JSFUtils.resolveExpression("#{pageFlowScope.filePath}");
Integer startNum = (Integer)JSFUtils.resolveExpression("#{pageFlowScope.startNum}");
我这里没用临时表,用的是固定表,首先要清除表中上次存储的Excel数据,上文中的表3。
读取表1中存储的该用例对应的字段信息并存放到一个数据结构中。(我自己定义了一个类作为模版存到Arraylist中)