- 浏览: 555286 次
- 性别:
- 来自: 长沙
文章分类
- 全部博客 (145)
- apache-struts (3)
- apache-shiro (4)
- apache-wicket (1)
- spring (34)
- spring-data-jpa (2)
- spring-mvc (20)
- spring-security (1)
- spring-webflow (1)
- hibernate (2)
- mongodb (1)
- ibatis (5)
- mysql (4)
- 开源组件 (18)
- java (3)
- maven (7)
- jBPM (1)
- EJB (1)
- JMS (2)
- servlet / jsp (9)
- javascript / jquery (10)
- 工作技巧 (12)
- ubuntu (6)
- bootstrap (10)
- javaee (1)
- 他山石 (7)
- jetbrick (1)
最新评论
-
yubao2008:
[size=x-small]为什么我也这样试了, 就是没有生效 ...
javax.servlet.http.HttpServletResponse 得到 status_code -
chenrl:
...
SpringMVC杂记(十五) spring-mvc controller 的切面 -
LONGTENGLONG:
你好,这样配置的,得到的集合为空,是什么原因?
apache-shiro杂记(一) 统计在线用户数目 -
xiafengfeiwu:
[flash=200,200][url]引用[/url][/f ...
apache-shiro 学习笔记 -
3108493554:
你好 ,有些问题想请教下,加下我qq310849354,你这上 ...
SpringMVC杂记(十二) 自定义Interceptor从Active Directory得到域信息
SpringMVC杂记(十一) 使用Excel视图
一) 其实这个功能在spring2.x时代就提供了。一直没用过,今天在spring-mvc3.2.x的环境下试验了一次。还算简单易用。
二) 依赖。
spring依赖POI或jExcel来实现对excel输出的支持,前者是apache出品,貌似名气更大,本例使用第一个。
三) spring提供了一个AbstractExcelView作为自己实现的视图的父类。实例代码如下。
poi的api一般人不会太熟悉,可以参考apache-poi官方文档
四) Controller中返回逻辑视图名 (代码片段)
五) 为spring-mvc配置多个视图解析器。
spring-mvc.xml(片段)
views.xml
六) 效果图
一) 其实这个功能在spring2.x时代就提供了。一直没用过,今天在spring-mvc3.2.x的环境下试验了一次。还算简单易用。
二) 依赖。
spring依赖POI或jExcel来实现对excel输出的支持,前者是apache出品,貌似名气更大,本例使用第一个。
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.7</version> </dependency>
三) spring提供了一个AbstractExcelView作为自己实现的视图的父类。实例代码如下。
poi的api一般人不会太熟悉,可以参考apache-poi官方文档
package ying.car.view; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.view.document.AbstractExcelView; import ying.car.binding.DateRange; import ying.car.domain.RefuelingRecord; public class RefuelingRecordExcelView extends AbstractExcelView { private static final Logger LOGGER = LoggerFactory.getLogger(RefuelingRecordExcelView.class); private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd"); @Override @SuppressWarnings({"unchecked"}) protected void buildExcelDocument( Map<String, Object> model, // MVC中的M就在这里了 HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { List<RefuelingRecord> rrl = (List<RefuelingRecord>) model.get("refuelingRecordList"); DateRange dr = (DateRange) model.get("dateRange"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("refueling-record-list-size: {}", rrl.size()); LOGGER.debug("dateRange: {}", dr); if (dr != null) { LOGGER.debug("start: {}", new DateTime(dr.getStart()).toString("yyyy/MM/dd")); LOGGER.debug("end: {}", new DateTime(dr.getEnd()).toString("yyyy/MM/dd")); } } HSSFSheet sheet = workbook.createSheet(DATE_FORMAT.format(dr.getStart()) + "-" + DATE_FORMAT.format(dr.getEnd())); setColumnsWidth(sheet); fillTableHeader(workbook, sheet); fillTableBody(workbook, sheet, rrl); } private void setColumnsWidth(HSSFSheet sheet) { final int[] warr = new int[] { 500, // <空> 4500, // 日期 4500, // 车辆 4500, // 燃油种类 4500, // 燃油单价 4500, // 加油方式 4500, // 加油量 3000, // 花费 12000 // 备注 }; for (int i = 0; i < warr.length; i ++) { sheet.setColumnWidth(i, warr[i]); } } // 填充表格头 private void fillTableHeader(HSSFWorkbook workbook, HSSFSheet sheet) { final String[] contents = new String[] { "日期", "车辆", "燃油种类", "燃油单价(元/升)", "加油方式", "加油量(升)", "花费(元)", "备注" }; int r = 1; int c = 1; CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); // 填充黄色 style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充方式 // 设置border style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); for (int i = 0; i < contents.length; i ++) { Cell cell = getCell(sheet, r, c + i); cell.setCellValue(contents[i]); cell.setCellStyle(style); } } private void fillTableBody(HSSFWorkbook workbook, HSSFSheet sheet, List<RefuelingRecord> records) { // 通用style CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); // 填充白色 style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充方式 style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); // 日期style CellStyle dateStyle = workbook.createCellStyle(); dateStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); // 填充白色 dateStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充方式 dateStyle.setBorderLeft(CellStyle.BORDER_THIN); dateStyle.setBorderRight(CellStyle.BORDER_THIN); dateStyle.setBorderTop(CellStyle.BORDER_THIN); dateStyle.setBorderBottom(CellStyle.BORDER_THIN); dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); int r = 2; int c = 1; Cell cell = null; for (int i = 0; i < records.size(); i ++) { RefuelingRecord rr = records.get(i); // 日期 cell = getCell(sheet, r, c + 0); if (rr.getDate() != null) cell.setCellValue(rr.getDate()); cell.setCellStyle(dateStyle); // 车辆 cell = getCell(sheet, r, c + 1); if (rr.getVehicle().getNickname() != null) cell.setCellValue(rr.getVehicle().getNickname()); cell.setCellStyle(style); // 燃油种类 cell = getCell(sheet, r, c + 2); if (rr.getGasType() != null) { String s = null; switch (rr.getGasType()) { case _0: s = "0号柴油"; break; case _93: s = "93号汽油"; break; case _97: s = "97号汽油"; break; case _98: s = "98号汽油"; break; } cell.setCellValue(s); } cell.setCellStyle(style); // 单价 cell = getCell(sheet, r, c + 3); if (rr.getPriceOfGas() != null) cell.setCellValue(rr.getPriceOfGas()); cell.setCellStyle(style); // 加油方式 cell = getCell(sheet, r, c + 4); if (rr.getRefuelingType() != null) { String s = null; switch (rr.getRefuelingType()) { case FIXED_CUBAGE: s = "固定容积"; break; case FIXED_MONEY: s = "固定金额"; break; case FULL: s = "加满"; break; } cell.setCellValue(s); } cell.setCellStyle(style); // 加油量 cell = getCell(sheet, r, c + 5); if (rr.getCubageOfGas() != null) cell.setCellValue(rr.getCubageOfGas()); cell.setCellStyle(style); // 花费 cell = getCell(sheet, r, c + 6); if (rr.getSumOfMoney() != null) cell.setCellValue(rr.getSumOfMoney()); cell.setCellStyle(style); // 备注 cell = getCell(sheet, r, c + 7); if (rr.getComment() != null) cell.setCellValue(rr.getComment()); cell.setCellStyle(style); r ++; } } }
四) Controller中返回逻辑视图名 (代码片段)
@RequiresUser // 安全框架用元注释 @RequiresRoles({"ROLE_USER"}) @RequestMapping(value = "/list/excel", method = RequestMethod.GET) public String listByExcel( @DateRangeFormat(pattern = "yyyy-MM-dd") @RequestParam("dateRange") DateRange dateRange, ModelMap modelMap ) { Integer currentUserId = ShiroUtils.getPrincipalDomainId(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("currentUserId: {}", currentUserId); if (dateRange != null) { LOGGER.debug("start: {}", new DateTime(dateRange.getStart()).toString("yyyy/MM/dd")); LOGGER.debug("end: {}", new DateTime(dateRange.getEnd()).toString("yyyy/MM/dd")); } } // 放入model modelMap.put("dateRange", dateRange); modelMap.put("refuelingRecordList", gasService.findRefuelingRecordByDateRange(currentUserId, dateRange)); return "refueling-record-list"; // 最终返回逻辑视图名 }
五) 为spring-mvc配置多个视图解析器。
spring-mvc.xml(片段)
<bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="order" value="1" /> <!-- order很重要 --> <property name="location" value="classpath:/META-INF/views.xml" /> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="9999" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
views.xml
<bean name="refueling-record-list" class="ying.car.view.RefuelingRecordExcelView" />
六) 效果图
发表评论
-
SpringMVC杂记(十八) ServletRequestAttributes的应用
2014-02-28 12:38 14138看了一下SpringMVC的源代码,原来SpringMVC也提 ... -
SpringMVC杂记(十七) HandlerMethodArgumentResolver接口应用example
2014-01-24 15:29 12151自从spring3.1 开始就有了这个接口,可以为@Reque ... -
SpringWebflow杂记(一) 框架初探,与SpringMVC的集成
2013-10-23 17:18 1664今日研究了一下SpringWebFlow这个项目,作为Spri ... -
SpringMVC杂记(十六) spring-mvc 与 openid4java
2013-10-12 15:25 3814SpringMVC杂记(十六) spring-mvc 与 op ... -
SpringMVC杂记(十五) spring-mvc controller 的切面
2013-08-01 19:42 6060SpringMVC杂记(十五) spring-mvc cont ... -
Spring集成CXF
2013-06-24 15:53 1522Spring集成CXF 零) jar依赖 <dep ... -
SpringMVC杂记(十四) Ajax方式的JSR303认证
2013-06-13 07:29 4480自己定义一个Exception,用来表示数据绑定失败 im ... -
SpringMVC杂记(十三) 使用FreeMarker作为视图层
2013-06-09 11:55 3349实在没什么好说的,直接上配置文件好了 <bean i ... -
SpringMVC杂记(十二) 自定义Interceptor从Active Directory得到域信息
2013-06-04 14:04 3149一)最近项目中要求实现Web应用的SSO(Single Sig ... -
通过spring,javamail,和freemarker集成发送HTML方式的电子邮件
2013-05-30 14:02 4819一) 现在项目中用的javamail和org.springfr ... -
Spring集成junit
2012-12-24 10:43 1452package junit; import org. ... -
BootstrapPlugin - daterangepicker 使用笔记
2012-11-13 12:17 21005BootstrapPlugin - daterangepick ... -
SpringMVC杂记(十) 验证码生成
2012-11-06 10:18 2725以前写过一篇关于这个的博客,现在用SpringMVC了,重写一 ... -
SpringMVC杂记(九) 模拟其他类型(非GET,POST)的请求
2012-10-22 10:49 26321) 以前一个小兄弟问我,SpringMVC是否可以使用很多浏 ... -
SpringMVC杂记(八) 使用阿里巴巴的fastjson
2012-07-21 08:27 102281) 国产开源软件要支持的 <dependency& ... -
ActiveMQ学习笔记(二) JMS与Spring
2012-06-24 10:21 7347上文可见,JMS Native API使用起来不是特别方便。好 ... -
我的SpringSecurity实践
2012-04-08 07:49 8908我的SpringSecurity实践 (一) 数据库与实体类 ... -
SpringMVC杂记(七) Jackson与Hibernate LazyLoding无法正常工作解决办法
2012-03-21 13:35 7000SpringMVC杂记(七) Jackson与Hibernat ... -
SpringMVC杂记(六) 下载文件
2012-03-21 09:04 4214SpringMVC杂记(六) 下载文件 1) jar依赖 ... -
SpringMVC杂记(五) JSR303数据验证
2012-03-16 16:30 12379SpringMVC杂记(五) JSR303数据验证 1) 首 ...
相关推荐
SpringMVC 使用JSON、XML视图,json-lib jackson,xstream castor,jibx jaxb2
SSM笔记-SpringMVC自定义视图及重定向视图、语言国际化
使用注解SpringMVC从页面导出Excel和word文档的使用的jar包——使用ExportExcel工具类
springMVC 整合poi导入数据,在很多项目中都会使用,能大大减少很多工作量
SpringMVC导出excel项目,国外网站下载下来有稍微修改,多次测试可用无bug。
理解视图和视图解析器的解析流程、使用常用的InternalResourceView和JstlView、实现自定义视图、实现重定向操作
SpringMVC自定义多视图,多视图实现
SpringMvc+POI 导入Excel文件,采用简单的form表单提交方式和Jquery.form.js插件提供的异步表单方式导入Excel数据
SpringMvc+POI,实现Excel2003-和Excel2007+不同版本的Excel文件导入功能,并简单实现了Excel模板方式的导出功能
主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
SpringMvc+POI 实现Excel文件的导入操作,采用传统表单提交和Jquery.form.js插件提供的异步表单提交方式
NULL 博文链接:https://yingzhuo.iteye.com/blog/1453438
SpringMVC POI Excel 生成导出
这是一个用mysql数据库做的SpringMVC,前台页面用的是EasyUI,对应两个项目用的是WebService相互调用。 实现功能:增删改查,Excel的导入导出,读取Excel文件,并在控制台打印。利用Dom4j 解析XML文件,并且实现对...
springmvc+mybaits实现excel导入功能,整理非常详细,欢迎下载参考使用
有效的纠正了下载时出现的文件类型未知或没有扩展名的情况,也处理了spring下载的情况下文件名以@RequestMapping的value值为下载文件名称的情况
1、报表导出pdf及excel 2、ireport + springmvc
springMVC 多视图配置(Jsp Freemarket Json)实例
poi excel 读写 2007 springmvc 实现
NULL 博文链接:https://yingzhuo.iteye.com/blog/1455438