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

时间:2015-08-25 14:13:41

标签: c# excel charts excel-interop

我需要使用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和#39;值)。

Excel output

1 个答案:

答案 0 :(得分:1)

也许你想为X轴使用文字?

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

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

enter image description here

或许您可以使用条形图:

enter image description here