java-SpringMVC框架导出Excel格式数据
2017-12-19 16:13
696 查看
1.控制层Controller,使用getLCCnsFeeCalMainExcel.do去接收
2.LCCnsFeeCalMainViewExcel
LCCnsFeeCalMainViewExcel类继承AbstractExcelView,调用构造方法及重写的buildExcelDocument方法
构造方法:创建变量templetFilePath,内容为模板地址,并赋值,赋值过程在步骤3中展示
重写的buildExcelDocument:将参数使用ExcelUtil类,插入Excel表中,代码如下:
3.web.xml配置
在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,最好保证每个项目的参数值不同,以免引起项目冲突。
4.配置监听器GeneralListener类
使用event.getServletContext().getRealPath("/")方法取得物理路径,并把其作为key和value放到system.properties系统属性中
5.ModelAndView
最后使用ModelAndView接收excel格式数据参数,并返回给前端展示
6.Excel文件导出成功
@SuppressWarnings("rawtypes") //去除警告 @RequestMapping("getLCCnsFeeCalMainExcel.do") public ModelAndView queryExcel(@RequestParam("req") String hreq) throws ParseException, IOException, SQLException { // 根据保单号查询其他信息 hreq = URLDecoder.decode(hreq,"UTF-8"); LCCnsFeeCalMainDto tLCCnsFeeCalMainDto = JsonUtils.toJsonObject(hreq, LCCnsFeeCalMainDto.class); List<Map> ptpList = tLCCnsFeeCalMainBlo.queryExcel(tLCCnsFeeCalMainDto);// 数据导出数据查询 String excelName = "LCCnsFeeCalMain.xls";// 创建生成文件的名字 LCCnsFeeCalMainViewExcel tLCCnsFeeCalMainViewExcel = new LCCnsFeeCalMainViewExcel( excelName, ptpList);// 保单跟进文件生成方法 return new ModelAndView(tLCCnsFeeCalMainViewExcel); }
2.LCCnsFeeCalMainViewExcel
LCCnsFeeCalMainViewExcel类继承AbstractExcelView,调用构造方法及重写的buildExcelDocument方法
构造方法:创建变量templetFilePath,内容为模板地址,并赋值,赋值过程在步骤3中展示
package com.yunhui.oversea.policy.view; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.util.List; import java.util.Map; import java.util.Properties; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.servlet.view.document.AbstractExcelView; import com.yunhui.oversea.policy.util.ExcelUtil; public class LCCnsFeeCalMainViewExcel extends AbstractExcelView { String templetFilePath; String generateFilePath; Properties datas; @SuppressWarnings("rawtypes") List<Map> list; /*** * 构造方法 * @param generateFilePath * @param list */ @SuppressWarnings("rawtypes") public LCCnsFeeCalMainViewExcel(String generateFilePath,List<Map> list) { //获取文件模板 this.templetFilePath = System.getProperty("webServerRoot")+"/excel/LCCnsFeeCalMainView.xls"; this.generateFilePath = generateFilePath; this.list = list; } /*** * 向Excel表中写数据,生成对应的Excel文档 */ @Override protected void buildExcelDocument(Map<String, Object> arg0, HSSFWorkbook arg1, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + this.generateFilePath); ExcelUtil excel = new ExcelUtil(this.templetFilePath); excel.insertRow(1,this.list); OutputStream ouputStream = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(ouputStream); excel.wb.write(bos); bos.flush(); bos.close(); } }
重写的buildExcelDocument:将参数使用ExcelUtil类,插入Excel表中,代码如下:
public void insertRow(int starRow,List<Map> list) throws IOException { HSSFSheet sheet = this.wb.getSheetAt(0); // 选择一个区域,从startRow+1直到最后一行 sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), list.size(), true, false); //System.out.println("starRow1:" + starRow); starRow = starRow - 1; for (int i = 0; i < list.size(); i++) { HSSFRow sourceRow = null; HSSFRow targetRow = null; HSSFCell sourceCell = null; HSSFCell targetCell = null; short m; starRow = starRow + 1; sourceRow = sheet.getRow(starRow); //System.out.println("starRow2:" + starRow); if (sourceRow == null) { sourceRow = sheet.createRow(starRow); } // 從start創建新的一行 targetRow = sheet.createRow(starRow + 1); targetRow.setHeight(sourceRow.getHeight()); // sourceRow.setRowNum(sourceRow.getRowNum()); // 处理刚刚创建的一行 Map cellmap = (HashMap)list.get(i); Object s[] = cellmap.keySet().toArray(); for(m = 0; m < cellmap.size(); m++) { //for (m = sourceRow.getFirstCellNum(); m < cellList.size(); m++) { sourceCell = sourceRow.getCell(m); targetCell = targetRow.createCell(m); // 风格一样 HSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); HSSFFont fontNormal = wb.createFont(); fontNormal.setFontHeightInPoints((short) 10); fontNormal.setFontName("宋体"); fontNormal.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 style.setFont(fontNormal); targetCell.setCellStyle(style); //targetCell.setCellStyle(sourceCell.getCellStyle()); //targetCell.setCellType(sourceCell.getCellType()); String value = ""; if (cellmap.get(s[m]) != null) { if (cellmap.get(s[m]).getClass().equals(Timestamp.class)) { Timestamp timestamp = (Timestamp) cellmap.get(s[m]); Date date = new Date(timestamp.getTime()); value = DateUtils.format(date, DateUtils.FORMAT_LONG); } else if (cellmap.get(s[m]).getClass().equals(BigDecimal.class)) { value = StringUtils.twoDecimal(((BigDecimal) cellmap.get(s[m])).doubleValue()); } else { value = cellmap.get(s[m]) + ""; } } targetCell.setCellType(HSSFCell.CELL_TYPE_STRING); targetCell.setEncoding(HSSFCell.ENCODING_UTF_16); targetCell.setCellValue(value);// 設置值 } } }
3.web.xml配置
在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,最好保证每个项目的参数值不同,以免引起项目冲突。
<!-- <context-param> <param-name>webAppRootKey</param-name> <param-value>webServerRoot</param-value> </context-param> -->
<listener> <listener-class>com.yunhui.oversea.policy.servlet.GeneralListener </listener-class> </listener>
4.配置监听器GeneralListener类
使用event.getServletContext().getRealPath("/")方法取得物理路径,并把其作为key和value放到system.properties系统属性中
public class GeneralListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent event) { String webServerRoot = event.getServletContext().getRealPath("/"); System.setProperty("webServerRoot", webServerRoot); Properties properties = new Properties(); try { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("system.properties"); properties.load(inputStream); inputStream.close(); //关闭流 } catch (IOException e) { e.printStackTrace(); } LogUtils.info(Log.BIZ_LOGGER, "webServerRoot:"+event.getServletContext().getRealPath("/")); }
5.ModelAndView
最后使用ModelAndView接收excel格式数据参数,并返回给前端展示
6.Excel文件导出成功
相关文章推荐
- Java导出Excel设置大数据不被科学计数法显示、自定义日期格式
- Java大批量导出数据,格式可以为xml或excel.
- Java大批量导出数据,格式可以为xml或excel
- java 使用poi导出excel,可控制固定前2列固定标头排版,带统计数据格式的
- Java大批量导出数据,格式可以为xml或excel
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 数据导出Excel的简便方法(无格式要求)
- Web页面的数据导出excel时的格式问题
- 将DATAGRID的数据导出为一个标准EXCEL格式的文件
- C#用流实现DataGridView中数据Excel格式的导出
- Web页面的数据导出excel时的格式问题(转)
- java中jxl导出数据到excel的例子
- [转]asp.net导出Excel/Csv格式数据最优方案(C#)
- .net解决数据导出excel时的格式问题
- Java利用xml将大批量数据导出到excel的一个方法
- Web页面的数据导出excel时的格式问题 【转】
- Web页面的数据导出excel时的格式问题(转)
- Java技巧:创建Excel格式的数据
- Web页面的数据导出excel时的格式问题
- Web页面的数据导出excel时的格式问题