当前位置: 首页 > news >正文

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

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

Aspose.Cells官方试用版免费下载,请联系慧都科技

加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。

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

结论

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

Aspose.Cells官方试用版免费下载,请联系慧都科技

加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。

http://www.gsyq.cn/news/123955.html

相关文章:

  • 2025年星链连接线优质厂家TOP5推荐,靠谱厂商与生产厂全解析 - mypinpai
  • 2025年门窗行业品牌年度排名:门窗第一的品牌受客户认可不? - 工业品牌热点
  • 2025年FAG轴承优质供应商排行榜,轴承fag供应商、fag轴承代理新测评推荐 - 工业推荐榜
  • 2025年玻璃钢耐腐蚀弯头直销厂家权威推荐榜单:玻璃钢弯头/FRP弯头/玻璃钢90度弯头源头厂家精选 - 品牌推荐官
  • 北京GEO服务商综合评测:专业能力与实战效果深度解析 - 博客万
  • 2025年厚壁无缝钢管定做厂家权威推荐榜单:化工专用无缝钢管/API5LPSL2无缝钢管/碳钢无缝管源头厂家精选 - 品牌推荐官
  • Item44--将与参数无关的代码抽离 templates
  • 房叮咚订房平台深度解析:“用户出价,酒店抢单”如何颠覆传统酒店预订? - 资讯焦点
  • 房叮咚订房平台深度解析:“用户出价,酒店抢单”如何颠覆传统酒店预订? - 资讯焦点
  • 2025年方圆螺旋焊管直销厂家权威推荐榜单:螺旋管防腐/螺旋钢管/螺旋管涂塑源头厂家精选 - 品牌推荐官
  • 1N4007在小电流的压降
  • 2025年12月坦克链,无尘坦克链,工程塑料坦克链厂商推荐:聚焦企业综合实力与核心竞争力 - 品牌鉴赏师
  • 2025年高精度固体密度仪权威推荐榜单:高精度固体密度计/高精度固体电子密度计/固体电子比重测试仪源头厂家精选 - 品牌推荐官
  • imgui-python
  • 搞懂 Java 中的 VO、BO、PO、DTO、DO
  • 2025深圳旧房改造公司权威推荐! - 品牌评测官
  • 2025年隔热条生产厂家哪家质量可靠?五大品质之选厂商推荐全解析 - 工业品牌热点
  • 2025年度热解炉口碑好的生产商、靠谱服务商及价格合理加工厂推荐 - mypinpai
  • 2025-2026北京离婚子女抚养法律服务白皮书:专业律师测评与选择指南 - 老周说教育
  • 2025年星链连接线制造厂合作案例丰富的排名,推荐靠谱技术先进厂商 - 工业品牌热点
  • Agent工程化-langfuse
  • Agent工程化-langfuse
  • 2025 年辣味零食品牌推荐:重口味解馋小零食推荐及挑选指南和选购建议 - AIEO
  • 手机防止丢失方案
  • 探寻优质蓝牙音箱喇叭:泰声源电子脱颖而出 - mypinpai
  • 2025深圳旧房改造公司权威推荐榜单 - 品牌评测官
  • 2025年平层全屋定制公司TOP5推荐榜:基于市场格局与交付实力深度解析,这五家值得重点考察 - 品牌推荐
  • 2025年资深家居产业观察家推荐:当前最值得关注的5家平层全屋定制公司研究报告 - 品牌推荐
  • 2025 年 12 月制氮机厂家权威推荐榜:PSA制氮机装置,模组制氮机,氨气净化干燥装置,高效节能稳定供气系统深度解析 - 品牌企业推荐师(官方)
  • 警惕存储型XSS漏洞:Gal Dubinski Stars Testimonials插件安全风险剖析