精华内容
下载资源
问答
  • easyexcel设置单元格样式
    千次阅读
    2021-01-26 22:24:56

    导出时注册registerWriteHandler(new RowWriteHandlerImpl())

    /**

    * @author jamin

    * @date 2020/7/29 15:18

    */

    public class RowWriteHandlerImpl implements CellWriteHandler {

    @Override

    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override

    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    //设置单元格格式为文本

    Workbook workbook = writeSheetHolder.getSheet().getWorkbook();

    CellStyle cellStyle = workbook.createCellStyle();

    DataFormat dataFormat = workbook.createDataFormat();

    cellStyle.setDataFormat(dataFormat.getFormat("@"));

    cell.setCellStyle(cellStyle);

    }

    //加@Override会报错

    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override

    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List list, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    }

    更多相关内容
  • 工作中由于有用到excel导出的功能,在设置单元格样式的时候呢也是网上各种找资料,既然问题已经解决了呢! 自然要做一个记录,便于日后翻阅。也供各位同学做参考啦!首先我们添加easyExcel依赖,我们这里选用的版本是...

    工作中由于有用到excel导出的功能,在设置单元格样式的时候呢也是网上各种找资料,既然问题已经解决了呢! 自然要做一个记录,便于日后翻阅。也供各位同学做参考啦!

    首先我们添加easyExcel依赖,我们这里选用的版本是2.1.6

    com.alibaba

    easyexcel

    2.1.6

    废话不多说,直接上代码吧

    package com.xchaset.excelexample.excel.service;

    import com.alibaba.excel.EasyExcel;

    import com.alibaba.excel.ExcelWriter;

    import com.alibaba.excel.support.ExcelTypeEnum;

    import com.alibaba.excel.write.builder.ExcelWriterBuilder;

    import com.alibaba.excel.write.metadata.WriteSheet;

    import com.xchaset.excelexample.excel.model.SimpleCustomerModel;

    import java.util.List;

    public class ExcelWriteService {

    public static void main(String[] args) {

    new ExcelWriteService().writeExcel();

    }

    public void writeExcel(){

    // 读取的excel路径,此处主要是为获取数据源。

    String readPathName = "C:\\Users\\customer.XLS";

    List rows = new ExcelReadService().readExcel(readPathName);

    // 写入的文件路径

    String writePathName = "C:\\Users\\write.xlsx";

    ExcelWriterBuilder writerBuilder = EasyExcel.write(writePathName, SimpleCustomerModel.class);

    writerBuilder.excelType(ExcelTypeEnum.XLSX);

    // 注册写入处理器

    writerBuilder.registerWriteHandler(new CellCommentWriteHandler());

    writerBuilder.registerWriteHandler(new CellStyleWriteHandler());

    ExcelWriter writer = writerBuilder.build();

    WriteSheet writeSheet = new WriteSheet();

    writeSheet.setSheetName("客户信息");

    writer.write(rows, writeSheet);

    //可以写入多个sheet页

    WriteSheet writeSheet2 = new WriteSheet();

    writeSheet2.setSheetName("客户信息2");

    writer.write(rows,writeSheet2);

    // finish才会写入到文件中

    writer.finish();

    }

    /**

    * 多个sheet页写入

    * @param filePath 写出文件路径

    * @param dataMap sheetName:数据集 的Map 集合

    * @param writeHandlers 写入处理器

    */

    public static void writeToExcel(String filePath, Map dataMap, List writeHandlers){

    if (!FileUtil.exist(filePath)) {

    FileUtil.mkParentDirs(filePath);

    }

    ExcelWriterBuilder writerBuilder = EasyExcel.write(filePath);

    writerBuilder.excelType(ExcelTypeEnum.XLSX);

    for (WriteHandler writeHandler : writeHandlers) {

    writerBuilder.registerWriteHandler(writeHandler);

    }

    ExcelWriter writer = writerBuilder.build();

    for(Map.Entry entry :dataMap.entrySet()){

    String sheetName = entry.getKey();

    List rows = entry.getValue();

    WriteSheet writeSheet = new WriteSheet();

    writeSheet.setNeedHead(true);

    writeSheet.setClazz(rows.get(0).getClass());

    writeSheet.setSheetName(sheetName);

    writer.write(rows,writeSheet);

    }

    writer.finish();

    }

    上面代码有提到我们的注册写入处理器

    package com.xchaset.excelexample.excel.service;

    import com.alibaba.excel.metadata.Head;

    import com.alibaba.excel.write.style.AbstractCellStyleStrategy;

    import org.apache.poi.ss.usermodel.*;

    /**

    * 单元格样式写入处理器

    */

    public class CellStyleWriteHandler extends AbstractCellStyleStrategy {

    @Override

    protected void initCellStyle(Workbook workbook) {

    }

    @Override

    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {

    Workbook workbook = cell.getSheet().getWorkbook();

    CellStyle cellStyle = workbook.createCellStyle();

    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    cellStyle.setFillForegroundColor(IndexedColors.CORAL.getIndex());

    cell.setCellStyle(cellStyle);

    }

    @Override

    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {

    Workbook workbook = cell.getSheet().getWorkbook();

    CellStyle cellStyle = workbook.createCellStyle();

    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());

    if ("*".equals(cell.getStringCellValue())) {

    //隐藏行

    cell.getRow().setZeroHeight(true);

    }

    cell.setCellStyle(cellStyle);

    }

    }

    注意上面的代码CellStyleWriteHandler类是继承自AbstractCellStyleStrategy 类,而不是直接实现CellWriteHandler。通过实现AbstractCellStyleStrategy 的setHeadCellStyle可以对头部的样式进行设置,实现setContentCellStyle方法可以对内容进行样式设置。我这里只是简单对单元格的颜色做了设置。

    另外还有一点需要注意的就是由于AbstractCellStyleStrategy 实现了NotRepeatExecutor接口,这个接口只有一个方法uniqueValue(),就是返回一个唯一的值,AbstractCellStyleStrategy 本身已经实现了这个方法。

    所以如果同时有多个类实现了AbstractCellStyleStrategy ,而没有重写uniqueValue()方法,那只会有其中一个会生效。

    我们可以设置多个单元格的处理器。针对需要设置单元格批注的,我们添加了一个CellCommentWriteHandler。这次我们直接实现CellWriteHandler接口。

    package com.xchaset.excelexample.excel.service;

    import com.alibaba.excel.metadata.CellData;

    import com.alibaba.excel.metadata.Head;

    import com.alibaba.excel.write.handler.CellWriteHandler;

    import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;

    import com.alibaba.excel.write.metadata.holder.WriteTableHolder;

    import com.xchaset.excelexample.excel.constant.CellCommentEnum;

    import org.apache.poi.ss.usermodel.Cell;

    import org.apache.poi.ss.usermodel.Comment;

    import org.apache.poi.ss.usermodel.Drawing;

    import org.apache.poi.ss.usermodel.Row;

    import org.apache.poi.xssf.usermodel.XSSFClientAnchor;

    import org.apache.poi.xssf.usermodel.XSSFRichTextString;

    import java.util.List;

    /**

    * 批注写入处理器

    */

    public class CellCommentWriteHandler implements CellWriteHandler {

    @Override

    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override

    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override

    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    addCellComment(cell);

    }

    /**

    * 添加批注信息

    * @param cell

    */

    private void addCellComment(Cell cell) {

    for (CellCommentEnum commentEnum : CellCommentEnum.values()) {

    int rowIndex = commentEnum.getRowIndex();

    int columnIndex = commentEnum.getColumnIndex();

    if (rowIndex == cell.getRowIndex() && columnIndex == cell.getColumnIndex()){

    Drawing> drawing = cell.getSheet().createDrawingPatriarch();

    Comment cellComment = drawing.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, columnIndex, 1, columnIndex + 2, 6));

    cellComment.setString(new XSSFRichTextString(commentEnum.getCellCommentStr()));

    cell.setCellComment(cellComment);

    }

    }

    }

    }

    针对需要添加批注的单元格。新建了一个枚举类,这样我们可以把头部的一些需要添加批注的信息维护在枚举类中。

    package com.xchaset.excelexample.excel.constant;

    import lombok.AllArgsConstructor;

    import lombok.Getter;

    @Getter

    @AllArgsConstructor

    public enum CellCommentEnum {

    CUSTOMER_CODE_COMMENT(1,0,"客户代码"),

    CUSTOMER_NAME_COMMENT(1,1,"客户名称"),

    ADDRESS_COMMENT(1,5,"工作单位地址"),

    LIVING_ADDRESS_COMMENT(1,16,"住宅地址");

    private int rowIndex;

    private int columnIndex;

    private String cellCommentStr;

    }

    下面是model类,我们写入excel的头部信息可以通过注解进行设置。在之前的easyExcel中是需要继承com.alibaba.excel.metadata.BaseRowModel的,不过这个类已经废弃了。所以新版本的话就不在需要了。

    package com.xchaset.excelexample.excel.model;

    import com.alibaba.excel.annotation.ExcelProperty;

    import lombok.Data;

    @Data

    public class SimpleCustomerModel {

    @ExcelProperty(value = {"客户信息","客户代码"})

    private String customerCode;

    @ExcelProperty(value = {"客户信息","客户名称"})

    private String name;

    @ExcelProperty(value = {"客户信息","性别"})

    private String gender;

    @ExcelProperty(value = {"出生信息","年龄"})

    private String age;

    @ExcelProperty(value = {"出生信息","出生日期"})

    private String birthday;

    @ExcelProperty(value = {"地址信息","客户地址"})

    private String address;

    @ExcelProperty(value = {"联系方式","电话"})

    private String tel;

    @ExcelProperty(value = {"联系方式","移动电话"})

    private String mobile;

    @ExcelProperty(value = {"联系方式","邮编"})

    private String postcode;

    @ExcelProperty(value = {"联系方式","邮箱"})

    private String email;

    @ExcelProperty(value = {"工作信息","职业"})

    private String occupation;

    @ExcelProperty(value = {"工作信息","职务"})

    private String job;

    @ExcelProperty(value = {"地址信息","省区代码"})

    private String provinceCode;

    @ExcelProperty(value = {"地址信息","省区名称"})

    private String provinceName;

    @ExcelProperty(value = {"地址信息","市县代码"})

    private String cityCode;

    @ExcelProperty(value = {"地址信息","市县名称"})

    private String cityName;

    @ExcelProperty(value = {"地址信息","住宅区"})

    private String livingAddress;

    }

    展开全文
  • 文章目录 1.全局设置标题和内容字体格式 ...这种单元格有内容时字体才会生效,无内容时还是"常规"格式 private static WriteHandler templateWriteHandler; static { //表头样式 WriteCellStyl

    文章目录

    1.全局设置标题和内容字体格式

    通过WriteCellStyle 的dataFormat属性和BuiltinFormats指定字体格式
    这种单元格有内容时字体才会生效,无内容时还是"常规"格式

        private static WriteHandler templateWriteHandler;
        static {
            //表头样式
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
                //字体
                WriteFont headWriteFont = new WriteFont();
                headWriteFont.setFontHeightInPoints((short) 11);
                headWriteFont.setBold(true);
                headWriteCellStyle.setWriteFont(headWriteFont);
                //边框
                headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
                headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
                headWriteCellStyle.setBorderRight(BorderStyle.THIN);
                //前景色
                headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
                //是否换行
                headWriteCellStyle.setWrapped(true);
                headWriteCellStyle.setLocked(true);
            //表体样式
            WriteCellStyle bodyWriteCellStyle = new WriteCellStyle();
            //设置数据格式索引
            bodyWriteCellStyle.setDataFormat((short)49);
    
            templateWriteHandler = new HorizontalCellStyleStrategy(headWriteCellStyle,bodyWriteCellStyle);
        }
    
    //快速根据索引获取字符串、或根据字符串获取索引
        public static void main(String[] args) {
            int builtinFormat = BuiltinFormats.getBuiltinFormat("h:mm:ss AM/PM");
            System.out.println(builtinFormat);
            String builtinFormat1 = BuiltinFormats.getBuiltinFormat(49);
            System.out.println(builtinFormat1);
        }
    

    记得注册

            excelWriterBuilder.registerWriteHandler(templateWriteHandler);
    

    2.个性化设置某一列格式

    继承 AbstractVerticalCellStyleStrategy ,实现个性化方法 单独设置某一列

    package com.example.easyexceldemo.bo;
    
    import com.alibaba.excel.metadata.Head;
    import com.alibaba.excel.write.metadata.style.WriteCellStyle;
    import com.alibaba.excel.write.metadata.style.WriteFont;
    import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
    import org.apache.poi.ss.usermodel.BorderStyle;
    import org.apache.poi.ss.usermodel.IndexedColors;
    
    public class DemoVerticalCellStyleStrategy extends AbstractVerticalCellStyleStrategy {
        @Override
        protected WriteCellStyle headCellStyle(Head head) {
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            //字体
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 11);
            headWriteFont.setBold(true);
            headWriteCellStyle.setWriteFont(headWriteFont);
            //边框
            headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
            headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            headWriteCellStyle.setBorderRight(BorderStyle.THIN);
            //前景色
            headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            //是否换行
            headWriteCellStyle.setWrapped(true);
            headWriteCellStyle.setLocked(true);
            return headWriteCellStyle;
        }
    
        @Override
        protected WriteCellStyle contentCellStyle(Head head) {
            Integer columnIndex = head.getColumnIndex();
            //这里只单独设置第4列,看情况自己修改,DateFormat取值见 demo1 的 BuiltinFormats 类
            if(3 == columnIndex){
                WriteCellStyle bodyWriteCellStyle = new WriteCellStyle();
                //设置数据格式索引
                bodyWriteCellStyle.setDataFormat((short)49);
                return bodyWriteCellStyle;
            }else {
                return new WriteCellStyle();
            }
        }
    }
    

    记得注册

            excelWriterBuilder.registerWriteHandler(templateWriteHandler);
    

    3.无内容时 (预制模板,流形式写会)

    其实我遇到的场景,就只是简单的空白模板,网上找了好多为无内容excel设置文本格式的资料,都没有解决。后来干脆把模板上传到resource。
    在这里插入图片描述

    在这里插入图片描述

        @GetMapping("/invoiceTemplateDownload2")
        public void templateDownload2(HttpServletResponse response) throws IOException {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("模板", "UTF-8").replaceAll("\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    
            byte[] fileToByteArray = FileUtils.readFileToByteArray(new File("src/main/resources/invoiceTemplate.xlsx"));
            response.getOutputStream().write(fileToByteArray);
    
        }
    
    展开全文
  • easyExcel可以实现斜线的效果吗?如果可以,请问如何实现
  • easyExcel自定义单元格配置依赖对象写excel导出excel方法实现CellWriteHandler接口读excel 配置 依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel --> <dependency> <groupId...

    配置

    依赖

     <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>2.2.11</version>
            </dependency>
    

    对象

    @Data
    @Accessors(chain = true)
    @HeadFontStyle(fontName = "等线", fontHeightInPoints = 10)
    public class ApproveList {
    
        @ExcelProperty(value = "approveResult",index = 0)
        private String approveResult;
    
        private String remark;
    
        private String id;
    
        private String pn;
    
        private String detail;
     }
    

    写excel

    导出excel方法

    public void download(HttpServletResponse response,ApproveList approveList){
           String fileName =
                    URLEncoder.encode(
                            ("ApproveList"  + dateFormat.format(new Date())), "UTF-8");
    
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            
            //设置approveResult为下拉选择框,设置选择框的枚举值
            approveList.getApproveResult();
            HashMap<Integer, String[]> dropDownMap = new HashMap<>();
            dropDownMap.put(0,new String[]{"Approve","Reject"});
    
            //设置哪些列不可编辑,字段索引
            List<Integer> columnIndex = new ArrayList<>();
            for (int i = 2; i < 4; i++) {
                columnIndex.add(i);
            }
    
            try {
                EasyExcel.write(response.getOutputStream(), ApproveList.class).
                        registerWriteHandler(new CustomCellWriteHandler())
                        .registerWriteHandler(new ContentCellWriteHandler(columnIndex,dropDownMap))
                        .sheet("ApproveList").doWrite(approveList);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    

    实现CellWriteHandler接口

    public class ContentCellWriteHandler implements CellWriteHandler {
    
        private List<Integer> columnIndexs;
        private Short colorIndex;
        private HashMap<Integer,String> annotationsMap;
        private HashMap<Integer,String[]> dropDownMap;
    
        public ContentCellWriteHandler(List<Integer> columnIndexs, Short colorIndex, HashMap<Integer,String> annotationsMap){
            this.columnIndexs=columnIndexs;
            this.colorIndex=colorIndex;
            this.annotationsMap=annotationsMap;
        }
    
        public ContentCellWriteHandler(List<Integer> columnIndexs, Short colorIndex, HashMap<Integer,String> annotationsMap, HashMap<Integer,String[]> dropDownMap){
            this.columnIndexs=columnIndexs;
            this.colorIndex=colorIndex;
            this.annotationsMap=annotationsMap;
            this.dropDownMap=dropDownMap;
        }
    
        public ContentCellWriteHandler(List<Integer> columnIndexs, HashMap<Integer,String[]> dropDownMap){
            this.columnIndexs=columnIndexs;
            this.dropDownMap=dropDownMap;
        }
    
        @Override
        public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {}
    
        @Override
        public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {}
    
        @Override
        public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {}
    
        @Override
        public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean isHead) {
            if (!isHead){
                Sheet sheet = writeSheetHolder.getSheet();
    
                if (CollectionUtils.isNotEmpty(columnIndexs)){
                    Workbook workbook = sheet.getWorkbook();
    //                CellStyle cellStyle1 = StyleUtil.buildHeadCellStyle(workbook, writeFont);
                    /* 这里要重新create不锁定的列样式 */
                    CellStyle unlockCellStyle = workbook.createCellStyle();
                    unlockCellStyle.setLocked(false);//默认是锁定状态;将所有单元格设置为:未锁定;然后再对需要上锁的单元格单独锁定
                    cell.setCellStyle(unlockCellStyle);
                    if (colorIndex != null && columnIndexs.contains(cell.getColumnIndex())){
                        WriteCellStyle writeCellStyle = new WriteCellStyle();
                        WriteFont writeFont = new WriteFont();
                        writeFont.setColor(colorIndex);
                        writeCellStyle.setWriteFont(writeFont);
                    }
                    /* 这里可以根据需要进行判断;我这就将columnIndexs列上锁了 */
                    if (cell.getColumnIndex() != 1 && columnIndexs.contains(cell.getColumnIndex())){
                        // 设置表单保护密码
                        writeSheetHolder.getSheet().protectSheet("password");
                        // 设置锁定单元格
    //                    CellStyle cellLockStyle = StyleUtil.buildHeadCellStyle(workbook, writeFont);
                        /*  这里要重新create列锁定的列样式,*/
                        CellStyle cellLockStyle = workbook.createCellStyle();
                        cellLockStyle.setLocked(true);
                        cellLockStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                        cellLockStyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
                        cellLockStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                        cellLockStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
                        cellLockStyle.setBorderBottom(BorderStyle.THIN);
                        cellLockStyle.setBorderTop(BorderStyle.THIN);
                        cellLockStyle.setBorderLeft(BorderStyle.THIN);
                        cellLockStyle.setBorderRight(BorderStyle.THIN);
                        cellLockStyle.setWrapText(true);
                        cell.setCellStyle(cellLockStyle);
                    }
    
                }
                if (annotationsMap != null && annotationsMap.containsKey(cell.getColumnIndex())){
                    String context = annotationsMap.get(cell.getColumnIndex());
                    Comment cellComment =sheet.createDrawingPatriarch().createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), 0, (short) 5, 5));
                    cellComment.setString(new XSSFRichTextString(context));
                    cell.setCellComment(cellComment);
                }
                //设置下拉框
                if (dropDownMap !=null && dropDownMap.containsKey(cell.getColumnIndex())){
                    String[] datas = dropDownMap.get(cell.getColumnIndex());
                    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
                    DataValidationConstraint dvConstraint = dataValidationHelper.createExplicitListConstraint(datas);
                    CellRangeAddressList addressList = null;
                    DataValidation validation = null;
                    for (int i = 1; i < 1000; i++) {
                        addressList = new CellRangeAddressList(i, i, cell.getColumnIndex(), cell.getColumnIndex());
                        validation = dataValidationHelper.createValidation(
                                dvConstraint, addressList);
                        sheet.addValidationData(validation);
                    }
                }
            }
        }
    }
    
    • cellLockStyle.setLocked(true);设置为不可编辑,但导出后发现所有单元格均不可编辑:
      • 因为createCell();创建单元格后,单元格默认是锁定状态;
      • protectSheet(“密码”);保护工作表是保护所有锁定的单元格;

    导出效果:
    在这里插入图片描述

    读excel

    对象中设置@Accessors(chain = true),easyExcel能读取行数据,但是解析不了,也就是映射不了相应的类。

    展开全文
  • } /** * 设置底纹信息 * * @param style 单元格样式 * @param cellShadingModel 底纹信息 */ private void setCellShading(XSSFCellStyle style, CellShadingModel cellShadingModel) { //背景颜色 Object ...
  • EasyExcel使用及自定义设置单元格样式
  • 关于使用EasyExcel进行单元格合并的问题 1.项目场景: 简介:报销单导出要根据指定的excel模板去自动替换对应,然后重新生成一份新的excel。在给定的excel模板中,有部分字段进行了单元格合并,如下所示。 2.问题...
  • <artifactId>lombok</artifactId> <optional>true</optional> </dependency> 2、CellStyleModel 样式信息类。 package com.easyexcel.model; import cn.hutool.core.util.StrUtil; import lombok.Data; import org....
  • easyExcel 导出时设置样式、列宽策略。
  • 自定义EasyExcel单元格合并策略,动态合并单元格。
  • easyExcel设置单个单元格(颜色)样式

    万次阅读 2019-07-26 19:01:32
    背景:需求是使用excel设置目标单元格的样式(颜色),但我之前没有学过easyExcel,在网上找资料的时候,发现有关easyExcel相关的单个单元格样式设置的资料比较少,有的还源码不全,只能说用来参考。我的代码很多一...
  • 比如这种情况,有那么部分时候,客户的要求那是相当的十分刁钻,平时的导出如果是用easyExcel的话,就需要写自定义拦截器再引入poi富文本来修改了。 新建类实现CellWriteHandler接口: public class ...
  • EasyExcel设置行中单个单元格样式

    千次阅读 多人点赞 2020-08-15 17:54:46
    1. 前言 在近期的工作中遇到一个导出Excel的需求: ...导出Excel文件的技术使用的是阿里巴巴提供的一个EasyExcel工具,有关easyExcel相关的单个单元格样式设置的资料比较少,自己也是查阅了大量的资料,最后也是在尝
  • EasyExcel实现自定义单元格样式导出

    千次阅读 2021-03-25 10:10:16
    EasyExcel实现自定义单元格样式导出 需求: 计划逾期数据进行标红 导出样式: 1. 引入依赖 <!--建议不要选用此版本以下的依赖--> <dependency> <groupId>com.alibaba</groupId> <...
  • 单元格字体格式设置

    2013-12-31 10:57:08
    这是一个Exel单元格字体格式设置的方法,需解压
  • EasyExcel非模板导出,根据数据类型设置单元格数据格式
  • easyexcel 合并单元格(非注解)

    万次阅读 热门讨论 2020-09-16 14:02:27
    --引入 阿里的 easyexcel 同时需要引入 asm 或者 cglib--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.4</...
  • 1 Maven配置 cn.hutool hutool-all 5.5.1 com.alibaba easyexcel 2.2.8 org.projectlombok lombok true 2 调试代码 /** * 导出(单元格... EasyExcel 批量设置单元格样式(字体样式、背景颜色)
  • 1 Maven配置 cn.hutool hutool-all 5.5.1 com.alibaba easyexcel 2.2.8 org.projectlombok lombok true 2 ... EasyExcel 批量设置单元格样式(字体样式、背景颜色、边框样式、对齐方式、自动换行)
  • easyExcel模板填充实现动态模板生成
  • easyexcel官方文档中可以设置表头单元格格式及内容的单元格格式,其中单元格格式策略类中有两个构造函数 public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, List<WriteCellStyle> ...
  • easyExcel由于导出和导入的注解差异,所以实现不了标题功能 此处不在介绍导出导出的注解 entity: @DropDownSetField自行封装注解:稍后解释 public class SalesOrderExportExcel { private static final ...
  • 本人在开发中用到Easyexcel的第三方jar包,遇到需要单独设置表头单元格每一格的样式的情况。 1.官方文档给出的 HorizontalCellStyleStrategy只能实现把整个表头的样式设置为一样的。 2.而注解方式并不适合动态导出...
  • import java.util.Iterator; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle;...import org.apache.poi.ss.usermodel....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,071
精华内容 428
热门标签
关键字:

easyexcel设置单元格样式

友情链接: Lnux.rar