springmvc html excel文件,springmvc生成文件(excel、pdf...)和文件上传

生成文件

以下以下载excel文件为例,如有其它需要可自定义实现类继承相应springmvc提供的试图接口即可。

如生成Excel则继承AbstractExcelView,生成PDF则继承AbstractPdfView。

java代码:

1 /*

2 * 导出用户信息到EXCEL

3 *

4 * @return

5 */

6 @RequestMapping(value = "/excel") 7 public ModelAndView exportExcel() { 8 ModelAndView mv = this.getModelAndView(); 9 PageData pd = new PageData(); 10 pd = this.getPageData(); 11 try { 12 13 // 检索条件=== 14 String USERNAME = pd.getString("USERNAME"); 15 if (null != USERNAME && !"".equals(USERNAME)) { 16 USERNAME = USERNAME.trim(); 17 pd.put("USERNAME", USERNAME); 18 } 19 String lastLoginStart = pd.getString("lastLoginStart"); 20 String lastLoginEnd = pd.getString("lastLoginEnd"); 21 if (lastLoginStart != null && !"".equals(lastLoginStart)) { 22 lastLoginStart = lastLoginStart + " 00:00:00"; 23 pd.put("lastLoginStart", lastLoginStart); 24 } 25 if (lastLoginEnd != null && !"".equals(lastLoginEnd)) { 26 lastLoginEnd = lastLoginEnd + " 00:00:00"; 27 pd.put("lastLoginEnd", lastLoginEnd); 28 } 29 // 检索条件=== 30 31 MapdataMap = new HashMap(); 32 Listtitles = new ArrayList(); 33 34 titles.add("用户名"); // 1 35 titles.add("编号"); // 2 36 titles.add("姓名"); // 3 37 titles.add("职位"); // 4 38 titles.add("手机"); // 5 39 titles.add("邮箱"); // 6 40 titles.add("最近登录"); // 7 41 titles.add("上次登录IP"); // 8 42 43 dataMap.put("titles", titles); 44 45 ListuserList = userService.listAllUser(pd); 46 ListvarList = new ArrayList(); 47 for (int i = 0; i < userList.size(); i++) { 48 PageData vpd = new PageData(); 49 vpd.put("var1", userList.get(i).getString("USERNAME")); // 1 50 vpd.put("var2", userList.get(i).getString("NUMBER")); // 2 51 vpd.put("var3", userList.get(i).getString("NAME")); // 3 52 vpd.put("var4", userList.get(i).getString("ROLE_NAME")); // 4 53 vpd.put("var5", userList.get(i).getString("PHONE")); // 5 54 vpd.put("var6", userList.get(i).getString("EMAIL")); // 6 55 vpd.put("var7", userList.get(i).getString("LAST_LOGIN")); // 7 56 vpd.put("var8", userList.get(i).getString("IP")); // 8 57 varList.add(vpd); 58 } 59 60 dataMap.put("varList", varList); 61 62 ObjectExcelView erv = new ObjectExcelView(); // 执行excel操作 63 64 mv = new ModelAndView(erv, dataMap); 65 } catch (Exception e) { 66 logger.error(e.toString(), e); 67 } 68 return mv; 69 }

返回视图类代码:

1 package com.fh.util;

2

3 import java.util.Date;

4 import java.util.List;

5 import java.util.Map;

6

7 import javax.servlet.http.HttpServletRequest;

8 import javax.servlet.http.HttpServletResponse; 9 10 import org.apache.poi.hssf.usermodel.HSSFCell; 11 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 12 import org.apache.poi.hssf.usermodel.HSSFFont; 13 import org.apache.poi.hssf.usermodel.HSSFSheet; 14 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 15 import org.springframework.web.servlet.view.document.AbstractExcelView; 16 17 18 /** 19 * 导入到EXCEL 类名称:ObjectExcelView.java 20 * @author link 21 * 22 */ 23 public class ObjectExcelView extends AbstractExcelView { 24 25 @Override 26 protected void buildExcelDocument(Mapmodel, HSSFWorkbook workbook, HttpServletRequest request, 27 HttpServletResponse response) throws Exception { 28 // TODO Auto-generated method stub 29 Date date = new Date(); 30 String filename = Tools.date2Str(date, "yyyyMMddHHmmss"); 31 HSSFSheet sheet; 32 HSSFCell cell; 33 response.setContentType("application/octet-stream"); 34 response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); 35 sheet = workbook.createSheet("sheet1"); 36 37 Listtitles = (List) model.get("titles"); 38 int len = titles.size(); 39 HSSFCellStyle headerStyle = workbook.createCellStyle(); // 标题样式 40 headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 41 headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 42 HSSFFont headerFont = workbook.createFont(); // 标题字体 43 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 44 headerFont.setFontHeightInPoints((short) 11); 45 headerStyle.setFont(headerFont); 46 short width = 20, height = 25 * 20; 47 sheet.setDefaultColumnWidth(width); 48 for (int i = 0; i < len; i++) { // 设置标题 49 String title = titles.get(i); 50 cell = getCell(sheet, 0, i); 51 cell.setCellStyle(headerStyle); 52 setText(cell, title); 53 } 54 sheet.getRow(0).setHeight(height); 55 56 HSSFCellStyle contentStyle = workbook.createCellStyle(); // 内容样式 57 contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 58 ListvarList = (List) model.get("varList"); 59 int varCount = varList.size(); 60 for (int i = 0; i < varCount; i++) { 61 PageData vpd = varList.get(i); 62 for (int j = 0; j < len; j++) { 63 String varstr = vpd.getString("var" + (j + 1)) != null ? vpd.getString("var" + (j + 1)) : ""; 64 cell = getCell(sheet, i + 1, j); 65 cell.setCellStyle(contentStyle); 66 setText(cell, varstr); 67 } 68 } 69 } 70 }

文件上传

springmvc配置文件:

1 xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:mvc="http://www.springframework.org/schema/mvc"

5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27 mvc:interceptor>

28 mvc:interceptors>

29

30

31

33

34

35 bean>

36

37

38 bean>

39

40

41

42

43 104857600value>

44 property>

45

46 4096value>

47 property>

48

49 utf-8value>

50 property>

51 bean>

52 beans>

jsp代码:

1

2

3

4 5

6 td>

7 tr>

8

9

10 导入a>

11 取消a>

12 >下载模版a>

13 td>

14 tr>

15 table>

16 div>

17 h4>div>

18 form>

java代码:

1 /**2 * 从EXCEL导入到数据库3 */4 @RequestMapping(value = "/readExcel")5 public ModelAndView readExcel(@RequestParam(value = "excel", required = false) MultipartFile file)6 throws Exception {7 ModelAndView mv = this.getModelAndView();8 PageData pd = new PageData();9

10 if (null != file && !file.isEmpty()) {11 String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE; // 文件上传路径12 String fileName = FileUpload.fileUp(file, filePath, "userexcel"); // 执行上传13

14 ListlistPd = (List) ObjectExcelRead.readExcel(filePath, fileName, 2, 0, 0); // 执行读EXCEL操作,读出的数据导入List15 // 2:从第3行开始;0:从第A列开始;0:第0个sheet16

17 /* 存入数据库操作 */18 pd.put("RIGHTS", ""); // 权限19 pd.put("LAST_LOGIN", ""); // 最后登录时间20 pd.put("IP", ""); // IP21 pd.put("STATUS", "0"); // 状态22 pd.put("SKIN", "default"); // 默认皮肤23

24 ListroleList = roleService.listAllERRoles(); // 列出所有二级角色25

26 pd.put("ROLE_ID", roleList.get(0).getROLE_ID()); // 设置角色ID为随便第一个27 /**28 * var0 :编号 var1 :姓名 var2 :手机 var3 :邮箱 var4 :备注29 */30 for (int i = 0; i < listPd.size(); i++) {

31 pd.put("USER_ID", this.get32UUID()); // ID

32 pd.put("NAME", listPd.get(i).getString("var1")); // 姓名

33

34 String USERNAME = GetPinyin.getPingYin(listPd.get(i).getString("var1")); // 根据姓名汉字生成全拼

35 pd.put("USERNAME", USERNAME);

36 if (userService.findByUId(pd) != null) { // 判断用户名是否重复

37 USERNAME = GetPinyin.getPingYin(listPd.get(i).getString("var1")) + Tools.getRandomNum();

38 pd.put("USERNAME", USERNAME);

39 }

40 pd.put("BZ", listPd.get(i).getString("var4")); // 备注

41 if (Tools.checkEmail(listPd.get(i).getString("var3"))) { // 邮箱格式不对就跳过

42 pd.put("EMAIL", listPd.get(i).getString("var3"));

43 if (userService.findByUE(pd) != null) { // 邮箱已存在就跳过

44 continue;

45 }

46 } else {

47 continue;

48 }

49

50 pd.put("NUMBER", listPd.get(i).getString("var0")); // 编号已存在就跳过

51 pd.put("PHONE", listPd.get(i).getString("var2")); // 手机号

52

53 pd.put("PASSWORD", new SimpleHash("SHA-1", USERNAME, "123").toString()); // 默认密码123

54 if (userService.findByUN(pd) != null) {

55 continue;

56 }

57 userService.saveU(pd);

58 }

59 /* 存入数据库操作*/

60

61 mv.addObject("msg", "success");

62 }

63

64 mv.setViewName("save_result");

65 return mv;

66 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值