如何在 xlLine ChartType 中使用 Y 轴的字符串值

How to use string values for Y-Axis in xlLine ChartType

我需要使用 excel.interop 生成一些折线图,我想为 Y 轴值设置文本。

Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 1);
data.Add("def", 2);
data.Add("ghi", 3);
data.Add("jkl", 4);
data.Add("mno", 5);

var application = new excel.Application();
var workbook = application.Workbooks.Open(docName);
var worksheet = workbook.Worksheets[1] as excel.Worksheet;
var charts = worksheet.ChartObjects() as excel.ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300);
var chart = chartObject.Chart;
chart.ChartType = excel.XlChartType.xlLine;
chart.Location(excel.XlChartLocation.xlLocationAsObject, worksheetName);
var seriesCollection = (excel.SeriesCollection)chart.SeriesCollection();
var series = seriesCollection.NewSeries();

series.Values = data.Keys.ToArray();
series.XValues = data.Values.ToArray();

series.Name = "test";

workbook.Save();

但这是我的结果(series.Values 仅包含 0 个值)。

也许您想为 X 轴使用文本?

        //series.Values = data.Keys.ToArray();
        //series.XValues = data.Values.ToArray();

        series.XValues = data.Keys.ToArray();
        series.Values = data.Values.ToArray();

或者您可以使用条形图: