spring+springmvc+hibernate利用poi实现导出Excel功能
来源:互联网 发布:淘宝上那些代购是真的 编辑:程序博客网 时间:2024/06/07 01:21
转自:http://www.cnblogs.com/baizhanshi/p/5667751.html
1 主要相关jar包:http://pan.baidu.com/s/1bKLscQ 密码:y1yj
ooxml-schemas-1.3.jar
poi-3.14.jar
poi-ooxml-3.14.jar
xmlbeans-2.6.0.jar
2 主要工具类:ExportExcel
package cn.com.utils;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;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.Font;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;/** * 导出Excel文档工具类 * * @author yanzi * @date 2017-9-19 */public class ExportExcel { /** * 创建excel文档, * * @param list 数据 * @param keys list中map的key数组集合 * @param columnNames excel的列名 * @throws Exception */ public static Workbook createWorkBook(List<?> lists, String[] keys, String columnNames[], Class<?> clazz) throws Exception { List<Map<String, Object>> listMap = initData(lists, clazz); // 创建excel工作簿 //Workbook wb = new HSSFWorkbook(); // 创建第一个sheet(页),并命名 //Sheet sheet = wb.createSheet(list.get(0).get("sheetName").toString()); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet= wb.createSheet(listMap.get(0).get("sheetName").toString()); // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。 for (int i = 0; i < keys.length; i++) { sheet.setColumnWidth(i, (int)35.7 * 150); } // 创建第一行 Row row = sheet.createRow(0); // 创建两种单元格格式 CellStyle cs = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); // 创建两种字体 Font f = wb.createFont(); Font f2 = wb.createFont(); // 创建第一种字体样式(用于列名) f.setFontHeightInPoints((short) 10); f.setColor(IndexedColors.BLACK.getIndex()); f.setBoldweight(Font.BOLDWEIGHT_BOLD); // 创建第二种字体样式(用于值) f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.BLACK.getIndex()); // 设置第一种单元格的样式(用于列名) cs.setFont(f); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); cs.setAlignment(CellStyle.ALIGN_CENTER); // 设置第二种单元格的样式(用于值) cs2.setFont(f2); cs2.setBorderLeft(CellStyle.BORDER_THIN); cs2.setBorderRight(CellStyle.BORDER_THIN); cs2.setBorderTop(CellStyle.BORDER_THIN); cs2.setBorderBottom(CellStyle.BORDER_THIN); cs2.setAlignment(CellStyle.ALIGN_CENTER); //设置列名 for (int i = 0; i < columnNames.length; i++) { Cell cell = row.createCell(i); cell.setCellValue(columnNames[i]); cell.setCellStyle(cs); } //设置每行每列的值 for (int i = 1; i < listMap.size(); i++) { // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的 // 创建一行,在页sheet上 Row row1 = sheet.createRow(i); // 在row行上创建一个方格 for (int j = 0; j < keys.length; j++) { Cell cell = row1.createCell(j); cell.setCellValue(listMap.get(i).get(keys[j]) == null ? " " : listMap.get(i).get(keys[j]).toString()); cell.setCellStyle(cs2); } } return wb; } /** * 转换数据,把类型List<?> roomLists转化为List<Map<String, Object>>, * * @param list 原始数据 * @param clazz 反射类 * @param columnNames excel的列名 */ private static List<Map<String, Object>> initData(List<?> lists, Class<?> clazz) throws Exception { List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("sheetName", "sheet1"); listmap.add(map); Object object; Field[] fields = clazz.getDeclaredFields(); for (int j = 0; j < lists.size(); j++) { object = lists.get(j); Map<String, Object> mapValue = new HashMap<>(); mapValue.put("no", j+1); for (Field field : fields) { field.setAccessible( true ); mapValue.put(field.getName(), field.get(object)); } listmap.add(mapValue); } return listmap; }}
3 Controller
// 处理请求@RequestMapping("room/export") public String getRoomExportExcel2(String COName, String FullName, String Name, PageBean pageBean, HttpServletResponse response) throws Exception { //填充list数据 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); String fileName = "导出房屋excel" + sf.format(new Date()) + ".xlsx"; // 获取数据来源 pageBean.setPageSize(101); PageBean result = personRoomDao.getRoomList(pageBean, COName, FullName, Name); List<RoomInfo> roomLists = result.getRows(); String columnNames[] = { "序号", "房间号", "所属地区","所属社区", "社区结构", "修改时间"}; ;//列名 String keys[] = {"no", "roomName", "regionName", "coName", "structName", "modifytime"};//map中的key ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ExportExcel.createWorkBook(roomLists, keys, columnNames, RoomInfo.class).write(os); } catch (IOException e) { e.printStackTrace(); } byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 设置response参数,可以打开下载页面 response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (final IOException e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } return null; }
4 jsp调用:
<button class="btn btn-sm btn-info" type="button"style="vertical-align: top;" onclick="exportExcel(this)"> <i class="fa fa-search"></i>导出Excel</button>function exportExcel(dom) { var rows = 10, COName=$.trim($("#COName").val()), FullName=$.trim($("#FullName").val()), Name=$.trim($("#Name").val()); $(dom).attr("disabled", "disabled"); window.open("export.action?COName=" + COName + "&FullName=" + FullName + "&Name=" +Name);}
5 效果(ie浏览器)
阅读全文
0 0
- spring+springmvc+hibernate利用poi实现导出Excel功能
- Spring+Mybatis+SpringMVC框架,利用Apache POI动态导出Excel
- SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
- SpringMVC+POI 实现Excel按模板方式简单导出功能
- 利用Java反射机制和POI实现导出Excel功能
- 利用Java反射机制和POI实现导出Excel功能
- 利用poi完成导出excel功能
- SpringMVC实现poi 解析excel 导入导出
- SpringMVC实现poi 解析excel 导入导出
- SpringMVC POI导出EXCEL
- springMVC+poi导出excel
- Springboot/SpringMVC+POI 实现Excel导出功能(点击下载方式实现)
- 利用poi实现table表格导出excel
- 利用POI实现Excel的导入导出
- Java利用POI实现导出excel文件
- Struts2+Poi实现导出excel功能
- POI实现java导出Excel功能
- Java实现POI导出Excel报表功能
- Dubbo 管理页面的部署
- iOS-从任何一个view中获得当前控制器
- CSR是什么样的公司?CSR蓝牙芯片有何过人之处?
- 域名--->IP 和 IP---->MAC
- hdu 6215 Brute Force Sorting
- spring+springmvc+hibernate利用poi实现导出Excel功能
- 关于服务机器人,这都是你不知道的事
- 深度学习里神奇的1*1卷积核
- php综合复习题大全(上)——基础篇
- cat命令
- C# “不能使用非固定表达式中包含的固定大小缓冲区。请尝试使用 fixed 语句”
- 生成随机数组-php
- caffe-0.16 nvidia docker编译
- 冒泡排序