工作总结之----动态导出数据到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

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt