在糖尿病患者信息管理系统中,导入病人信息功能!form表单提交数据(Excel文件),在后台得不到file文件,解决方法:
private File filePath; //文件
private String fileName; //文件名
private String fileType; //文件类型
注:上面filePath必须有,否则下面得到filePath为空!
/**
* 导入病人信息,并且插入用户信息
*/
public String importPatient() throws Exception {
if (filePath != null) {
FileInputStream fis = new FileInputStream(filePath);
Workbook readWb = Workbook.getWorkbook(fis);
// 得到sheet
Sheet readSheet = readWb.getSheet(0);
// 得到多少列
int rsColumns = readSheet.getColumns();
// 得到多少行
int rsRows = readSheet.getRows();
// 单元格
Cell cell;
// 实例化病人对象
Patient patient = new Patient();
SysUser sysUser = new SysUser();
Map<Integer, String> map = new HashMap<Integer, String>();
for (int i = 1; i < rsRows; i++) {
for (int j = 0; j < rsColumns; j++) {
cell = readSheet.getCell(j, i);
map.put(j, cell.getContents());
}
UUID uuid = UUID.randomUUID();
String patientId = uuid.toString();
patient.setId(patientId);
//用户信息
sysUser.setId(uuid.toString());
sysUser.setUserName(map.get(5));
sysUser.setPassword(md5keyBean.getkeyBeanofStr("123456"));
sysUser.setNickName(map.get(1));
sysUser.setPhone(map.get(5));
//1:默认不锁定;0:锁定
sysUser.setStatus("1");
//角色id
List<SysRole> sysRoles = sysRoleService.findAllSysRoles();
if(sysRoles.size()>0) {
for (int j = 0; j < sysRoles.size(); j++) {
if("病人".equals(sysRoles.get(j).getName())) {
sysUser.setSysRole(sysRoles.get(j));
}
}
}
//病人id
sysUser.setFlagId(patientId);
sysUser.setCreateTime(new Date());
sysUserService.saveSysUser(sysUser);
patient.setNumber(map.get(0));
patient.setRealName(map.get(1));
patient.setAge(Integer.parseInt(map.get(2)));
// 性别:1男0女
if ("男".equals(map.get(3))) {
patient.setSex("1");
} else if ("女".equals(map.get(3))) {
patient.setSex("0");
} else {
patient.setSex("1");
}
patient.setNation(map.get(4));
patient.setPhone(map.get(5));
// 婚况:1:已婚 0:未婚
if ("已婚".equals(map.get(6))) {
patient.setStateMarriage("1");
} else if ("未婚".equals(map.get(6))) {
patient.setStateMarriage("0");
} else {
patient.setStateMarriage("1");
}
patient.setBirthAdress(map.get(7));
patient.setAddress(map.get(8));
patient.setOrganize(map.get(9));
patient.setNumberId(map.get(10));
// 患病类型
String diseaseType = map.get(11);
List<DiseaseType> diseaseTypes = diseaseTypeService
.findAllDiseaseTypes();
if (diseaseTypes.size() > 0) {
for (int j = 0; j < diseaseTypes.size(); j++) {
if (diseaseType.equals(diseaseTypes.get(j)
.getDiseaseType())) {
patient.setDiseaseTypeId(diseaseTypes.get(j)
.getId());
}
}
}
patientService.savePatient(patient);
}
}
return "listAction";
}
jsp代码:
<!-- 批量导入病人信息 -->
<form class="form-horizontal" method="post" action="${pageContext.request.contextPath}/sys/patientAction_importPatient.do" id="fileUpload" name="fileUpload" enctype="multipart/form-data">
<style type="text/css">
.file {
position: relative;
display: inline-block;
background: #78CD51;
border: 1px solid #99D3F5;
border-radius: 4px;
padding: 4px 12px;
color: #FFFFFF;
text-decoration: none;
text-indent: 0;
line-height: 25px;
}
.file input {
position: absolute;
width:120px;
right: 0;
top: 0;
opacity: 0;
}
.file:hover {
background: #78CD51;
border-color: #78C3F3;
color: #FFFFFF;
text-decoration: none;
}
</style>
<script type="text/javascript" language="javascript">
function submitFile() {
document.getElementById("fileUpload").submit();
}
</script>
<a href="javascript:;" class="file">批量导入病人信息
<input type="file" name="filePath" id="excelFile" onChange="submitFile()">
</a>
</form>
FileInputStream是InputStream的子类,不能直接转换,不过FileInputStream可当做InputStream来使用。
该批量导入和幼儿园管理系统导入不太一样!