jsp页面:
<text id="theFile" type="file" name="theFile"
cssStyle="height:30px" />
<button id="expBtn" key="导入" onClick="fnSaveFile()" />
js方法:
//导入用例按钮
function fnSaveFile() {
Base.submitForm('form1', null, false, 'testAction!impExcel.do');
}
//接受上传的文件,和前台组件的id一致
private File theFile;
//文件的名字
private String theFileFileName;
//文件的类型
private String theFileContentType;
public File getTheFile() {
return theFile;
}
public void setTheFile(File theFile) {
this.theFile = theFile;
}
public String getTheFileFileName() {
return theFileFileName;
}
public void setTheFileFileName(String theFileFileName) {
this.theFileFileName = theFileFileName;
}
public String getTheFileContentType() {
return theFileContentType;
}
public void setTheFileContentType(String theFileContentType) {
this.theFileContentType = theFileContentType;
}
/**
*
* excel文件导入,并显示到前台表格中
* @return
* @throws Exception
*/
public String impExcel() throws Exception {
ParamDTO dto = getDto();
if("".equals(dto.get("atm002"))) {
setMsg("请先选择功能!");
}else{
List list = null;
if (theFile != null){
try{
//获得上传的文件流
InputStream fin = new FileInputStream(theFile.getPath());
//判断文件类型,调用相应的工具类的方法
if (getTheFileFileName().indexOf(".xls") > 0) {
//获得excel文件中的数据
list=ExcelFileUtils.getExcelInputStream2ObjectList(fin, "导入表格中的字段", "实体类的路径", true);
}else {
setMsg("文件格式只能为.xls!");
return "success";
}
//将获得的数据设置到前台表格dgUC
setList("dgUC", list);
setData("atm001",dto.get("atm001"));
setMsg("文件导入成功!");
//删除零时文件
theFile.delete();
//关闭流
fin.close();
}catch(Exception e){
setMsg(e.getMessage());
}
}else {
setMsg("请先上传文件!");
}
}
setData("atm001",dto.get("atm001"));
setData("OneAtm001",dto.get("atm001"));
setActiveTab("tb_usecase");
setEnable("saveBtn");
return "success";
}