excel合并列导出文件

简介: excel合并列导出文件

image.png

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>

@Log(title = "字典类型", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:dict:export')")
@PostMapping("/export")
public void export(HttpServletResponse response, SysDictType dictType) throws IOException
{
List list = dictTypeService.selectDictTypeList(dictType);
//List pilebodysList = pilebodycheckService.pilebodystatisticsmonth(sysDepartDto, month);
//设置序号
for (int i = 1;i <= list.size();i++){
list.get(i-1).setDictId(Long.valueOf(i));
}
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// response.setContentType("application/vnd.ms-excel");
// response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("存量建筑垃圾堆体治理进度月报表", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//内容样式策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//垂直居中,水平居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置 自动换行
contentWriteCellStyle.setWrapped(true);
// 字体策略
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short) 12);
contentWriteCellStyle.setWriteFont(contentWriteFont);
//头策略使用默认
WriteCellStyle headWriteCellStyle = new WriteCellStyle();

    //excel如需下载到本地,只需要将response.getOutputStream()换成File即可(注释掉以上response代码)
    EasyExcel.write(response.getOutputStream(), SysDictType.class)
            //设置输出excel版本,不设置默认为xlsx
           // .excelType(ExcelTypeEnum.XLS).head(SysDictType.class)
            //设置拦截器或自定义样式
            .registerWriteHandler(new MonthSheetWriteHandler())
            .registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle))
            .sheet("存量建筑垃圾堆体治理进度月报表")
            //设置默认样式及写入头信息开始的行数
            .useDefaultStyle(true).relativeHeadRowIndex(1)
            //这里的addsumColomn方法是个添加合计的方法,可删除
            .doWrite(list/*pilebodycheckService.addSumColomn(pilebodysList)*/);
   // return new WebApiResponse(200, "生成excel文件成功", null);
}

package com.AAA.web.ExcelUtil;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

public class MonthSheetWriteHandler implements SheetWriteHandler {

@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

}

@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
    Workbook workbook = writeWorkbookHolder.getWorkbook();
    Sheet sheet = workbook.getSheetAt(0);
/*    Row row1 = sheet.createRow(0);
    row1.setHeight((short) 500);
    Cell cell = row1.createCell(0);
    //设置单元格内容
    cell.setCellValue("附件2");*/
    //设置标题
    Row row2 = sheet.createRow(0);
    row2.setHeight((short) 800);
    Cell cell1 = row2.createCell(0);
    cell1.setCellValue("存量建筑垃圾堆体治理进度月报表");
    CellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    cellStyle.setAlignment(HorizontalAlignment.CENTER);
    Font font = workbook.createFont();
    font.setBold(true);
    font.setFontHeight((short) 400);
    cellStyle.setFont(font);
    cell1.setCellStyle(cellStyle);
    sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, 17));
    //设置填表日期,填报人,联系方式
/*    Row row3 = sheet.createRow(2);
    row3.setHeight((short) 500);*/

/ row3.createCell(1).setCellValue("填表日期");
row3.createCell(11).setCellValue("填表人");
row3.createCell(15).setCellValue("联系方式");
/

}
}

package com.AAA.common.core.domain.entity;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.AAA.common.annotation.Excel;
import com.AAA.common.annotation.Excel.ColumnType;
import com.AAA.common.core.domain.BaseEntity;

import java.util.Date;

/**

  • 字典类型表 sys_dict_type
    *
  • @author ruoyi
    /
    @Data
    @ToString
    @NoArgsConstructor
    @Accessors(chain = true)
    @ContentRowHeight(45)
    @HeadRowHeight(50)
    public class SysDictType /
    extends BaseEntity*/
    {
    private static final long serialVersionUID = 1L;

    /* 字典主键 /
    @ExcelProperty(value = {"字典主键","字典主键"},index = 0)
    @ColumnWidth(30)

    private Long dictId;

    /* 字典名称 /
    @ExcelProperty(value = {"字典名称","字典名称"},index = 1)
    @ColumnWidth(30)

    private String dictName;

    /* 字典类型 /
    // @Excel(name = "字典类型")
    @ExcelProperty(value = {"字典类型","字典类型"},index = 2)
    @ColumnWidth(30)

    private String dictType;

    /* 状态(0正常 1停用) /
    @ExcelProperty(value = {"合并列","状态"},index = 3)
    private String status;

    /* 创建时间 /
    @ExcelProperty(value = {"合并列","创建时间"},index = 4)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(30)

    private Date createTime;

    /* 备注 /
    @ExcelProperty(value = {"合并列","备注"},index = 5)
    @ColumnWidth(30)

    private String remark;

    public Long getDictId()
    {

     return dictId;
    

    }

    public void setDictId(Long dictId)
    {

     this.dictId = dictId;
    

    }
    // @ExcelProperty(value = {"字典类型","字典类型"},index = 0)

    @NotBlank(message = "字典名称不能为空")
    @Size(min = 0, max = 100, message = "字典类型名称长度不能超过100个字符")
    public String getDictName()
    {

     return dictName;
    

    }

    public void setDictName(String dictName)
    {

     this.dictName = dictName;
    

    }

    @NotBlank(message = "字典类型不能为空")
    @Size(min = 0, max = 100, message = "字典类型类型长度不能超过100个字符")
    @Pattern(regexp = "^[a-z][a-z0-9_]*$", message = "字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)")
    public String getDictType()
    {

     return dictType;
    

    }

}

package com.AAA.common.core.domain.entity;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.AAA.common.annotation.Excel;
import com.AAA.common.annotation.Excel.ColumnType;
import com.AAA.common.core.domain.BaseEntity;

import java.util.Date;

/**

  • 字典类型表 sys_dict_type
    *
  • @author ruoyi
    /
    @Data
    @ToString
    @NoArgsConstructor
    @Accessors(chain = true)
    @ContentRowHeight(45)
    @HeadRowHeight(50)
    public class SysDictType /
    extends BaseEntity*/
    {
    private static final long serialVersionUID = 1L;

    /* 字典主键 /
    @ExcelProperty(value = {"字典主键","字典主键"},index = 0)
    @ColumnWidth(30)

    private Long dictId;

    /* 字典名称 /
    @ExcelProperty(value = {"字典名称","字典名称"},index = 1)
    @ColumnWidth(30)

    private String dictName;

    /* 字典类型 /
    // @Excel(name = "字典类型")
    @ExcelProperty(value = {"字典类型","字典类型"},index = 2)
    @ColumnWidth(30)

    private String dictType;

    /* 状态(0正常 1停用) /
    @ExcelProperty(value = {"合并列","状态"},index = 3)
    private String status;

    /* 创建时间 /
    @ExcelProperty(value = {"合并列","创建时间"},index = 4)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(30)

    private Date createTime;

    /* 备注 /
    @ExcelProperty(value = {"合并列","备注"},index = 5)
    @ColumnWidth(30)

    private String remark;

    public Long getDictId()
    {

     return dictId;
    

    }

    public void setDictId(Long dictId)
    {

     this.dictId = dictId;
    

    }
    // @ExcelProperty(value = {"字典类型","字典类型"},index = 0)

    @NotBlank(message = "字典名称不能为空")
    @Size(min = 0, max = 100, message = "字典类型名称长度不能超过100个字符")
    public String getDictName()
    {

     return dictName;
    

    }

    public void setDictName(String dictName)
    {

     this.dictName = dictName;
    

    }

    @NotBlank(message = "字典类型不能为空")
    @Size(min = 0, max = 100, message = "字典类型类型长度不能超过100个字符")
    @Pattern(regexp = "^[a-z][a-z0-9_]*$", message = "字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)")
    public String getDictType()
    {

     return dictType;
    

    }

}

/**

  • 开始时间
    excel中不显示 /
    @ExcelIgnore
相关文章
|
17天前
|
Java 数据库 数据安全/隐私保护
Java操作Excel文件导入导出【内含有 jxl.jar 】
Java操作Excel文件导入导出【内含有 jxl.jar 】
24 0
|
18天前
|
easyexcel Java API
SpringBoot集成EasyExcel 3.x:高效实现Excel数据的优雅导入与导出
SpringBoot集成EasyExcel 3.x:高效实现Excel数据的优雅导入与导出
59 1
|
18天前
|
API Python
Python库`openpyxl`是一个用于读取和写入Excel 2010 xlsx/xlsm/xltx/xltm文件的库。
【6月更文挑战第19天】`openpyxl`是Python处理xlsx文件的库,支持读写Excel 2010格式。使用`pip install openpyxl`安装。基本操作包括加载文件、读写单元格、操作行和列。例如,加载Excel后,可以读取单元格`A1`的值,或将“Hello, World!”写入`A1`。还可修改单元格内容,如加1后保存到新文件。更多功能,如样式和公式,见官方文档[1]。 [1]: &lt;https://openpyxl.readthedocs.io/en/stable/&gt;
38 1
|
19天前
|
JSON 资源调度 JavaScript
蓝易云 - vue实现导出excel的多种方式
以上两种方式都可以实现在Vue中导出Excel的功能,你可以根据你的需求选择合适的方式。
21 1
|
23天前
|
JavaScript 前端开发
JS导出excel功能
JS导出excel功能
|
29天前
|
XML Java 测试技术
《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)
【6月更文挑战第8天】本文介绍了Java中操作Excel的工具,包括POI和JXL。POI支持处理Office 2003及以下的OLE2格式(.xls)和2007以上的OOXML格式(.xlsx)。而JXL只能处理2003版本的Excel文件。文章详细讲解了如何下载和使用JXL库,并给出了一个简单的Java代码示例,展示如何读取2003版Excel文件中的数据。在实际项目中,由于JXL对新版本Excel的支持限制,通常推荐使用POI。
38 5
|
5天前
|
JSON JavaScript 数据格式
vue 电子表格Excel的上传导入、导出下载、读取本地Excel、json转Excel
vue 电子表格Excel的上传导入、导出下载、读取本地Excel、json转Excel
7 0
|
28天前
|
Java 测试技术 Apache
《手把手教你》系列技巧篇(六十八)-java+ selenium自动化测试 - 读写excel文件 - 下篇(详细教程)
【6月更文挑战第9天】本文介绍了如何使用Java处理Excel文件中的不同数据类型,包括日期、数字、布尔值和标签(常规字符串)。文章提供了两个示例,分别使用JXL库和Apache POI库来读取Excel文件。
22 1
|
29天前
|
C#
【C#】C#读写Excel文件
【C#】C#读写Excel文件
23 1
|
1月前
|
Java 测试技术 Apache
《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)
【6月更文挑战第7天】本文介绍了在Java自动化测试中如何操作Excel数据。文章提到了当测试数据存储在Excel文件时,可以使用Apache的POI库来读写Excel。POI提供了对OLE2(.xls)和OOXML(.xlsx)格式的支持,比JXL库功能更全面。文章还详细讲解了如何下载和添加POI库到项目中,以及准备测试用的Excel文件。最后,给出了一个简单的Java代码示例,演示如何读取Excel文件的内容。
31 1