Microsoft® Excel 文件图表创建和转换 via .NET
在基于 .NET 的应用程序中使用服务器端 API 创建 Excel 文档图表并转换为图像。
绘制图表是一门以图形方式显示数据以便于分析的艺术。 .NET Excel 库 支持在Excel文件中绘制图表。 API 支持中列出的不同图表创建 图表类型枚举 包括饼图、金字塔图、折线图和气泡图。此外,它还将图表转换为图像。 API 提供 图表类 用于图表构建块。
在 Excel 文件中创建图表
使用 Excel API 创建图表很简单。流程是,创建 作业本类 对象并通过提供其索引来选择第一个工作表或相关工作表。使用插入所需的单元格数据 价值法 。使用 Charts 集合将图表添加到工作表 添加方法 。指定 图表类型 来自 ChartType 枚举。
C# 创建 Excel 图表的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instantiating a Workbook object | |
Workbook wkb = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet wks = workbook.Worksheets[0]; | |
// Adding sample values to cells | |
wks.Cells["A2"].PutValue("Category1"); | |
wks.Cells["A3"].PutValue("Category2"); | |
wks.Cells["A4"].PutValue("Category3"); | |
wks.Cells["B1"].PutValue("Column1"); | |
wks.Cells["B2"].PutValue(4); | |
wks.Cells["B3"].PutValue(20); | |
wks.Cells["B4"].PutValue(50); | |
wks.Cells["C1"].PutValue("Column2"); | |
wks.Cells["C2"].PutValue(50); | |
wks.Cells["C3"].PutValue(100); | |
wks.Cells["C4"].PutValue(150); | |
// Adding a chart to the worksheet | |
int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
// Setting chart data source as the range "A1:C4" | |
chart.SetChartDataRange("A1:C4", true); | |
// Saving the Excel file | |
wkb.Save(dataDir + "output.xls"); | |
// API supports creating Pyramid, Line and Bubble chart as well |
将 Excel 图表转换为图像
将图表转换为图像的过程是,使用 Workbook 类加载 Excel 文件,选择包含图表的相关工作集并调用 图像方法 用于转换。
C# 将Excel图表转换为图像的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load Excel XLSX document having pie chart | |
var workbook = new Workbook("PieChart.xlsx"); | |
// get the designer chart (first chart) in the first worksheet of the workbook | |
var chart = workbook.Worksheets[0].Charts[0]; | |
// convert the chart to an image file. | |
chart.ToImage("PieChartOut.emf", System.Drawing.Imaging.ImageFormat.Emf); |