easyexcel v3.1.1 如何在指定行插入list?

ukxgm1gy  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(39)

问题描述

现在有个excel样式

类似这种的。使用easyExcel应该如何处理?

hpcdzsge

hpcdzsge1#

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.ArrayList;
import java.util.List;

public class ExcelStyleProcessor {

    public static void main(String[] args) {
        // 读取Excel文件并处理样式
        processExcelWithStyle("input.xlsx", "output.xlsx");
    }

    public static void processExcelWithStyle(String inputFileName, String outputFileName) {
        // 读取Excel文件并处理样式
        List<List<Object>> data = readExcelWithStyle(inputFileName);

        // 创建Excel写入器
        ExcelWriterBuilder writerBuilder = EasyExcel.write(outputFileName);

        // 设置写入样式
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        WriteFont writeFont = new WriteFont();
        writeFont.setBold(true);
        writeFont.setFontHeightInPoints((short) 12);
        writeCellStyle.setWriteFont(writeFont);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        writeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        writeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        writeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

        // 写入Excel文件
        writerBuilder.registerWriteHandler(new CustomCellStyleStrategy(writeCellStyle));
        writerBuilder.sheet().doWrite(data);
    }

    public static List<List<Object>> readExcelWithStyle(String fileName) {
        // 使用EasyExcel读取Excel文件
        ExcelReaderBuilder readerBuilder = EasyExcel.read(fileName, new CustomDataListener());
        readerBuilder.sheet().headRowNumber(0).doRead();
        return CustomDataListener.getData();
    }
}

class CustomCellStyleStrategy extends AbstractCellStyleStrategy {

    public CustomCellStyleStrategy(WriteCellStyle writeCellStyle) {
        super(writeCellStyle);
    }

    @Override
    protected void setContentStyle(List<List<String>> head, List<List<Object>> dataList, List<List<String>> content,List<Integer> excludeColumnIndexes) {
        for (List<String> row : content) {
            for (int i = 0; i < row.size(); i++) {
                // 设置样式
                rowStyle(i);
            }
        }
    }

    private void rowStyle(int i){
        // 设置不同列的样式
    }
}

class CustomDataListener extends AnalysisEventListener<Object> {

    private static List<List<Object>> data = new ArrayList<>();

    @Override
    public void invoke(Object object, AnalysisContext context) {
        // 处理每行数据
        data.add((List<Object>) object);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 所有数据解析完成后的处理
    }

    public static List<List<Object>> getData() {
        return data;
    }
}

相关问题