Excel处理控件Aspose.Cells教程:使用C#在Excel中创建树状图
<p><img src="https://image.evget.com/attachment/keditor/image/20251218/095746_2.png"></p><p>使用树状图可视化层级数据,可以使复杂的信息一目了然。本文将介绍如何使用 C# 和<strong>Aspose.Cells for .NET</strong>在 Excel 中创建树状图。本指南包含完整的可运行代码示例、自定义图表外观的技巧以及快速入门的资源。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p>
<h3>用于创建树状图的 C# Excel 库</h3>
<p><strong>Aspose.Cells for .NET</strong>是一个功能全面的 Excel 操作库,允许开发人员在不使用 Microsoft Office 的情况下创建、编辑和渲染 Excel 文件。它支持多种图表类型,包括<strong>树状</strong>图,该图表非常适合可视化层级结构,例如按地区、产品类别或组织结构图划分的销售额。</p>
<p>使用<strong> Aspose.Cells for .NET</strong> 的主要优势:</p>
<ul>
<li><strong>丰富的 API</strong> – 完全访问工作簿、工作表、单元格和图表对象。</li>
<li><strong>高性能</strong>——能够高效处理大型工作簿和数据集。</li>
<li><strong>无外部依赖项</strong>– 可在任何支持 .NET 的平台上运行。</li>
<li><strong>多种导出格式</strong>– 保存为 XLSX、XLS、CSV、PDF、PNG 等格式。</li>
</ul>
<p>入门很简单:</p>
<ol>
<li>
<p>从<strong>慧都网Aspose.Cells 页面</strong>下载库。</p>
</li>
<li>
<p>安装 NuGet 包:</p>
</li>
</ol>
<pre class="prettyprint highlighter-hljs"><code>PM> Install-Package Aspose.Cells</code></pre>
<h3>使用 C# 在 Excel 中创建树状图</h3>
<h4>如何构建树状图</h4>
<p>以下示例演示了如何操作:</p>
<ol>
<li>创建一个新的工作簿。</li>
<li>在工作表中填充层级数据。</li>
<li>添加<strong>树状</strong>图。</li>
<li>配置剧集、标题和格式。</li>
<li>将工作簿保存为 Excel 文件。</li>
</ol>
<blockquote>
<p><strong>注意</strong>——该代码是完全独立的,可以使用 .NET 6.0 或更高版本进行编译。</p>
</blockquote>
<pre class="prettyprint lang-cs highlighter-hljs"><code>// ------------------------------
// 1. Create a new workbook
// ------------------------------
var workbook = new Workbook();
var sheet = workbook.Worksheets;
sheet.Name = "SalesData";
// -------------------------------------------------
// 2. Fill the worksheet with hierarchical sample data
// -------------------------------------------------
// A B C D E
// -------------------------------------------------
// Region Country Category SubcategorySales
// Europe Germany Electronics Phones 120000
// Europe Germany Electronics Laptops 85000
// Europe France FurnitureChairs 45000
// Asia China Electronics Phones 200000
// Asia China FurnitureTables 95000
// America USA Electronics TVs 175000
// -------------------------------------------------
string[,] data = new string[,]
{
{ "Region", "Country", "Category", "Subcategory", "Sales" },
{ "Europe", "Germany", "Electronics", "Phones", "120000" },
{ "Europe", "Germany", "Electronics", "Laptops", "85000" },
{ "Europe", "France", "Furniture", "Chairs", "45000" },
{ "Asia", "China", "Electronics", "Phones", "200000" },
{ "Asia", "China", "Furniture", "Tables", "95000" },
{ "America", "USA", "Electronics", "TVs", "175000" }
};
for (int row = 0; row < data.GetLength(0); row++)
{
for (int col = 0; col < data.GetLength(1); col++)
{
sheet.Cells.PutValue(data);
}
}
// -------------------------------------------------
// 3. Add a Treemap chart
// -------------------------------------------------
// The chart will be placed starting at row 9, column 0
// and will occupy rows 9?30 and columns 0?10.
int chartIndex = sheet.Charts.Add(ChartType.Treemap, 9, 0, 30, 10);
Chart treemap = sheet.Charts;
// Set chart title
treemap.Title.Text = "Global Sales Treemap";
// -------------------------------------------------
// 4. Define the series for the Treemap
// -------------------------------------------------
// The data range includes columns A?E (rows 2?7) without the header.
// Category data (hierarchy) is taken from columns A?D.
// Values are taken from column E.
treemap.NSeries.Add("=SalesData!$E$2:$E$7", true);
treemap.NSeries.CategoryData = "=SalesData!$A$2:$D$7";
// -------------------------------------------------
// 5. Customize the appearance (optional)
// -------------------------------------------------
// Example: set a graduated fill based on sales values.
treemap.NSeries.IsColorVaried = true; // Vary color by value
// -------------------------------------------------
// 6. Save the workbook
// -------------------------------------------------
string outputPath = "TreemapChart_Output.xlsx";
workbook.Save(outputPath);
Console.WriteLine($"Treemap chart created successfully. File saved to: {outputPath}");</code></pre>
<p><strong>关键步骤说明</strong></p>
<table>
<thead>
<tr>
<th>步</th>
<th>目的</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>1</strong></td>
<td>实例化一个新的对象Workbook并获取第一个工作表。</td>
</tr>
<tr>
<td><strong>2</strong></td>
<td>将分层销售数据填充到工作表中。第一行包含标题。</td>
</tr>
<tr>
<td><strong>3</strong></td>
<td>添加一个图表类型ChartType.Treemap。图表的位置和大小由传递给的行/列索引定义Charts.Add。</td>
</tr>
<tr>
<td><strong>4</strong></td>
<td>添加一个使用<strong>销售额</strong>作为值范围(E2:E7)和层次结构(A2:D7)作为类别数据的单个系列。</td>
</tr>
<tr>
<td><strong>5</strong></td>
<td>启用数据标签,使其同时显示数值和类别名称,并根据销售额激活颜色变化。</td>
</tr>
<tr>
<td><strong>6</strong></td>
<td>将工作簿保存为 XLSX 文件。生成的文件可在 Excel 中打开以查看树状图。</td>
</tr>
</tbody>
</table>
<h3>结论</h3>
<p>使用 <strong>Aspose.Cells for .NET</strong> 创建树状图非常简单,只需几行代码即可完成。该库无需 Microsoft Office 即可处理数据层次结构、图表渲染和文件输出。按照上面的示例,您可以快速生成专业的树状图可视化图表,用于财务报告、销售分析或任何层次结构数据集。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong><em>加入Aspose技术交流QQ群(1041253375),与更多小伙伴一起探讨提升开发技能。</em></strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19365670
頁:
[1]