news 2026/9/2 0:29:19

Excel处理控件Aspose.Cells教程:使用C#在Excel中创建旭日图

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建旭日图

使用旭日图可以更清晰地可视化层级信息,例如组织结构、产品类别或文件系统布局。本文将演示如何使用Aspose.Cells for .NET在 Excel 工作簿中创建旭日图。该示例完全可运行,仅需 Aspose.Cells NuGet 包,并且可以适用于任何层级数据集。

Aspose.Cells官方试用版免费下载

用于创建旭日图的 C# Excel 库

Aspose.Cells for .NET是一个功能强大的 Excel 自动化库,无需 Office 互操作即可使用。它提供了一个简洁的面向对象的 API,用于创建、修改和设置图表样式,包括 Excel 2016 中引入的旭日图。

选择 Aspose.Cells 生成旭日图的主要原因:

  • 全面的 API– 完全访问图表类型、数据系列和格式选项。
  • 无需安装 Excel– 可在服务器端、云端或容器环境中运行。
  • 支持跨格式——保存为 XLSX、XLS、CSV、PDF、PNG 等格式。
  • 高性能——高效处理大型工作簿和数据集。

入门

  1. 慧都网Aspose.Cells页面下载该库。
  2. 从NuGet安装
    PM> Install-Package Aspose.Cells
  3. Aspose.Cells在你的C#项目中添加对它的引用。

使用 C# 在 Excel 中创建旭日图

下面是一个完整的、独立的 C# 示例,演示如何在 Excel 中创建旭日图。

// ------------------------------------------------------------ // 1. Create a new workbook and obtain the first worksheet. // ------------------------------------------------------------ var workbook = new Workbook(); var sheet = workbook.Worksheets[0]; sheet.Name = "Hierarchy"; // ------------------------------------------------------------ // 2. Populate hierarchical data. // The data layout follows the structure required by Sunburst: // Column A ¨C Category (Level 1) // Column B ¨C Sub?Category (Level 2) // Column C ¨C Item (Level 3) // Column D ¨C Value (numeric) // ------------------------------------------------------------ string[,] data = new string[,] { // Category, Sub?Category, Item, Value { "Technology", "Hardware", "Laptop", "120" }, { "Technology", "Hardware", "Desktop", "80" }, { "Technology", "Software", "OS", "150" }, { "Technology", "Software", "Office", "100" }, { "Finance", "Banking", "Retail", "200" }, { "Finance", "Banking", "Corporate", "180" }, { "Finance", "Investments","Equity", "130" }, { "Finance", "Investments","Bonds", "90" } }; // Write the header row sheet.Cells["A1"].PutValue("Category"); sheet.Cells["B1"].PutValue("Sub?Category"); sheet.Cells["C1"].PutValue("Item"); sheet.Cells["D1"].PutValue("Value"); // Fill the data rows for (int r = 0; r < data.GetLength(0); r++) { for (int c = 0; c < data.GetLength(1); c++) { sheet.Cells[r + 1, c].PutValue(data[r, c]); } } // ------------------------------------------------------------ // 3. Add a Sunburst chart. // ------------------------------------------------------------ // The chart will be placed starting at row 12, column 0 and // will span 20 rows and 10 columns. int chartIdx = sheet.Charts.Add(ChartType.Sunburst, 12, 0, 32, 10); Chart sunburstChart = sheet.Charts[chartIdx]; sunburstChart.Title.Text = "Company Revenue by Category"; // ------------------------------------------------------------ // 4. Set the data range for the chart. // Sunburst expects the first column to contain the innermost // level (Category), and the last column to hold the numeric // values. // ------------------------------------------------------------ // A2:D9 contains the hierarchy + values. sunburstChart.SetChartDataRange("=Hierarchy!$A$2:$D$9", true); // ------------------------------------------------------------ // 5. (Optional) Customize the appearance. // ------------------------------------------------------------ // Example: Set a pastel background for the PlotArea. sunburstChart.PlotArea.Area.Formatting = FormattingType.Custom; sunburstChart.PlotArea.Area.ForegroundColor = Color.FromArgb(247, 250, 255); // Set the legend to the right side. sunburstChart.Legend.Position = LegendPositionType.Right; // ------------------------------------------------------------ // 6. Save the workbook. // ------------------------------------------------------------ string outputPath = "SunburstChart_Output.xlsx"; workbook.Save(outputPath); Console.WriteLine($"Sunburst chart created successfully: {outputPath}");

