简单Java实现Excel文件导出

Java导出要在action层(controller层)中操作
需要到的jar包:

后端代码:
后面两个函数都是设置Excel文件里的标题样式,专注第一个方法就可以了

/*** 导出房产信息* @param fcHouseProMain* @param request* @param response* @param redirectAttributes* @return*/@RequiresPermissions("housepro:fcHouseProMain:view")@RequestMapping(value = "export", method=RequestMethod.POST)public String exportFile(FcHouseProMain fcHouseProMain, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {try {String fileName = "房产信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";/*List<FcHouseProMain> list = fcHouseProMainService.findList(fcHouseProMain);new ExportExcel("房产信息", FcHouseProMain.class).setDataList(list).write(response, fileName).dispose();*/SXSSFWorkbook wb = new SXSSFWorkbook(500);Sheet sheet = wb.createSheet("Export");Map<String, CellStyle> styles = createStyles(wb);String str[] = {"ID","名称"};initialize("房产信息",Arrays.asList(str),wb,sheet,styles);for (int i = 0; i <2; i++) {Row row = sheet.createRow(rownum++);Cell ce1 = row.createCell(0);ce1.setCellValue("100"+(i+1));Cell ce2 = row.createCell(1);ce2.setCellValue("张三"+(i+1));}OutputStream out = response.getOutputStream();response.reset();response.setContentType("application/octet-stream; charset=utf-8");response.setHeader("Content-Disposition", "attachment; filename="+Encodes.urlEncode(fileName));wb.write(out);wb.dispose();out.flush();out.close();return null;} catch (Exception e) {addMessage(redirectAttributes, "导出房产信息失败!失败信息:"+e.getMessage());}return "redirect:"+Global.getAdminPath()+"/housepro/fcHouseProMain/?repage";}/*** 当前行号*/private int rownum;private void initialize(String title, List<String> headerList,SXSSFWorkbook wb,Sheet sheet,Map<String, CellStyle> styles) {// Create titleif (StringUtils.isNotBlank(title)){Row titleRow = sheet.createRow(rownum++);titleRow.setHeightInPoints(30);Cell titleCell = titleRow.createCell(0);titleCell.setCellStyle(styles.get("title"));titleCell.setCellValue(title);sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(),titleRow.getRowNum(), titleRow.getRowNum(), headerList.size()-1));}// Create headerif (headerList == null){throw new RuntimeException("headerList not null!");}Row headerRow = sheet.createRow(rownum++);headerRow.setHeightInPoints(16);for (int i = 0; i < headerList.size(); i++) {Cell cell = headerRow.createCell(i);cell.setCellStyle(styles.get("header"));String[] ss = StringUtils.split(headerList.get(i), "**", 2);if (ss.length==2){cell.setCellValue(ss[0]);Comment comment = sheet.createDrawingPatriarch().createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));comment.setString(new XSSFRichTextString(ss[1]));cell.setCellComment(comment);}else{cell.setCellValue(headerList.get(i));}sheet.autoSizeColumn(i);}for (int i = 0; i < headerList.size(); i++) {  int colWidth = sheet.getColumnWidth(i)*2;sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth);  }}/*** 创建表格样式* @param wb 工作薄对象* @return 样式列表*/private Map<String, CellStyle> createStyles(Workbook wb) {Map<String, CellStyle> styles = new HashMap<String, CellStyle>();CellStyle style = wb.createCellStyle();style.setAlignment(CellStyle.ALIGN_CENTER);style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);Font titleFont = wb.createFont();titleFont.setFontName("Arial");titleFont.setFontHeightInPoints((short) 16);titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);style.setFont(titleFont);styles.put("title", style);style = wb.createCellStyle();style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);style.setBorderRight(CellStyle.BORDER_THIN);style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderLeft(CellStyle.BORDER_THIN);style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderTop(CellStyle.BORDER_THIN);style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setBorderBottom(CellStyle.BORDER_THIN);style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());Font dataFont = wb.createFont();dataFont.setFontName("Arial");dataFont.setFontHeightInPoints((short) 10);style.setFont(dataFont);styles.put("data", style);style = wb.createCellStyle();style.cloneStyleFrom(styles.get("data"));style.setAlignment(CellStyle.ALIGN_LEFT);styles.put("data1", style);style = wb.createCellStyle();style.cloneStyleFrom(styles.get("data"));style.setAlignment(CellStyle.ALIGN_CENTER);styles.put("data2", style);style = wb.createCellStyle();style.cloneStyleFrom(styles.get("data"));style.setAlignment(CellStyle.ALIGN_RIGHT);styles.put("data3", style);style = wb.createCellStyle();style.cloneStyleFrom(styles.get("data"));
//      style.setWrapText(true);style.setAlignment(CellStyle.ALIGN_CENTER);style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);Font headerFont = wb.createFont();headerFont.setFontName("Arial");headerFont.setFontHeightInPoints((short) 10);headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);headerFont.setColor(IndexedColors.WHITE.getIndex());style.setFont(headerFont);styles.put("header", style);return styles;}

Java后端实现Excel导出,及添加Excel文件标题样式相关推荐

  1. npoi导出文件不保存在服务器,winform NPOI excel 导出并选择保存文件路径

    public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...

  2. spring boot + vue 使用poi实现Excel导出功能(包括Excel样式调整,以及前后端代码)

    可以直接复制拿来用哟.(#^.^#) 目录 一.pom.xml依赖 二.代码大致流程是这样的 三.后端工具类 四.基于vue,使用axios调用后端接口,实现下载文件的方法 五.看效果 一.pom.x ...

  3. java excel导出2007_java操作excel文件,实现批量导出,和导入

    一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...

  4. java excel导出 模板_Java Excel 导出 模板

    上面导出PDF和EXCEL的问题是图片路径出错!!! 数据库中存存的图片路径是"../dishpic/722f464f-3883-42aa-901f-21706da9c582.png&quo ...

  5. springboot+poi开发excel导出 加载Excel模板导出 Excel批量导出详解

    提到Excel导出功能,可能很多人都使用springmvc框架做过,笔者今天要给大家分享的是基于springBoot开发Excel复杂模板导出功能(所谓复杂模板指在模板里的特定表头里有不同的单元格合并 ...

  6. java的excel导出_java 实现excel 导出功能

    1 //导出数据2 //SELECT SUBSTRING(create_time,1,10) s,COUNT(*) FROM data_read WHERE data_id IN (SELECT da ...

  7. Java后端使用Freemarker导出word文档的各种细节

    2019独角兽企业重金招聘Python工程师标准>>> 1.前言 最近在项目中,因客户要求,需要做一个导出成word的功能(比如月度报表等),技术选型也考虑过几种,比如easypoi ...

  8. C#利用Microsoft.Office.Interop.Excel导出数据到Excel

    添加引用(VS2013位于C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\ ...

  9. 怎么把html表格转化为excel,导出html到excel表格数据格式-如何将html转换成excel

    如何将html转换成excel 一:Html转为Excel 1 我们打开网络上的一个包含表格内容的页面,点击菜单栏-文件-另存为. 2 弹出窗口选择保存路径,将保存类型选为"htm 或Htm ...

最新文章

  1. Android Studio无法打开解决方法
  2. 模型难复现不一定是作者的错,最新研究发现模型架构要背锅丨CVPR 2022
  3. 用archoctopus下载花瓣
  4. php表格自动添加滚动条,jsp中为表格添加水平滚动条的方法
  5. 基本select语句的生命周期
  6. JDK14性能管理工具:jmap和jhat使用介绍
  7. 《剑指offer》数组中出现次数超过一半的数字
  8. ghost不要用作域控的备份
  9. 小米12 Ultra或将在5月发布:依旧为骁龙8移动平台
  10. 零基础学sql要多久_成人零基础学习钢琴,要多久能学会?
  11. 【已解决】FAILURE: Build failed with an exception......
  12. qq在线客服html代码,QQ在线客服JS代码,自适应漂浮在网页右侧
  13. 光储直流微电网能量管理。 系统主要由光伏发电模块、mppt控制模块、混合储能系统模块、直流负载模块、改进前的soc限值管理控制模块
  14. 手把手 网络爬虫:用爬虫爬取贝壳房租网西安的租房信息
  15. Guava的Splitter和Joiner
  16. Skyscrapers (hard version)(1900/单调栈)
  17. Apollo(阿波罗)(一)环境搭建
  18. Hbuilder如何替换选中代码快捷键
  19. 多模块,Maven无法下载依赖,仓库查看有这个版本但是无法下载,点reload也没用
  20. 磨金石教育插画技能干货分享|学习插画可以从事什么工作?

热门文章

  1. 28BYJ-48 电机驱动(Linux)
  2. 学习python的记录1-通过if语句实现猜拳游戏
  3. 如何计算近似纳什均衡_莫纳什大学申请条件有哪些
  4. 列出叶节点 python
  5. 网络共享查看其他计算机取消密码,Win7局域网访问需要密码 win7取消共享密码的方法...
  6. FPGAi2c总线调试M24LC04B
  7. 机器学习之深度学习简介
  8. 视频在线发布网站HTML模板分享
  9. 大数据之大数据的典型应用
  10. 用户活跃/用户价值度分析