简体   繁体   English

使用Aspose.Cells从C#代码的数据源而不是电子表格范围中创建Excel条形图

[英]Create Excel bar chart from a data source in C# code instead of a spreadsheet range using Aspose.Cells

I'm trying to use Aspose.Cells to generate a bar chart on a spreadsheet. 我正在尝试使用Aspose.Cells在电子表格上生成条形图。 The data source is an IEnumerable of decimal numbers, which are retrieved from a database. 数据源是一个IEnumerable十进制数字,可从数据库中检索。 Reading the Aspose.Cells documentation, the only way I've seen to create a bar chart in code is by taking the bar chart's data from a range on a spreadsheet. 阅读Aspose.Cells文档,我看到的在代码中创建条形图的唯一方法是从电子表格上的某个范围获取条形图的数据。 I want to generate the chart directly from the IEnumerable, without having to actually include the data on a spreadsheet. 我想直接从IEnumerable生成图表,而不必实际将数据包含在电子表格中。 Is there any way to do this, either using Aspose.Cells or standard .NET libraries? 使用Aspose.Cells或标准.NET库,有什么方法可以做到这一点? If it's possible to generate a temporary workbook in code, paste the data onto it and use that as a data source (discarding it immediately afterward), I could probably also make that work. 如果可以用代码生成一个临时工作簿,将数据粘贴到该工作簿上并将其用作数据源(此后立即将其丢弃),那么我也可能会完成该工作。 Thanks! 谢谢!

Please see the following code. 请参见以下代码。 It creates the Bar Chart using Aspose.Cells APIs. 它使用Aspose.Cells API创建条形图 Please read the comments inside the code for more help. 请阅读代码中的注释以获取更多帮助。

The following screenshot shows the output Excel file containing the Bar Chart generated by the code for your reference. 以下屏幕截图显示了输出Excel文件,其中包含由代码生成的条形图,以供您参考。

由Aspose.Cells生成的条形图


Sample Code in C# C#中的示例代码

// Create empty workbook.
Workbook wb = new Workbook();

// Access first worksheet.
Worksheet ws = wb.Worksheets[0];

// Add Bar chart in first worksheet.
int idx = ws.Charts.Add(ChartType.Bar, 5, 2, 20, 10);

// Access Bar chart.
Chart ch = ws.Charts[0];

// Add two number series, true means they are vertical.
ch.NSeries.Add("{6,3,1,7}", true);
ch.NSeries.Add("{2,5,7,1}", true);

// Set the category data to show on X-axis.
ch.NSeries.CategoryData = "{Apple,Pear,Orange,Mango}";

// Set the name of first and second series.
ch.NSeries[0].Name = "Cricket";
ch.NSeries[1].Name = "Hockey";

// Save the output in xlsx format.
wb.Save("outputBarChart.xlsx", SaveFormat.Xlsx);

Note: I am working as Developer Evangelist at Aspose 注意: 我在Aspose担任开发人员布道者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM