You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Apache POI设置Excel图例的外部阴影预设?

Apache POI 设置Excel图表图例格式

以下是使用Apache POI(针对XSSF格式Excel)设置图表图例格式的实操方案:

核心操作步骤与代码示例

1. 获取图例对象

通过图表实例的getLegend()方法获取XSSFLegend,这是所有图例格式设置的入口:

XSSFChart chart = ...; // 已创建好的图表对象
XSSFLegend legend = chart.getLegend();

2. 设置基础属性

图例位置设置

支持多种预设位置,比如靠右、顶部、底部等:

// 设置图例靠右显示
legend.setPosition(LegendPosition.RIGHT);
// 其他可选值:TOP, BOTTOM, LEFT, TOP_RIGHT, BOTTOM_RIGHT等

图例可见性控制

需要隐藏图例时直接调用:

legend.setVisible(false);

3. 样式自定义

字体格式设置

创建XSSFFont配置字体样式后,应用到图例:

XSSFFont legendFont = workbook.createFont();
legendFont.setFontName("微软雅黑");
legendFont.setFontHeightInPoints((short)10);
legendFont.setBold(true);
legend.setFont(legendFont);

背景填充设置

给图例添加纯色背景:

legend.getFillProperties().setFillType(FillType.SOLID_FILL);
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(new byte[]{(byte)240, (byte)240, (byte)240}));
legend.getFillProperties().setSolidFill(fill);

边框样式设置

自定义图例的边框颜色和宽度:

XDDFLineProperties border = new XDDFLineProperties();
border.setFillProperties(new XDDFSolidFillProperties(XDDFColor.from(new byte[]{(byte)128, (byte)128, (byte)128})));
border.setWidth(0.75);
legend.setLineProperties(border);

完整示例代码

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class ChartLegendDemo {
    public static void main(String[] args) throws IOException {
        try (XSSFWorkbook workbook = new XSSFWorkbook()) {
            XSSFSheet sheet = workbook.createSheet("ChartDemo");
            // 生成测试数据
            initTestData(sheet);

            // 创建图表锚点与图表对象
            XSSFDrawing drawing = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 1, 15, 30);
            XSSFChart chart = drawing.createChart(anchor);

            // 初始化柱状图数据
            XDDFChartData barData = chart.createData(ChartTypes.BAR, null, null);
            XDDFCategoryAxis categoryAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
            XDDFValueAxis valueAxis = chart.createValueAxis(AxisPosition.LEFT);
            valueAxis.setCrosses(AxisCrosses.AUTO_ZERO);

            XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 4, 0, 0));
            XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 4, 1, 1));
            barData.addSeries(categories, values);
            chart.plot(barData);

            // 开始设置图例格式
            XSSFLegend legend = chart.getLegend();
            legend.setPosition(LegendPosition.RIGHT);

            // 设置字体
            XSSFFont legendFont = workbook.createFont();
            legendFont.setFontName("Arial");
            legendFont.setFontHeightInPoints((short)10);
            legendFont.setBold(true);
            legend.setFont(legendFont);

            // 设置背景填充
            legend.getFillProperties().setFillType(FillType.SOLID_FILL);
            XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(new byte[]{(byte)245, (byte)245, (byte)245}));
            legend.getFillProperties().setSolidFill(fill);

            // 设置边框
            XDDFLineProperties border = new XDDFLineProperties();
            border.setFillProperties(new XDDFSolidFillProperties(XDDFColor.from(new byte[]{(byte)192, (byte)192, (byte)192})));
            border.setWidth(0.5);
            legend.setLineProperties(border);

            // 保存文件
            try (FileOutputStream fos = new FileOutputStream("formatted_chart.xlsx")) {
                workbook.write(fos);
            }
        }
    }

    private static void initTestData(XSSFSheet sheet) {
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("分类");
        header.createCell(1).setCellValue("数值");

        String[] categories = {"A", "B", "C", "D"};
        double[] values = {15, 22, 18, 25};
        for (int i = 0; i < categories.length; i++) {
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(categories[i]);
            row.createCell(1).setCellValue(values[i]);
        }
    }
}

注意事项

  • 上述代码基于Apache POI 4.1.2版本,不同版本的API可能存在细微差异,建议使用稳定的最新版本
  • 若处理HSSF格式(.xls),需替换为HSSFLegend等对应类,核心逻辑一致

内容的提问来源于stack exchange,提问作者NCToan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:25:59