本文作为自己记录笔记
看到摩课网视频有介绍java导入导出Excel的视频,看了一下就想着实践一下,本文用的poi导出Excel
工具类
public class ExportUtil { private XSSFWorkbook wb = null; private XSSFSheet sheet = null; /** * @param wb * @param sheet */ public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet) { this.wb = wb; this.sheet = sheet; } /** * 合并单元格后给合并后的单元格加边框 * * @param region * @param cs */ public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs) { int toprowNum = region.getFirstRow(); for (int i = toprowNum; i <= region.getLastRow(); i++) { XSSFRow row = sheet.getRow(i); for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) { XSSFCell cell = row.getCell(j);// XSSFCellUtil.getCell(row, // (short) j); cell.setCellStyle(cs); } } } /** 43 * 设置表头的单元格样式 44 * 45 * @return 46 */ public XSSFCellStyle getHeadStyle() { // 创建单元格样式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 设置单元格的背景颜色为淡蓝色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 创建单元格内容显示不下时自动换行 cellStyle.setWrapText(true); // 设置单元格字体样式 XSSFFont font = wb.createFont(); // 设置字体加粗 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); // 设置单元格边框为细线条 cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); return cellStyle; } /** 76 * 设置表体的单元格样式 77 * 78 * @return 79 */ public XSSFCellStyle getBodyStyle() { // 创建单元格样式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 设置单元格居中对齐 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 创建单元格内容显示不下时自动换行 cellStyle.setWrapText(true); // 设置单元格字体样式 XSSFFont font = wb.createFont(); // 设置字体加粗 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); // 设置单元格边框为细线条 cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); return cellStyle; } }
主要方法(导出订单)
public void exportExcel(List<OrderBean> orderList,String[] titles, ServletOutputStream outputStream) { // TODO Auto-generated method stub //数据 // List<User> orderList = dao.findUser(0, 3); // 创建一个workbook 对应一个excel应用文件 XSSFWorkbook workBook = new XSSFWorkbook(); // 在workbook中添加一个sheet,对应Excel文件中的sheet XSSFSheet sheet = workBook.createSheet("导出订单excel例子"); ExportUtil exportUtil = new ExportUtil(workBook, sheet); //头部样式 XSSFCellStyle headStyle = exportUtil.getHeadStyle(); //内容样式 XSSFCellStyle bodyStyle = exportUtil.getBodyStyle(); // 构建表头 XSSFRow headRow = sheet.createRow(0); XSSFCell cell = null; for (int i = 0; i < titles.length; i++) { cell = headRow.createCell(i); cell.setCellStyle(headStyle); cell.setCellValue(titles[i]); } // 构建表体数据 if (orderList != null && orderList.size() > 0) { for (int j = 0; j < orderList.size(); j++) { XSSFRow bodyRow = sheet.createRow(j + 1); OrderBean order = orderList.get(j); cell = bodyRow.createCell(0); cell.setCellStyle(bodyStyle); cell.setCellValue(order.getUser().getUsername()); cell = bodyRow.createCell(1); cell.setCellStyle(bodyStyle); cell.setCellValue(order.getMoveArrang().getMove().getName()); cell = bodyRow.createCell(2); //cell.setCellStyle(bodyStyle); XSSFCellStyle bodyStyle1 = workBook.createCellStyle(); //格式化时间 XSSFDataFormat format= workBook.createDataFormat(); bodyStyle1.setDataFormat(format.getFormat("yyyy-MM-dd")); bodyStyle1.setBorderLeft(XSSFCellStyle.BORDER_THIN); bodyStyle1.setBorderBottom(XSSFCellStyle.BORDER_THIN); bodyStyle1.setBorderRight(XSSFCellStyle.BORDER_THIN); bodyStyle1.setBorderTop(XSSFCellStyle.BORDER_THIN); cell.setCellValue(order.getDate()); cell.setCellStyle(bodyStyle1); cell = bodyRow.createCell(3); cell.setCellStyle(bodyStyle); cell.setCellValue(order.getTotal()); cell = bodyRow.createCell(4); cell.setCellStyle(bodyStyle); cell.setCellValue(order.getState()); cell = bodyRow.createCell(5); cell.setCellStyle(bodyStyle); cell.setCellValue(order.getTelephoto()); } } try { workBook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }传入相关数据
@RequestMapping("/orderExcel") public String exportExcel(HttpServletResponse response,HttpServletRequest request) { HttpSession session = request.getSession(); List<OrderBean> orderList = (List<OrderBean>) session.getAttribute("OrderList"); response.setContentType("application/binary;charset=ISO8859_1"); try { System.out.println("excel"); ServletOutputStream outputStream = response.getOutputStream(); String fileName = new String(("导出订单excel例子").getBytes(), "ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式 String[] titles = { "下单者", "电影名称", "上映日期", "价格", "下单状态", "取票手机号码" }; orderService.exportExcel(orderList, titles, outputStream); } catch (IOException e) { e.printStackTrace(); } return null; }
导入相关jar包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency>