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
相关文章
|
5天前
|
Java 关系型数据库 MySQL
【MySQL × SpringBoot 突发奇想】全面实现流程 · xlsx文件,Excel表格导入数据库的接口(下)
【MySQL × SpringBoot 突发奇想】全面实现流程 · xlsx文件,Excel表格导入数据库的接口
20 0
|
5天前
|
Java 关系型数据库 MySQL
【MySQL × SpringBoot 突发奇想】全面实现流程 · xlsx文件,Excel表格导入数据库的接口(上)
【MySQL × SpringBoot 突发奇想】全面实现流程 · xlsx文件,Excel表格导入数据库的接口
26 0
|
5天前
aspose实现word,excel等文件预览
aspose实现word,excel等文件预览
|
5天前
|
Java
java导出复杂excel
java导出复杂excel
|
2天前
|
JSON Rust 前端开发
【sheetjs】纯前端如何实现Excel导出下载和上传解析?
本文介绍了如何使用`sheetjs`的`xlsx`库在前端实现Excel的导出和上传。项目依赖包括Vite、React、SheetJS和Arco-Design。对于导出,从后端获取JSON数据,通过`json_to_sheet`、`book_new`和`writeFile`函数生成并下载Excel文件。对于上传,使用`read`函数将上传的Excel文件解析为JSON并发送至后端。完整代码示例可在GitHub仓库[fullee/sheetjs-demo](https://github.com/fullee/sheetjs-demo)中查看。
33 10
|
4天前
|
Java Apache 索引
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
|
4天前
|
开发框架 资源调度 JavaScript
uniapp本地导出表格excel
uniapp本地导出表格excel
|
5天前
|
前端开发 关系型数据库 MySQL
【MySQL × SpringBoot 突发奇想】全面实现流程 · 数据库导出Excel表格文件的接口
【MySQL × SpringBoot 突发奇想】全面实现流程 · 数据库导出Excel表格文件的接口
28 0
|
5天前
|
Python
python如何读取excel文件,并修改内容?
python如何读取excel文件,并修改内容?
22 0
|
5天前
|
XML 存储 安全
基于 ABAP OLE 和 abap2xlsx 两种技术方案对 Excel 文件进行读写的优缺点比较
基于 ABAP OLE 和 abap2xlsx 两种技术方案对 Excel 文件进行读写的优缺点比较
5 0