批量导入CMS内容,用到jxl.jar包

来源:互联网 发布:知乎红色警戒2 编辑:程序博客网 时间:2024/06/05 12:08
package com.tynet.cms.controller;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import jxl.Sheet;
import jxl.Workbook;


import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;


import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.soft.gh.util.StringUtil;
import com.tynet.base.controller.BaseController;
import com.tynet.base.controller.MgBaseController;
import com.tynet.base.controller.pageform.PageForm;
import com.tynet.cms.dao.DyresourceMapper;
import com.tynet.cms.model.CmsFj;
import com.tynet.cms.model.CmsTitle;
import com.tynet.cms.model.CmsType;
import com.tynet.cms.model.Dyresource;
import com.tynet.cms.service.MgFjService;
import com.tynet.cms.service.MgTitleService;
import com.tynet.cms.service.MgTypeService;
import com.tynet.hos.pageForm.GhYhxxHospital;
import com.tynet.hos.service.HospitalService;
import com.tynet.shiro.ShiroDbRealm.ShiroUser;
import com.tynet.sys.model.SysUser;
import com.tynet.util.BeanUtil;
import com.tynet.util.Const;
import com.tynet.util.DateUtils;
import com.tynet.util.FileUtil;
import com.tynet.util.ModelAndViewUtil;
import com.tynet.util.SessionKeyConst;
import com.tynet.util.SessionUtils;
import com.tynet.util.WebFileUtils;


@Controller
@RequestMapping(value = BaseController.ADMIN_PATH + "/mg/cms/title")
public class MgTitleController extends MgBaseController {
@Autowired
private MgTypeService mgTypeService;
@Autowired
private MgTitleService mgTitleService;


/**
* 批量导入CMS内容
* 使用xls文件进行导入
* @param model
* @param file
* @return
* @throws Exception
*/
@RequiresPermissions("cms:title:edit")
@RequestMapping("/batchsave")
public String batchsave(@RequestParam(value = "file", required = false) MultipartFile file, RedirectAttributesModelMap redirectAttributes) {
try {
//读取XLS文件内容开始
String url = FileUtil.saveFile(request, file, null, "/static/upload/");
File f = new File(url);
Workbook rwb = Workbook.getWorkbook(f);
Sheet rs = rwb.getSheet(0);
int rows = rs.getRows();
//读取XLS文件内容结束

//添加内容到数据
if (file != null) {
if (rows > 1) {
for (int i = 1; i < rows; i++) {
CmsTitle cmsTitle = new CmsTitle();
String typeName = rs.getCell(0, i).getContents();
cmsTitle.setPicId("1234");
cmsTitle.setTop("0");
cmsTitle.setTitle(rs.getCell(1, i).getContents());
cmsTitle.setSource(rs.getCell(2, i).getContents());
String time=rs.getCell(3, i).getContents();
cmsTitle.setcTime(DateUtils.getDate(time, "yyyyMMdd", new Date()));
cmsTitle.setContent(rs.getCell(4, i).getContents());
cmsTitle.setChkFlag("1");
mgTitleService.save(cmsTitle);
}
addFlashMessage(redirectAttributes, "内容导入成功");


} else {
addFlashMessage(redirectAttributes, "文件不正确");
}
}else{
addFlashMessage(redirectAttributes, "文件不正确");
}
} catch (Exception e) {
addFlashMessage(redirectAttributes, "内容导入失败");
e.printStackTrace();
}


return "redirect:/a/mg/cms/title";


}


/**
* 进入批量添加内容
* @return
*/
@RequiresPermissions("cms:title:edit")
@RequestMapping("/bacthfrom")
public String bacthfrom(HttpServletRequest request) {
String cmsTitleUrl = request.getParameter("cmsTitleUrl");
request.setAttribute("cmsTitleUrl", cmsTitleUrl);
return "mg/cms/bacthfrom";
}


public static String saveFile(HttpServletRequest request, MultipartFile file, String fileName, String path) throws IllegalStateException, IOException {
String url = null;
String savePath = null;
if (file != null && !file.isEmpty()) {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
if (!WebFileUtils.isSafeExt(suffix)) {
return null;
}
String logImageName = DateUtil.dateFormate(new Date(), "yyyyMMddHHmmss") + suffix;// 构建文件名称
if (StringUtils.isNotBlank(fileName)) {
logImageName = fileName;
}


/** 拼成完整的文件保存路径加文件* */
String fileurl = request.getSession().getServletContext().getRealPath(path);
File sfile = new File(fileurl);
if (!sfile.exists()) {
sfile.mkdir();
}
savePath = fileurl + "/" + logImageName;
File tempfile = new File(savePath);
file.transferTo(tempfile);
url = savePath;
}
return url;
}
}
0 0