grails 批量导入excel文件
defserverimport={
render(view:"server_import")
}
defserver_import={defloginUser=UtilsController.getLoginUser(session)//取得登录人
if(loginUser){
defufile=FileUploadUtil.upload(request,"batchfile")//上传文件。写好的java类直接引用
defout=0
defj=0
deffailDevice=''
if(ufile.state){
try{
defwb=Workbook.getWorkbook(ufile.file)
defsheet=wb.getSheet(0)
defrows=sheet.getRows()
for(inti=1;i<rows;i++){
defserverfaults=newServerFaults()
defcell=sheet.getRow(i)
if(cell[0].getContents()!=null&&cell[0].getContents()!=""){
defreport1=cell[0].getContents()
defreport2=U.findByUserRealName(report1)//把传入的姓名字符串转换为自己需要的类型
serverfaults.reporter=report2
serverfaults.type=cell[1].getContents()
serverfaults.describe=cell[2].getContents()
serverfaults.record=cell[3].getContents()
serverfaults.satus=cell[4].getContents()
serverfaults.createtime=newSimpleDateFormat("yyyy/MM/dd").parse(cell[5].getContents())//转换日期格式
serverfaults.save()
}
}
wb.close()
flash.message="\u4E0A\u4F20\u6210\u529F\uFF01"
render(view:"server_import")
}catch(Exceptione){
e.printStackTrace()
}
}else{
flash.message="\u8BF7\u9009\u62E9\u6587\u4EF6\u4E0A\u4F20\uFF01"
render(view:"server_import")
return
}
}else{
flash.message="\u4F60\u7684\u767B\u5F55\u5DF2\u7ECF\u8D85\u65F6,\u8BF7\u91CD\u65B0\u767B\u5F55\u540E\u518D\u64CD\u4F5C\uFF01"
render(view:"server_import")
}
}
java类
packagecom.detao.dtma.utils;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.util.ArrayList;
importjava.util.List;
importjavax.imageio.ImageIO;
importjavax.servlet.ServletContext;
importjavax.servlet.http.HttpServletRequest;
importorg.springframework.web.multipart.MultipartFile;
importorg.springframework.web.multipart.MultipartHttpServletRequest;
publicclassFileUploadUtil{
publicstaticvoidwriteScaledImage(UFileufile,ServletContextservletContext,
intwidth,intheight)throwsException{
BufferedImagescaledImage=ImageUtils.scaleImage(ufile.file,width,
height);
Filedes=newFile(servletContext.getRealPath(ufile.path.substring(0,
ufile.path.lastIndexOf("/"))),ufile.file.getName()+"_"+width+"x"
+height+".jpg");
ImageIO.write(scaledImage,"jpeg",des);
}
publicstaticvoidwriteScaledImage(UFileufile,ServletContextservletContext,
int[]width,int[]height)throwsException{
if(width==null||height==null){
return;
}
intlength=width.length>height.length?height.length
:width.length;
for(inti=0;i<length;i++){
writeScaledImage(ufile,servletContext,width[i],height[i]);
}
}
/**
*@paramrequest
*@paramlocation
*@paramname
*@return
*@throwsException
*/
publicstaticUFileupload(HttpServletRequestrequest,Stringlocation,
Stringname)throwsException{
returnupload(request,name,location,null,100*1024*1024);
}
/**
*${contextPath}/upload/location
*
*@paramrequest
*@paramlocation
*@paramname
*@return
*@throwsException
*/
publicstaticUFileuploadImages(HttpServletRequestrequest,
Stringlocation,Stringname)throwsException{
StringlimitedExt="jpg,jpeg,png,gif,bmp,JPG,JPEG,GIF,PNG,BMP,Jpg,Jpeg,Png,Gif,Bmp";
List<String>allowedExtensions=newArrayList<String>();
if(!"".equals(limitedExt)){
String[]extensions=limitedExt.split(",");
for(Stringext:extensions){
allowedExtensions.add(ext);
}
}
returnupload(request,name,location,allowedExtensions,4*1024);
}
/**
*${contextPath}/upload
*
*@paramrequest
*@paramname
*@return
*@throwsException
*/
publicstaticUFileupload(HttpServletRequestrequest,Stringname)
throwsException{
returnupload(request,name,null,null,100*1024*1024);
}
/**
*
*@paramrequest
*@paramname
*@return
*@throwsException
*/
publicstaticUFileuploadHeadPhoto(HttpServletRequestrequest,
Stringlocation,Stringname)throwsException{
StringlimitedExt=Configuration
.getProperty("image.allowed.extension");
List<String>allowedExtensions=newArrayList<String>();
if(!"".equals(limitedExt)){
String[]extensions=limitedExt.split(",");
for(Stringext:extensions){
allowedExtensions.add(ext);
}
}
longmaxSize=Configuration.getLongProperty("image.maxsize",2048);
returnupload(request,name,location,allowedExtensions,maxSize);
}
publicstaticUFile[]upload(HttpServletRequestrequest,String[]names,
String[]dirs,List<List<String>>allowedExtensions,long[]maxSizes)
throwsException{
UFile[]ufiles=newUFile[names.length];
for(inti=0;i<ufiles.length;i++){
ufiles[i]=upload(request,names[i],dirs[i],
allowedExtensions.get(i),maxSizes[i]);
}
returnufiles;
}
/**
*@paramrequest
*@paramname
*name
*@paramlocation
*
*@paramallowedExtensions
*
*@parammaxSize
*maxSize
*@return
*@throwsException
*/
publicstaticUFileupload(HttpServletRequestrequest,Stringname,
Stringlocation,List<String>allowedExtensions,longmaxSize)
throwsException{
UFileufile=newUFile();
ufile.state=false;
if(requestinstanceofMultipartHttpServletRequest){
MultipartHttpServletRequestreq=(MultipartHttpServletRequest)request;
MultipartFilefile=req.getFile(name);
StringfileExtension=null;
if(!file.isEmpty()){
fileExtension=file
.getOriginalFilename()
.substring(
file.getOriginalFilename().lastIndexOf('.')+1)
.toLowerCase();
if(allowedExtensions!=null&&!allowedExtensions.isEmpty()
&&!allowedExtensions.contains("*")){
if(file.getOriginalFilename().lastIndexOf(".")<0){
ufile.msg="文件格式错误";
returnufile;
}
if(!allowedExtensions.contains(fileExtension)){
ufile.msg="文件格式错误["+fileExtension+"],请上传"
+allowedExtensions+"格式的文件";
returnufile;
}
}
if(file.getSize()>maxSize*1024||file.getSize()==0){
ufile.msg="请上传小于["+maxSize+"]KB的文件";
returnufile;
}
StringdirStr="/upload";
location=location==null?"":location.trim();
if(!"".equals(location)){
dirStr=location.startsWith("/")?location:"/"
+location;
}
Stringdir=request.getSession().getServletContext()
.getRealPath(dirStr);
if(!newFile(dir).exists()){
if(!newFile(dir).mkdirs()){
ufile.msg="创建目录失败!";
returnufile;
}
}
Filedes=newFile(dir,StringUtils.randomString()+"."
+fileExtension);
file.transferTo(des);
ufile.state=true;
ufile.name=file.getOriginalFilename();
ufile.path=dirStr+"/"+des.getName();
ufile.msg="文件上传成功";
ufile.file=des;
returnufile;
}else{
ufile.msg="文件不能为空,请重新选择文件上传";
returnufile;
}
}else{
ufile.msg="theform'enctypeisnotmultipart/form-data!!!";
returnufile;
}
}
}