我有一张Excel图表。如何添加图例值?
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("B12", "B15");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xl3DPieExploded;
foreach (Series series in chartPage.SeriesCollection())
{
series.Name = "Diagram Name";
}
chartPage.HasLegend = true;
chartPage.Is
//export chart as picture file
String destPath = Directory.GetCurrentDirectory();
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue);
方法"系列"不可用。
答案 0 :(得分:0)
这对我有用。我添加了一些数据,所以这个例子可以单独使用。
Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbook xlWorkBook;
Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Sheet";
ChartObjects xlCharts = (ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
Chart chartPage = myChart.Chart;
chartPage.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Title";
xlWorkSheet.Cells[12, 1] = "Test";
xlWorkSheet.Cells[13, 1] = "Test2";
xlWorkSheet.Cells[14, 1] = "Test3";
xlWorkSheet.Cells[15, 1] = "Test4";
xlWorkSheet.Cells[12, 2] = 12;
xlWorkSheet.Cells[13, 2] = 13;
xlWorkSheet.Cells[14, 2] = 14;
xlWorkSheet.Cells[15, 2] = 15;
var sc = chartPage.SeriesCollection();
var series1 = sc.NewSeries();
series1.Name = "Title";
series1.XValues = $"'{xlWorkSheet.Name}'!A12:A15";
series1.Values = $"'{xlWorkSheet.Name}'!B12:B15";
series1.ChartType = XlChartType.xl3DPieExploded;
chartPage.ApplyDataLabels();
//export chart as picture file
String destPath = Directory.GetCurrentDirectory();
chartPage.Export(Directory.GetCurrentDirectory() + "\\dig2.bmp", "BMP", misValue);