easyExcel 用XSSF画斜线

注册拦截器

WriteSheet writeSheet = EasyExcel.writerSheet(0, "sheet1")
                    .registerWriteHandler(new CustomCellWriteHandler())
                    .head(header).build();
            excelWriter.write(collect, writeSheet);

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;


@Slf4j
public class CustomCellWriteHandler implements CellWriteHandler {

    @Override
    public void afterCellCreate(CellWriteHandlerContext context) {
        Cell cell = context.getCell();

        if (cell != null) {
            // 这里可以对cell进行任何操作
            log.info("第{}行,第{}列写入完成。{}", cell.getRowIndex(), cell.getColumnIndex(), cell.getStringCellValue());
            if (cell.getColumnIndex() == 0 && cell.getRowIndex() == 1) {

                Sheet sheet = context.getWriteSheetHolder().getSheet();
                XSSFDrawing drawingPatriarch = (XSSFDrawing) sheet.createDrawingPatriarch();

                ClientAnchor anchor = drawingPatriarch.createAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 0, 0);
                anchor.setCol1(0);
                anchor.setRow1(0);
                anchor.setCol2(1);
                anchor.setRow2(1);
                XSSFSimpleShape simpleShape = drawingPatriarch.createSimpleShape((XSSFClientAnchor) anchor);
                simpleShape.setShapeType(ShapeTypes.LINE);

                // 设置线宽
                simpleShape.setLineWidth(0.5);
                // 设置线的风格
                simpleShape.setLineStyle(0);
                // 设置线的颜色
                simpleShape.setLineStyleColor(0, 0, 0);
            }
        }
    }
}

我这是第一行第一列画斜线的
easyExcel 用XSSF画斜线_第1张图片
斜线画成了

你可能感兴趣的:(java)