<input type="button" value="導(dǎo)出Excel" id="exportexcel">
<input type="button" value="導(dǎo)入Excel" id="importexcel">
$("#exportexcel").click(function(){
location="person/exportExcel.do";
})
$("#importexcel").click(function(){
if($("#myfile").val()==""){
alert("請(qǐng)先選擇要導(dǎo)入的Excel文件!!!");
}else{
excel.submit();
}
})
})
private List<Dept> listDept = new ArrayList<Dept>();
private List<Person> listPerson = new ArrayList<Person>();
@Autowired
private DeptServiceImpl deptdb;
@Autowired
private PersonServiceImpl perdb;
private File myfile;//名必須為myfile ,同時(shí)有set get方法
private String myfileFileName;//名必須 為:myfile+FileName有set get方法
// 有set get方法,開(kāi)發(fā)struts多選框的回顯
@RequestMapping("exportExcel")
public String exportExcel( HttpServletResponse response) throws Exception{
// 初始化HttpServletResponse對(duì)象
// 定義表的標(biāo)題
String title = "開(kāi)發(fā)部員工信息";
//定義表的列名
String[] rowsName = new String[] { "用戶(hù)編號(hào)", "姓名", "性別", "年齡", "入職時(shí)間"
, "照片", "技能","學(xué)歷","簡(jiǎn)歷","部門(mén)"};
//定義表的內(nèi)容
List<Object[]> dataList = new ArrayList<Object[]>();
Object[] objs = null;
List<Person> listPerson =perdb.list();
for (int i = 0; i < listPerson.size(); i++) {
Person per = listPerson.get(i);
objs = new Object[rowsName.length];
objs[0] = per.getId();
objs[1] = per.getName();
objs[2] = per.getSex();
objs[3] = per.getAge();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(per.getJobtime());
objs[4] = date;
objs[5] = per.getFilepath();
objs[6] = per.getSkill();
objs[7] = per.getDegree();
objs[8] = per.getResume();
objs[9] = per.getDept().getDname();
dataList.add(objs);
}
// 創(chuàng)建ExportExcel對(duì)象
ExportExcel ex = new ExportExcel(title, rowsName, dataList);
// 輸出Excel文件
try {
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition",
"attachment; filename=PersonList.xls");//保存人excel文件名
response.setContentType("application/msexcel");
ex.export(output);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
@RequestMapping("importExcel")
public String importExcel(MultipartFile file,HttpServletRequest request) throws Exception{
String realPath = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();//取文件名
//解決同名問(wèn)題
//fileName = UUID.randomUUID().toString().replace("-", "")+fileName.substring(fileName.lastIndexOf("."));
File f1=new File(realPath,fileName);
if(!f1.exists()){
f1.mkdirs();//如果不存在則創(chuàng)建其目錄
}
// 上傳文件到服務(wù)器中
file.transferTo(f1);
Person user = new Person();// 新建一個(gè)user對(duì)象
Dept dept = new Dept();// 新建一個(gè)dept對(duì)象
// 獲取服務(wù)器中文件的路徑
String path = request.getSession().getServletContext().getRealPath("")
+ "/upload/" + fileName;
try {
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
// 循環(huán)工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循環(huán)行Row
for (int rowNum = 3; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
// 循環(huán)列Cell
// 姓名 性別 年齡 入職時(shí)間 照片 技能 學(xué)歷 簡(jiǎn)歷 部門(mén)
user.setName(getValue(hssfRow.getCell(1)));
user.setSex(getValue(hssfRow.getCell(2)));
user.setAge(Integer.parseInt(getValue(hssfRow.getCell(3))));
//處量時(shí)間
String da=getValue(hssfRow.getCell(4));
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
user.setJobtime(sd.parse(da));//把字符轉(zhuǎn)為日期
user.setFilepath(getValue(hssfRow.getCell(5)));
user.setSkill(getValue(hssfRow.getCell(6)));
user.setDegree(getValue(hssfRow.getCell(7)));
user.setResume(getValue(hssfRow.getCell(8)));
//這里很重要,通過(guò)部門(mén)列表然后與excel中的部門(mén)字段進(jìn)行對(duì)比,匹配后獲取對(duì)應(yīng)的did
String dname = getValue(hssfRow.getCell(9));//獲取excel中的部門(mén)字段
listDept = deptdb.listDept();//得到數(shù)據(jù)庫(kù)中的部門(mén)列表
for (Dept dd : listDept) {//增強(qiáng)for循環(huán)
if (dd.getDname().equals(dname)) {//如果兩者匹配
dept.setDid(dd.getDid());//則得到對(duì)應(yīng)的did粟焊,并設(shè)置dept對(duì)象的did
user.setDept(dept);//再把dept對(duì)象設(shè)置到user對(duì)象中
}
}
perdb.addPerson(user);//寫(xiě)入到數(shù)據(jù)中
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "redirect:list.do";//返回列表展示
}