工作总结之----动态导出数据到Excel,多sheet页

2024-08-27 10:18

本文主要是介绍工作总结之----动态导出数据到Excel,多sheet页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/**
     * 导出
     */
    @RequestMapping(value = "/exlMonthlyList")@ResponseBody
    public String exlMonthlyList(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {//选择列名
        String columnName = request.getParameter("columnName");//导出的表头columnName = URLDecoder.decode(URLDecoder.decode(columnName, "UTF-8"), "UTF-8");List<MonthlyCustom> monthlyCustomList = JSONArray.parseArray(columnName, MonthlyCustom.class);//单位
        String unitId = request.getParameter("unitId");//单位分sheet,一个单位一个sheet,空则所有的数据导入一个sheetString unitName = request.getParameter("unitName");
//        //项目类别
//        String projectType = request.getParameter("projectType");
        //导出月份
        String reportMonth = request.getParameter("reportMonth");String reportingType = request.getParameter("reportingType");Map<String, Object> map = new HashMap<String, Object>();map.put("columnName", monthlyCustomList);map.put("unitId", unitId);map.put("unitName", unitName);map.put("reportingType",reportingType);map.put("reportMonth", reportMonth);String filename = reportMonth + "月报汇总表.xls";try {//创建excle表格
            HSSFWorkbook workbook = new HSSFWorkbook();workbook = projectBaseInfoService.exlMonthlyList(workbook, map);response.reset();response.setContentType("application/octet-stream");response.addHeader("Content-Disposition", "attachment;filename="
                    + java.net.URLEncoder.encode(filename, "UTF-8"));OutputStream os = new BufferedOutputStream(response.getOutputStream());workbook.write(os);os.flush();os.close();} catch (Exception e) {logger.error("exlMonthlyList()出错", e);e.printStackTrace();}return null;}
/**
   * 导出
   */
  @Override
  public HSSFWorkbook exlMonthlyList(HSSFWorkbook workbook, Map<String,Object> map) {String  unitId= map.get("unitId")==null?"":map.get("unitId").toString();String unitName =map.get("unitName")==null?"":map.get("unitName").toString();String reportingType = map.get("reportingType")==null?"":map.get("reportingType").toString();String reportMonth = map.get("reportMonth")==null?"":map.get("reportMonth").toString();List <MonthlyCustom>  monthlyCustomList = (List <MonthlyCustom>)map.get("columnName");String [] unitIds=null;String [] unitNames=null;if(!"".equals(unitId)){unitIds = unitId.split(",");}if(!"".equals(unitName)){unitNames = unitName.split(",");}// 生成一个样式
      HSSFCellStyle style = workbook.createCellStyle();// 设置这些样式
      style.setFillForegroundColor(HSSFColor.WHITE.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);style.setAlignment(HSSFCellStyle.ALIGN_CENTER);style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 生成一个字体
      HSSFFont font = workbook.createFont();font.setFontHeightInPoints((short) 9);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 把字体应用到当前的样式
      style.setFont(font);// 生成并设置另一个样式
      HSSFCellStyle style2 = workbook.createCellStyle();style2.setFillForegroundColor(HSSFColor.WHITE.index);style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);style2.setBorderRight(HSSFCellStyle.BORDER_THIN);style2.setBorderTop(HSSFCellStyle.BORDER_THIN);style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style2.setWrapText(true);// 生成并设置另一个样式
      HSSFCellStyle style3 = workbook.createCellStyle();style3.setFillForegroundColor(HSSFColor.WHITE.index);style3.setAlignment(HSSFCellStyle.ALIGN_LEFT);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 生成另一个字体
      HSSFFont font3 = workbook.createFont();font3.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);font3.setFontName("楷体");font3.setFontHeightInPoints((short) 12);style3.setFont(font3);// 生成另一个字体
      HSSFFont font2 = workbook.createFont();font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 把字体应用到当前的样式
      style2.setFont(font2);HSSFSheet sheet;HSSFRow firstHeaderRow;HSSFRow secondHeaderRow;HSSFRow thirdHeaderRow;HSSFCell yearCell;HSSFCellStyle titleStyle;HSSFFont titleFont;HSSFRow row;HSSFCell cell;List dateList;//合并单元格
      int maxLength;if( unitIds!=null && unitIds.length>0) {maxLength =(monthlyCustomList==null?0:monthlyCustomList.size())+2;//生成各个单位的表格
          for (int ti = 0; ti < unitIds.length; ti++) {map.put("unitId", unitIds[ti]);map.put("unitName", unitNames[ti]);dateList = projectInfoDao.querymonthlyList(map);// 生成一个表格  sheet名称  汇总数据
              sheet = workbook.createSheet(unitNames[ti] + "月报汇总");// 设置表格默认列宽度为15个字节
              sheet.setDefaultColumnWidth((short) 15);//生成title
              firstHeaderRow = sheet.createRow(0);// 第一行
              secondHeaderRow = sheet.createRow(1);// 第二行
              thirdHeaderRow = sheet.createRow(2);// 第三行
              //合并单元格
              sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, maxLength-1));yearCell = firstHeaderRow.createCell(0);yearCell.setCellValue(reportMonth + "月报汇总表");titleStyle = workbook.createCellStyle();titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);titleFont = workbook.createFont();titleFont.setFontHeightInPoints((short) 16);titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);titleFont.setFontName("楷体");titleStyle.setFont(titleFont);yearCell.setCellStyle(titleStyle);//循环遍历列名
              HSSFCellStyle style5 = workbook.createCellStyle();style5.setFillForegroundColor(HSSFColor.WHITE.index);style5.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style5.setBorderBottom(HSSFCellStyle.BORDER_THIN);style5.setBorderLeft(HSSFCellStyle.BORDER_THIN);style5.setBorderRight(HSSFCellStyle.BORDER_THIN);style5.setBorderTop(HSSFCellStyle.BORDER_THIN);style5.setAlignment(HSSFCellStyle.ALIGN_LEFT);row = sheet.createRow(3);for (int i = 0; i < maxLength; i++) {//i是headers的索引,n是Excel的索引
                  HSSFCell cell1 = row.createCell(i);cell1.setCellStyle(style);sheet.setColumnWidth(i, 10000);HSSFRichTextString text = null;if (i == 0) {sheet.setColumnWidth(0,1500);text = new HSSFRichTextString("序号");}else if(i == 1){sheet.setColumnWidth(1,10000);text = new HSSFRichTextString("项目名称");}else {MonthlyCustom mc = monthlyCustomList.get(i - 2);text = new HSSFRichTextString(mc.getColumnName());}cell1.setCellValue(text);}for(int j = 0;j<maxLength;j++){HSSFCell cell1 = row.getCell(j);if("项目状态".equals(cell1.getStringCellValue())){sheet.setColumnWidth(j,3000);}}dateList = projectInfoDao.querymonthlyList(map);// 遍历集合数据,产生数据行
              // 遍历集合数据,产生数据行
              for (int i = 0, index = 4; i < dateList.size(); i++, index++) {row = sheet.createRow(index);Object[] obj = (Object[]) dateList.get(i);for (int x = 0; x < maxLength; x++) {HSSFCell cell1 = row.createCell((short) x);cell1.setCellStyle(style2);HSSFRichTextString richString = null;if (x == 0) {richString = new HSSFRichTextString(String.valueOf(i + 1));}else if(x == 1){cell1.setCellStyle(style5);richString = new HSSFRichTextString(obj[0] == null ? "" : obj[0].toString());} else {cell1.setCellStyle(style5);richString = new HSSFRichTextString(obj[x] == null ? "" : obj[x].toString());}cell1.setCellValue(richString);}}}}else{maxLength =(monthlyCustomList==null?0:monthlyCustomList.size())+3;// 生成一个表格  sheet名称  汇总数据
          sheet = workbook.createSheet("月报汇总");// 设置表格默认列宽度为15个字节
          sheet.setDefaultColumnWidth((short) 15);//生成title
          firstHeaderRow = sheet.createRow(0);// 第一行
          sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, maxLength-1));yearCell = firstHeaderRow.createCell(0);yearCell.setCellValue(reportMonth+"月报汇总表");titleStyle = workbook.createCellStyle();titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);titleFont = workbook.createFont();titleFont.setFontHeightInPoints((short) 16);titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);titleFont.setFontName("楷体");titleStyle.setFont(titleFont);yearCell.setCellStyle(titleStyle);//循环遍历列名
          HSSFCellStyle style4 = workbook.createCellStyle();style4.setFillForegroundColor(HSSFColor.WHITE.index);style4.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style4.setBorderBottom(HSSFCellStyle.BORDER_THIN);style4.setBorderLeft(HSSFCellStyle.BORDER_THIN);style4.setBorderRight(HSSFCellStyle.BORDER_THIN);style4.setBorderTop(HSSFCellStyle.BORDER_THIN);style4.setAlignment(HSSFCellStyle.ALIGN_LEFT);row = sheet.createRow(3);for (int i = 0; i < maxLength; i++) {//i是headers的索引,n是Excel的索引
              HSSFCell cell1 = row.createCell(i);cell1.setCellStyle(style);//sheet.setColumnWidth(i, 10000);
              HSSFRichTextString text = null;if(i<3){if(i==0){sheet.setColumnWidth(0,1500);text=new HSSFRichTextString("序号");}else if(i==1){sheet.setColumnWidth(1,10000);text=new HSSFRichTextString("项目名称");}else{sheet.setColumnWidth(2,3000);text=new HSSFRichTextString("合作单位");}}else {sheet.setColumnWidth(i,10000);MonthlyCustom mc = monthlyCustomList.get(i - 3);text = new HSSFRichTextString(mc.getColumnName());}cell1.setCellValue(text);}for(int j = 0;j<maxLength;j++){HSSFCell cell1 = row.getCell(j);if("项目状态".equals(cell1.getStringCellValue())){sheet.setColumnWidth(j,3000);}}dateList = projectInfoDao.querymonthlyList(map);// 遍历集合数据,产生数据行
          for (int i = 0, index = 4; i < dateList.size(); i++, index++) {row = sheet.createRow(index);Object [] obj = (Object [])dateList.get(i);for (int x=0;x<maxLength;x++){HSSFCell cell1 = row.createCell((short) x);cell1.setCellStyle(style2);HSSFRichTextString richString=null;if(x==0){richString = new HSSFRichTextString(String.valueOf(i+1));}else if(x == 1){cell1.setCellStyle(style4);richString = new HSSFRichTextString(obj[x-1]==null?"":obj[x-1].toString());}else{richString = new HSSFRichTextString(obj[x-1]==null?"":obj[x-1].toString());}cell1.setCellValue(richString);}}}
