grails 批量导入excel文件

def serverimport={
render(view: "server_import")
}
def server_import ={def loginUser = UtilsController.getLoginUser(session)//取得登录人

if(loginUser){
def ufile = FileUploadUtil.upload(request, "batchfile")//上传文件。写好的java类直接引用
def out = 0
def j = 0
def failDevice = ''
if(ufile.state){
try{
def wb = Workbook.getWorkbook(ufile.file)
def sheet = wb.getSheet(0)
def rows = sheet.getRows()

for (int i = 1; i < rows; i++){
def serverfaults=new ServerFaults()
def cell = sheet.getRow(i)
if(cell[0].getContents()!=null&&cell[0].getContents()!=""){
def report1=cell[0].getContents()

def report2=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=new SimpleDateFormat("yyyy/MM/dd").parse(cell[5].getContents())//转换日期格式

serverfaults.save()

}


}

wb.close()
flash.message ="\u4E0A\u4F20\u6210\u529F\uFF01"
render(view: "server_import")
}catch(Exception e){
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类
package com.detao.dtma.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

public class FileUploadUtil {

public static void writeScaledImage(UFile ufile, ServletContext servletContext,
int width, int height) throws Exception {
BufferedImage scaledImage = ImageUtils.scaleImage(ufile.file, width,
height);
File des = new File(servletContext.getRealPath(ufile.path.substring(0,
ufile.path.lastIndexOf("/"))), ufile.file.getName() + "_" + width + "x"
+ height + ".jpg");
ImageIO.write(scaledImage, "jpeg", des);
}

public static void writeScaledImage(UFile ufile, ServletContext servletContext,
int[] width, int[] height) throws Exception {
if (width == null || height == null) {
return;
}
int length = width.length > height.length ? height.length
: width.length;
for (int i = 0; i < length; i++) {
writeScaledImage(ufile, servletContext, width[i], height[i]);
}
}

/**
* @param request
* @param location
* @param name
* @return
* @throws Exception
*/
public static UFile upload(HttpServletRequest request, String location,
String name) throws Exception {
return upload(request, name, location, null, 100 * 1024 * 1024);
}

/**
* ${contextPath}/upload/location
*
* @param request
* @param location
* @param name
* @return
* @throws Exception
*/
public static UFile uploadImages(HttpServletRequest request,
String location, String name) throws Exception {
String limitedExt = "jpg,jpeg,png,gif,bmp,JPG,JPEG,GIF,PNG,BMP,Jpg,Jpeg,Png,Gif,Bmp";
List<String> allowedExtensions = new ArrayList<String>();
if (!"".equals(limitedExt)) {
String[] extensions = limitedExt.split(",");
for (String ext : extensions) {
allowedExtensions.add(ext);
}
}
return upload(request, name, location, allowedExtensions, 4 * 1024);
}

/**
* ${contextPath}/upload
*
* @param request
* @param name
* @return
* @throws Exception
*/
public static UFile upload(HttpServletRequest request, String name)
throws Exception {
return upload(request, name, null, null, 100 * 1024 * 1024);
}

/**
*
* @param request
* @param name
* @return
* @throws Exception
*/
public static UFile uploadHeadPhoto(HttpServletRequest request,
String location, String name) throws Exception {
String limitedExt = Configuration
.getProperty("image.allowed.extension");
List<String> allowedExtensions = new ArrayList<String>();
if (!"".equals(limitedExt)) {
String[] extensions = limitedExt.split(",");
for (String ext : extensions) {
allowedExtensions.add(ext);
}
}
long maxSize = Configuration.getLongProperty("image.maxsize", 2048);
return upload(request, name, location, allowedExtensions, maxSize);
}

public static UFile[] upload(HttpServletRequest request, String[] names,
String[] dirs, List<List<String>> allowedExtensions, long[] maxSizes)
throws Exception {
UFile[] ufiles = new UFile[names.length];
for (int i = 0; i < ufiles.length; i++) {
ufiles[i] = upload(request, names[i], dirs[i],
allowedExtensions.get(i), maxSizes[i]);
}
return ufiles;
}

/**
* @param request
* @param name
* name
* @param location
*
* @param allowedExtensions
*
* @param maxSize
* maxSize
* @return
* @throws Exception
*/
public static UFile upload(HttpServletRequest request, String name,
String location, List<String> allowedExtensions, long maxSize)
throws Exception {
UFile ufile = new UFile();
ufile.state = false;
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
MultipartFile file = req.getFile(name);
String fileExtension = 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 = "文件格式错误";
return ufile;
}
if (!allowedExtensions.contains(fileExtension)) {
ufile.msg = "文件格式错误 [" + fileExtension + "],请上传 "
+ allowedExtensions + " 格式的文件";
return ufile;
}
}
if (file.getSize() > maxSize * 1024 || file.getSize() == 0) {
ufile.msg = "请上传小于 [" + maxSize + "] KB 的文件";
return ufile;
}

String dirStr = "/upload";

location = location == null ? "" : location.trim();
if (!"".equals(location)) {
dirStr = location.startsWith("/") ? location : "/"
+ location;
}

String dir = request.getSession().getServletContext()
.getRealPath(dirStr);

if (!new File(dir).exists()) {
if (!new File(dir).mkdirs()) {
ufile.msg = "创建目录失败!";
return ufile;
}
}
File des = new File(dir, StringUtils.randomString() + "."
+ fileExtension);

file.transferTo(des);
ufile.state = true;
ufile.name = file.getOriginalFilename();
ufile.path = dirStr + "/" + des.getName();
ufile.msg = "文件上传成功";
ufile.file = des;

return ufile;
} else {
ufile.msg = "文件不能为空,请重新选择文件上传";
return ufile;
}
} else {
ufile.msg = "the form'enctype is not multipart/form-data !!!";
return ufile;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值