关键步骤说明

目的
2层级数据按列排列,其中最左边的列代表最外层的环(类别),最右边的数值列保存值。
3ChartType.Sunburst创建所需的图表类型。
4SetChartDataRange将数据范围与图表关联起来;该标志true告诉 Aspose.Cells 该范围包含类别和值。
5可选的外观调整(背景填充、图例位置)。
6将工作簿保存为 XLSX 格式,以便进一步处理或导出为 PDF/PNG。

结论

使用Aspose.Cells for .NET以编程方式创建旭日图非常简单。该库无需在服务器上运行 Microsoft Excel 即可处理数据层次结构、图表类型选择和样式设置。您可以以提供的示例为基础,根据您的领域调整数据源,并利用 Aspose 丰富的 API 生成精美且可直接导出的可视化图表。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 0:16:31

Fabric8 Kubernetes客户端终极指南:Java开发者的云原生利器

Fabric8 Kubernetes客户端终极指南&#xff1a;Java开发者的云原生利器 【免费下载链接】kubernetes-client Java client for Kubernetes & OpenShift 项目地址: https://gitcode.com/gh_mirrors/ku/kubernetes-client Fabric8 Kubernetes客户端是专为Java开发者设…

作者头像 李华
网站建设 2026/9/2 21:44:23

Godex:重构Godot游戏开发范式的ECS架构引擎

Godex&#xff1a;重构Godot游戏开发范式的ECS架构引擎 【免费下载链接】godex Godex is a Godot Engine ECS library. 项目地址: https://gitcode.com/gh_mirrors/go/godex 在现代游戏开发领域&#xff0c;性能优化和代码可维护性始终是开发者面临的核心挑战。Godex作为…

作者头像 李华
网站建设 2026/9/2 21:43:18

Kotaemon方言识别支持:地域性知识服务拓展

Kotaemon方言识别支持&#xff1a;地域性知识服务拓展 在智慧政务、远程医疗和数字教育快速普及的今天&#xff0c;一个常被忽视的现实是&#xff1a;仍有数亿人因语言障碍难以真正融入智能服务体系。他们可能是只会说粤语的广州老人&#xff0c;是习惯用闽南语交流的泉州渔民&…

作者头像 李华
网站建设 2026/9/2 21:13:28

Espanso文本扩展器完全手册:5个技巧让你的输入效率翻倍

Espanso文本扩展器完全手册&#xff1a;5个技巧让你的输入效率翻倍 【免费下载链接】espanso Cross-platform Text Expander written in Rust 项目地址: https://gitcode.com/gh_mirrors/es/espanso 你是否曾经在重复输入同样的地址、签名或代码片段时感到厌倦&#xff…

作者头像 李华
网站建设 2026/9/1 13:30:32

终极U盘量产工具:Chipsbank APTool V7200快速修复与批量生产完整指南

终极U盘量产工具&#xff1a;Chipsbank APTool V7200快速修复与批量生产完整指南 【免费下载链接】ChipsbankAPTool量产工具V72002020-00-21 Chipsbank APTool量产工具是专门针对Chipsbank生产的USB控制芯片设计的一款强大工具。本版本V7200发布于2020年2月21日&#xff0c;针对…

作者头像 李华
网站建设 2026/9/2 1:42:42

Kotaemon助力制造业知识管理数字化转型

Kotaemon助力制造业知识管理数字化转型在一家大型汽车零部件工厂的车间里&#xff0c;一位刚上岗的年轻操作员面对一台突然报错停机的数控机床手足无措。他没有呼叫主管&#xff0c;也没有翻找厚厚的操作手册&#xff0c;而是打开手机上的一个小程序&#xff0c;轻声问&#xf…

作者头像 李华