Springmvc导出excel表
来源:互联网 发布:什么是淘宝到家 编辑:程序博客网 时间:2024/06/05 20:53
先导入几个poi包
poi-3.8-20120326.jar
poi-excelant-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
Controller.java
//点击按钮下载表格 @RequestMapping("/excel.do") public ModelAndView downExcel(HttpServletResponse response){ String filename; try { filename = new String("excel文件名称".getBytes(),"iso8859-1"); response.setHeader("Content-Disposition","attachment;filename="+filename+".xls"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Map<String,List> dataMap = new HashMap<String,List>(); List<excelinfo> logList =excel要显示的所有数据 dataMap.put("excelList", logList); ExcelView excelview=new ExcelView(); return new ModelAndView(excelview,dataMap); }
ExcelViewExcelView继承AbstractExcelView,,excelinfo作为实体类存放一行数据
package com.lu.Dao;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.springframework.web.servlet.view.document.AbstractExcelView;import com.lu.entity.excelinfo;public class ExcelView extends AbstractExcelView { @Override protected void buildExcelDocument(Map<String, Object> excelList, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFCell cell; List<excelinfo> loglist = (List<excelinfo>) excelList.get("excelList");//从控制器Controller中返回的业务数据 int len=loglist.size(); HSSFSheet sheet = workbook.createSheet("报名表"); //创建表格 HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式 headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont headerFont = workbook.createFont(); //标题字体 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerFont.setFontHeightInPoints((short)11); headerStyle.setFont(headerFont); HSSFRow title = sheet.createRow(0);//定义表格的表头第一行 String[] headname={"xx","xx","xx","xx","xx","xx","xx","xx"}; for (int i = 0; i < headname.length; i++) { cell = title.createCell(i); //定义第一行的第i列 HSSFRichTextString text = new HSSFRichTextString(headname[i]); cell.setCellValue(text); //设置单元格格式为字符串类型 cell.setCellType(HSSFCell.ENCODING_UTF_16); } HSSFRow row; int num=1; for(excelinfo excelnum:loglist){ row= sheet.createRow(num++); //创建表格行 row.createCell(0).setCellValue(excelnum.getId()); row.createCell(1).setCellValue(excelnum.getProduction_name()); row.createCell(2).setCellValue(excelnum.getSchool()); row.createCell(3).setCellValue(excelnum.getClassify()); row.createCell(4).setCellValue(excelnum.getTeam()); row.createCell(5).setCellValue(excelnum.getAuthor_one()); row.createCell(6).setCellValue(excelnum.getAuthor_sec()); row.createCell(7).setCellValue(excelnum.getAuthor_third()); } }}
阅读全文
0 0
- Springmvc导出excel表
- springmvc导出excel
- springmvc导出excel
- SpringMVC POI导出EXCEL
- springmvc 导出excel
- SpringMVC导出Excel
- springmvc导出excel
- springMVC+poi导出excel
- jxl 导出excel springMVC
- jasperReport导出excel(springMvc)
- springMVC导出excel
- SpringMVC导出Excel
- springmvc 导出excel
- SpringMVC 导出Excel案例
- springMVC导出Excel(二)
- Springmvc 使用 AbstractExcelView 导出excel
- SpringMVC + poi 导出excel表格
- springMVC中导出excel案例
- 记录下心情
- HDU 1113 Word Amalgamation
- nodejs解析xml字符串为对象
- 2017算法课.13(Binary Tree Preoder Traversal)
- 关于 Quartus II 13.1中调用ModelSim-Altera 10.1d 报错问题
- Springmvc导出excel表
- 推送本地项目至git仓库
- IOI 1994 USACO 1.5 数字三角形 Number Triangles
- 数据结构之查找
- Hive学习指南(二)
- Hive安装配置+练习
- 微信小程序开发 一 tabbar图标和颜色
- 深度优先算法解决POJ1830开关问题
- 微服务,微架构[七]之springboot异常处理