import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook
-------------contorl代码
/**
* 批量导入保存
* @param request
* @param modelMap
* @param excelFile
* @return
* @throws Exception
*/
@RequestMapping(value = "/dept-import-save.do")
public String importSave(HttpServletRequest request,ModelMap modelMap,MultipartFile excelFile) throws Exception {
User login_user = (User) request.getSession().getAttribute(Config.LOGIN_INFO);
String hospital_id = (login_user.getHospital_id());
if(hospital_id==null || hospital_id.length()<1){
modelMap.addAttribute("message","导入失败,请使用医院用户导入");
return "message/success";
}
String filePath = request.getServletContext().getRealPath("")+ "/file/upload/";
//上传文件
File fileFile = UtilAPI.saveFile(excelFile, filePath);
Workbook rwb = Workbook.getWorkbook(fileFile);
Sheet sheet = rwb.getSheet(0);
// 获得有效行数
int rowNum = sheet.getRows();
int columnNum = sheet.getColumns();
if(rowNum<3){
modelMap.addAttribute("message","导入失败,模版无数据");
return "message/success";
}
if(!sheet.getCell(0,0).getContents().equals("科室导入模版")){
modelMap.addAttribute("message","导入失败,模版错误");
return "message/success";
}
List<Dept> deptList = new ArrayList<>();
for(int i=2;i<rowNum;i++){
Dept dept = new Dept();
//科室代码
String dept_code = sheet.getCell(0,i).getContents().toString().trim();
if(!UtilAPI.isNull(dept_code)){
dept.setDept_code(dept_code);
}else{
break;
}
//科室名称
String dept_name = sheet.getCell(1,i).getContents().toString().trim();
if(!UtilAPI.isNull(dept_name)){
dept.setDept_name(dept_name);
}else{
break;
}
//科室位置
String specific = sheet.getCell(2,i).getContents().toString().trim();
if(!UtilAPI.isNull(specific)){
dept.setDept_specific(specific);
}
//科室显示顺序
int order_no = UtilAPI.turnInt(sheet.getCell(3,i).getContents().toString().trim());
if(!UtilAPI.isNull(order_no)){
dept.setOrder_num(order_no);
}
//是否签到
int is_checked = UtilAPI.turnInt(sheet.getCell(4,i).getContents().toString().trim());
if(!UtilAPI.isNull(is_checked)){
dept.setIs_checked(is_checked);
}
//分诊模式
int triage_class = UtilAPI.turnInt(sheet.getCell(5,i).getContents().toString().trim());
if(!UtilAPI.isNull(triage_class)){
dept.setTriage_class(triage_class);
}
//科室描述
String dept_memo = sheet.getCell(6,i).getContents().toString().trim();
if(!UtilAPI.isNull(dept_memo)){
dept.setDept_memo(dept_memo);
}
dept.setHospital_id(hospital_id);
dept.setLast_modify_date(new Date());
dept.setLast_modify_user(login_user.getLast_modify_user());
deptList.add(dept);
}
if(!UtilAPI.isListNull(deptList)){
deptService.insertAll(deptList);
}
modelMap.addAttribute("message","导入成功");
return "message/success";
}
-------service层的代码
/**
*批量导入科室信息
* @param examItems
*/
public void insertAll(List<Dept> deptList){
if(!UtilAPI.isListNull(deptList)){
for(int i=0;i<deptList.size();i++){
Dept dept = this.findDeptByDeptCodeAndHospital(deptList.get(i).getDept_code(), deptList.get(i).getHospital_id());
if(UtilAPI.isNull(dept)){
this.insert(deptList.get(i));
}else{
Dept dept_new = deptList.get(i);
dept_new.setDept_id(dept.getDept_id());
this.update(dept_new);
}
}
}
}
-------工具类代码
package tools;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import org.springframework.web.multipart.MultipartFile;
public class UtilAPI {
/**
* 转换为int类型
*
* @param obj
* @return int 空返回0
*/
public static int turnInt(Object obj) {
int i = 0;
if (UtilAPI.isNull(obj)) {
return i;
}
try {
if (obj instanceof Double) {
Double d = (Double) obj;
return d.intValue();
}
if (obj instanceof Float) {
Float f = (Float) obj;
return f.intValue();
}
i = Integer.valueOf(obj.toString());
} catch (Exception e) {
// 类型转换异常
e.printStackTrace();
return 0;
}
return i;
}
/**
* 判断是否为null
*
* @param strData
* @return boolean
*/
public static boolean isNull(Object strData) {
if (strData == null || String.valueOf(strData).trim().equals("")) {
return true;
}
return false;
}
public static File saveFile(MultipartFile execlFile, String filePath) throws IOException {
InputStream is = execlFile.getInputStream();
File folderFile = new File(filePath);
if (!folderFile.exists()) {
folderFile.mkdirs();
}
File fileFile = new File(filePath + System.currentTimeMillis() + ".xls");
OutputStream os = new FileOutputStream(fileFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
return fileFile;
}
/**
* 判断List是否为null
* @param list
* @return
*/
public static boolean isListNull(List list) {
if (list == null || list.isEmpty() || list.size() == 0) {
return true;
}
return false;
}
/**
* Map转String
*
* @param map
* @param key
* @return
*/
public static String selectMapToString(Map<String, Object> map, String key) {
if (map == null && key != null) {
return null;
} else {
String upper = key.toUpperCase();
if (map.get(upper) != null && map.get(upper).toString().length() > 0) {
return map.get(upper).toString();
}
String lower = key.toLowerCase();
if (map.get(lower) != null && map.get(lower).toString().length() > 0) {
return map.get(lower).toString();
}
return null;
}
}
/**
* Map转Integer
*
* @param map
* @param key
* @return
*/
public static Integer selectMapToInt(Map<String, Object> map, String key) {
if (map == null && key != null) {
return null;
} else {
String upper = key.toUpperCase();
if (map.get(upper) != null && map.get(upper).toString().length() > 0) {
return Integer.valueOf(map.get(upper).toString());
}
String lower = key.toLowerCase();
if (map.get(lower) != null && map.get(lower).toString().length() > 0) {
return Integer.valueOf(map.get(lower).toString());
}
return null;
}
}
}