栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

POI Excel插入线条(直线、斜线)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

POI Excel插入线条(直线、斜线)

目录

1 Maven依赖

2 代码实现

3 调试代码

4 调试结果

 注:


1 Maven依赖
        
        
            com.alibaba
            easyexcel
            2.2.8
        
        
        
            cn.hutool
            hutool-all
            5.5.7
        

2 代码实现
    
    public static void insertLine(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex
            , int endColIndex) {
        insertLine(workbook, sheet, beginRowIndex, endRowIndex, beginColIndex, endColIndex, POIUtil.getXSSFRGBColor(0, 0, 0)
                , LineStyleConstant.INT_SOLID, 0.5d);
    }

    
    public static void insertLine(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex, int endColIndex
            , Color lineColor, int lineStyle, double lineWidth) {
        insertShape(workbook, sheet, beginRowIndex, endRowIndex, beginColIndex, endColIndex, lineColor, lineStyle, lineWidth
                , ShapeTypes.LINE);
    }

    
    public static void insertShape(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex, int endColIndex
            , Color lineColor, int lineStyle, double lineWidth, int shapeType) {
        CreationHelper helper = workbook.getCreationHelper();
        //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
        Drawing drawing = sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }
        ClientAnchor anchor = helper.createClientAnchor();
        // 设置线条的开始位置
        anchor.setCol1(beginColIndex);
        anchor.setRow1(beginRowIndex);
        // 设置线条的结束位置
        anchor.setCol2(endColIndex);
        anchor.setRow2(endRowIndex);
        if (drawing instanceof XSSFDrawing) {
            XSSFSimpleShape shape = ((XSSFDrawing) drawing).createSimpleShape((XSSFClientAnchor) anchor);
            // 设置形状类型
            shape.setShapeType(shapeType);
            // 设置线宽
            shape.setLineWidth(lineWidth);
            // 设置线的风格
            shape.setLineStyle(lineStyle - 1);
            // 设置线的颜色
            XSSFColor xssfColor = (XSSFColor) lineColor;
            byte[] rgbBytes = xssfColor.getRGB();
            if (rgbBytes == null || rgbBytes.length < 3) {
                shape.setLineStyleColor(0, 0, 0);
            } else {
                shape.setLineStyleColor(rgbBytes[0], rgbBytes[1], rgbBytes[2]);
            }
        }
    }

3 调试代码
    
    @Test
    public void testInsertLine(){
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            File file = new File("D:/easyexcel/testInsertLine.xlsx");
            FileUtil.createNewFile(file);
            //插入线条
            POIExcelUtil.insertLine(workbook,sheet,1,2,0,4);
            workbook.write(new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4 调试结果

 注:

(1)只支持07版的Excel文档。

(2)源码请查看Gitee。

xudongbase: 主要是项目中可以用到的共通方法https://gitee.com/xudong_master/xudongbase

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785525.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号