return  workbook;}
效果:


这篇关于工作总结之----动态导出数据到Excel,多sheet页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111387

相关文章

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Python实现数据可视化图表生成(适合新手入门)

《Python实现数据可视化图表生成(适合新手入门)》在数据科学和数据分析的新时代,高效、直观的数据可视化工具显得尤为重要,下面:本文主要介绍Python实现数据可视化图表生成的相关资料,文中通过... 目录前言为什么需要数据可视化准备工作基本图表绘制折线图柱状图散点图使用Seaborn创建高级图表箱线图热

MySQL数据脱敏的实现方法

《MySQL数据脱敏的实现方法》本文主要介绍了MySQL数据脱敏的实现方法,包括字符替换、加密等方法,通过工具类和数据库服务整合,确保敏感信息在查询结果中被掩码处理,感兴趣的可以了解一下... 目录一. 数据脱敏的方法二. 字符替换脱敏1. 创建数据脱敏工具类三. 整合到数据库操作1. 创建服务类进行数据库

MySQL中处理数据的并发一致性的实现示例

《MySQL中处理数据的并发一致性的实现示例》在MySQL中处理数据的并发一致性是确保多个用户或应用程序同时访问和修改数据库时,不会导致数据冲突、数据丢失或数据不一致,MySQL通过事务和锁机制来管理... 目录一、事务(Transactions)1. 事务控制语句二、锁(Locks)1. 锁类型2. 锁粒

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心

setsid 命令工作原理和使用案例介绍

《setsid命令工作原理和使用案例介绍》setsid命令在Linux中创建独立会话,使进程脱离终端运行,适用于守护进程和后台任务,通过重定向输出和确保权限,可有效管理长时间运行的进程,本文给大家介... 目录setsid 命令介绍和使用案例基本介绍基本语法主要特点命令参数使用案例1. 在后台运行命令2.