
使用旭日圖可以更清晰地可視化層級信息,例如組織結構、產品類別或文件系統佈局。本文將演示如何使用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 等格式。
- 高性能——高效處理大型工作簿和數據集。
入門
- 從慧都網Aspose.Cells頁面下載該庫。
- 從NuGet安裝
PM> Install-Package Aspose.Cells - 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),與更多小夥伴一起探討提升開發技能。