1.jsp:
(h-ui)
<!--弹框-导入-->
<div
id="festival_modal"
class="modal fade"
tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true">
<form
action=""
method="post"
class="form form-horizontal"
id="form-festival-submit">
<!-- <div class="modal fade" id="festival_modal"
tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> -->
<div
class="modal-dialog"
role="document">
<div
class="modal-content radius">
<div
class="modal-header">
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4
class="modal-title"
id="myModalLabel">请选择Excel文件</h4>
</div>
<div
class="modal-body">
<a
href="###"
class="form-control"
style="border:none;">下载导入模板</a><br><br>
<span
class="btn-upload form-group">
<input
class="input-text upload-url radius"
type="text"
name="uploadfile-1"
id="uploadfile-1"
readonly><a
href="javascript:void();"
class="btn btn-primary radius"><i
class="Hui-iconfont"></i>
浏览文件</a>
<input
type="file"
name="uploadfile"
class="input-file"
id="uploadfile">
</span>
</div>
<div
class="modal-footer">
<button
class="btn btn-primary"
type="submit">确定</button>
<button
class="btn"
data-dismiss="modal"
aria-hidden="true">关闭</button>
</div>
</div>
</div>
<!-- </div> -->
</form>
</div>
2.js:
$("#form-festival-submit").validate({
onkeyup:false,
focusCleanup:true,
success:"valid",
submitHandler :
function(form){
$(form).ajaxSubmit({
url : bPath+'/festival/config/upload.hrm',
type :
'post',
dataType :
'text',
data : {
},
success :
function(data){
if(data ==
'SUCC'){
alert('导入成功!');
window.location.reload();
}else
if(data ==
"FAIL"){
alert('导入失败!')
}else
if(data ==
"diffrent_year"){
alert('日期存在错误,请核实后再导入!')
}else
if(data ==
"error_year"){
alert('日期有空值或者格式不正确!')
}
}
})
}
})
3.java:
@RequestMapping(value="upload",method=RequestMethod.POST)
@ResponseBody
public
String
upload(MultipartHttpServletRequest
request){
String
flag
= AppConstants.ADD_FAIL;
long
maxfileupload
= 1024*1024*60;
String
suffix[] =
new
String[]{
"xls","xlsx"};
List<MultipartFile>
files
=
request.getFiles("uploadfile");
List<BFestivalConfig>
list
=
new
ArrayList<BFestivalConfig>();
if(files
!=
null){
list
= beginUpload(request,maxfileupload,suffix,"uploadfile");
}
if(list
!=
null
&&
list.size() > 0){
try
{//确定导入的年份为同一年
String
year1
=
list.get(0).getConfigDate().substring(0, 4);
for
(BFestivalConfig
config
:
list) {
String
year
=
config.getConfigDate().substring(0, 4);
if(!(year1.equals(year))){
flag
=
"diffrent_year";
return
flag;
}
}
HQLEntityString
hes
=
new
HQLEntityString(BFestivalConfig.class.getName());
hes.setCustomHql("
configDate like '%"+year1+"%'
");
List<BFestivalConfig>
configList
=
configService.getQueryFestivalConfig(hes);
if(configList
!=
null
&&
configList.size() > 0){//防止重复导入,如果存在导入,则先删除
configService.deleteFestival(year1);
}
}
catch
(Exception
e) {
flag
=
"error_year";//防止年份输入为空或者格式不正确
//System.err.println("日期栏有空值或者格式不正确!");
e.printStackTrace();
return
flag;
}
flag
=
configService.batchInsert(list);//批量插入
}
return
flag;
}
private
List<BFestivalConfig> beginUpload(MultipartHttpServletRequest
request,
long
maxfileupload, String[]
suffix, String
param) {
//1.获取
String
serverPath
= PropertyManager.getString("upload_file_path");//获取配置中存放在磁盘的路径
if(StringUtils.isEmpty(serverPath)){
serverPath
=
request.getSession().getServletContext().getRealPath("");
}
File
uploadPath
=
null;
SimpleDateFormat
sdf
=
new
SimpleDateFormat("yyyy/MM/dd");//目录地址按照日期
String
date
=
sdf.format(System.currentTimeMillis());
String
s
=
serverPath
+ File.separatorChar;
uploadPath
=
new
File(s
+
date
+ File.separator);//上传文件目录
if(!uploadPath.exists()){
uploadPath.mkdirs();
}
System.out.println("uploadPath:"+uploadPath);
List<BFestivalConfig>
list
=
new
ArrayList<BFestivalConfig>();
try
{
List<MultipartFile>
files
=
request.getFiles(param);
for
(MultipartFile
multipartFile
:
files) {
//2.存到磁盘
CommonsMultipartFile
fileMetaData
= (CommonsMultipartFile)
multipartFile;
String
realName
=
fileMetaData.getOriginalFilename();//excel表的名字
if(realName
==
null
|| StringUtils.isEmpty(realName)){
return
null;
}
String
filesuffix
=
realName.substring(realName.lastIndexOf(".")
+ 1);//获取文件后缀xlsx
boolean
isValid
=
false;//判断文件支持的类型是否合法
for(int
i
= 0;
i
<
suffix.length;
i++){
if(filesuffix.trim().equalsIgnoreCase(suffix[i])){
isValid
=
true;
}
}
if(!isValid){
throw
new
Exception("不合法的文件类型!");
}
String
newSuffix
=
filesuffix.trim().toLowerCase();
realName
= System.currentTimeMillis() +
"."
+
newSuffix;
File
saveFile
=
new
File(uploadPath,
realName);
fileMetaData.getFileItem().write(saveFile);
//3.存到数据库
String
absolutePath
=
saveFile.getAbsolutePath();
System.out.println("absolutePath:"+absolutePath);
FileInputStream
fis
=
new
FileInputStream(absolutePath);
Workbook
workbook
= WorkbookFactory.create(fis);
Sheet
sheet;
sheet
=
workbook.getSheetAt(0);
int
rowNum
=
sheet.getLastRowNum();
Row
row1
=
sheet.getRow(1);
if(row1
==
null){
throw
new
Exception("错误:无效Excel,没有列名!");
}
for(int
rowIndex
= 1;rowIndex
<=
rowNum;
rowIndex++){
BFestivalConfig
config
=
new
BFestivalConfig();
Row
row
=
sheet.getRow(rowIndex);
if(row
==
null){
return
null;
}
/*if(!("".equals(row.getCell(1).getStringCellValue()))){
}*/
config.setConfigDate(row.getCell(0).getStringCellValue());
config.setIsWork(row.getCell(1).getStringCellValue());
config.setRemark(row.getCell(2).getStringCellValue());
//System.err.println(row.getCell(0).getStringCellValue());
list.add(config);
}
}
}
catch
(Exception
e) {
e.printStackTrace();
}
return
list;
}