夜半孤鹤 發表於 2025-12-4 11:40:00

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

<p><img src="https://image.evget.com/attachment/keditor/image/20251204/094925_4.png"></p>
<p>使用漏斗图可以更清晰地可视化转化率、销售渠道或任何层级数据集。本文将介绍如何使用 C# 和<strong>Aspose.Cells for .NET</strong>在 Excel 中创建漏斗图。只需几行代码,即可自动生成极具视觉冲击力的漏斗图,并可将其导出为 XLSX、PDF 或图像格式。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系Aspose官方授权代理商<span style="color: rgba(53, 152, 219, 1)">慧都科技</span></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 id="csharp-excel-library">用于创建漏斗图的 C# Excel 库</h3>
<p><strong>Aspose.Cells for .NET</strong>是一个功能强大的电子表格库,它使开发人员能够以编程方式创建、修改和呈现 Excel 文件。它提供了一系列丰富的图表类型,包括<strong>漏斗</strong>图,而漏斗图在 Microsoft Office 原生服务器端互操作库中并不提供。</p>
<p>使用<strong>&nbsp;Aspose.Cells for .NET</strong>&nbsp;的主要优势:</p>
<ul>
<li><strong>功能全面的API——</strong>无需COM依赖即可完全访问Excel功能。</li>
<li><strong>服务器端友好</strong>——无需安装微软Office。</li>
<li><strong>高性能</strong>——以最小的内存占用处理大型工作簿。</li>
<li><strong>多种输出格式</strong>——保存为 XLSX、XLS、CSV、PDF、PNG 等格式。</li>
</ul>
<p>快速入门:</p>
<ol>
<li>
<p><strong>从慧都网</strong><strong>下载</strong>最新版本。</p>
</li>
<li>
<p>通过 NuGet 安装:</p>
</li>
</ol>
<pre class="prettyprint highlighter-hljs"><code>PM&gt; Install-Package Aspose.Cells</code></pre>
<h3 id="create-funnel-chart-in-excel">使用 C# 在 Excel 中创建漏斗图</h3>
<p><img src="https://image.evget.com/attachment/keditor/image/20251204/095908_6.png"></p>
<p>以下是两个完整的、可运行的示例,展示了如何操作:</p>
<ol>
<li>根据静态数据集<strong>创建基本漏斗图。</strong></li>
<li><strong>自定义漏斗图</strong>(颜色、数据标签和图例)。</li>
</ol>
<blockquote>
<p><strong>注意</strong>– 所有代码片段均使用<strong>.NET 6.0</strong>或更高版本编译,目标框架为net6.0(或任何更高版本)。请将 `&lt;folder&gt;` 替换outputPath为您计算机上的相应文件夹。</p>
</blockquote>
<h4 id="example-1--basic-funnel-chart">示例 1 – 基本漏斗图</h4>
<pre class="prettyprint lang-cs highlighter-hljs"><code>// ------------------------------------------------------------------
// 1. Create a new workbook and obtain the first worksheet.
// ------------------------------------------------------------------
var workbook = new Workbook();
Worksheet sheet = workbook.Worksheets;
sheet.Name = "FunnelData";

// ------------------------------------------------------------------
// 2. Populate worksheet with sample funnel data.
//    A column ¨C Stage description
//    B column ¨C Number of items at each stage
// ------------------------------------------------------------------
//    A          B
// 1Stage      Count
// 2Lead       5000
// 3Qualified3200
// 4Proposal   2100
// 5Negotiation1500
// 6Closed   950
// ------------------------------------------------------------------
sheet.Cells["A1"].PutValue("Stage");
sheet.Cells["B1"].PutValue("Count");

string[] stages = { "Lead", "Qualified", "Proposal", "Negotiation", "Closed" };
int[] counts = { 5000, 3200, 2100, 1500, 950 };

for (int i = 0; i &lt; stages.Length; i++)
{
    sheet.Cells.PutValue(stages);   // Column A
    sheet.Cells.PutValue(counts);   // Column B
}

// ------------------------------------------------------------------
// 3. Add a Funnel chart object.
// ------------------------------------------------------------------
int chartIndex = sheet.Charts.Add(ChartType.Funnel, 7, 0, 25, 12);
Chart funnelChart = sheet.Charts;
funnelChart.Title.Text = "Sales Funnel";

// ------------------------------------------------------------------
// 4. Define the data range for the chart.
//    Series data ¨C B2:B6 (counts)
//    Category data ¨C A2:A6 (stages)
// ------------------------------------------------------------------
funnelChart.NSeries.Add("=FunnelData!$B$2:$B$6", true);
funnelChart.NSeries.CategoryData = "=FunnelData!$A$2:$A$6";

// ------------------------------------------------------------------
// 5. Save the workbook.
// ------------------------------------------------------------------
string outputPath = "FunnelChart_Basic.xlsx";
workbook.Save(outputPath);
Console.WriteLine($"Workbook saved to {outputPath}");</code></pre>
<p><strong>代码的作用</strong></p>
<ol>
<li>创建工作簿并添加代表典型销售流程的示例数据。</li>
<li>从第 7 行第 0 列开始插入<strong>漏斗</strong>图( )。ChartType.Funnel</li>
<li>将序列值和类别绑定到数据范围。</li>
<li>将结果保存为FunnelChart_Basic.xlsx.</li>
</ol>
<p>在 Microsoft Excel 中打开生成的文件——您将看到一个清晰的漏斗图,该图直观地显示了每个阶段的下降情况。</p>
<h4 id="example-2--customized-funnel-chart-colors-data-labels-legend">示例 2 – 自定义漏斗图(颜色、数据标签、图例)</h4>
<pre class="prettyprint lang-cs highlighter-hljs"><code>// ------------------------------------------------------------------
// 1. Prepare workbook and data.
// ------------------------------------------------------------------
var wb = new Workbook();
Worksheet ws = wb.Worksheets;
ws.Name = "Pipeline";

ws.Cells["A1"].PutValue("Stage");
ws.Cells["B1"].PutValue("Leads");

string[] stageNames = { "Awareness", "Interest", "Consideration", "Intent", "Purchase" };
int[] leadCounts = { 8000, 5600, 3800, 2100, 950 };

for (int i = 0; i &lt; stageNames.Length; i++)
{
    ws.Cells.PutValue(stageNames);
    ws.Cells.PutValue(leadCounts);
}

// ------------------------------------------------------------------
// 2. Insert a Funnel chart.
// ------------------------------------------------------------------
int idx = ws.Charts.Add(ChartType.Funnel, 8, 0, 28, 13);
Chart funnel = ws.Charts;
funnel.Title.Text = "Marketing Funnel";

// Bind series and categories.
funnel.NSeries.Add("=Pipeline!$B$2:$B$6", true);
funnel.NSeries.CategoryData = "=Pipeline!$A$2:$A$6";

// ------------------------------------------------------------------
// 3. Apply custom colors to each funnel slice.
// ------------------------------------------------------------------
Color[] sliceColors = {
    Color.FromArgb(91, 155, 213),   // Awareness
    Color.FromArgb(237, 125, 49),   // Interest
    Color.FromArgb(165, 165, 165), // Consideration
    Color.FromArgb(255, 192, 0),   // Intent
    Color.FromArgb(112, 173, 71)   // Purchase
};

for (int i = 0; i &lt; funnel.NSeries.Points.Count; i++)
{
    funnel.NSeries.Points.Area.ForegroundColor = sliceColors;
    funnel.NSeries.Points.Area.Formatting = FormattingType.Custom;
}

// ------------------------------------------------------------------
// 4. Show data labels (value + percentage).
// ------------------------------------------------------------------
funnel.NSeries.DataLabels.ShowValue = true;
funnel.NSeries.DataLabels.ShowPercentage = true;
funnel.NSeries.DataLabels.Position = LabelPositionType.Center;

// ------------------------------------------------------------------
// 5. Adjust legend ¨C place it at the bottom and use a horizontal layout.
// ------------------------------------------------------------------
funnel.ShowLegend = true;
funnel.Legend.Position = LegendPositionType.Bottom;

// ------------------------------------------------------------------
// 6. Save the workbook.
// ------------------------------------------------------------------
string outFile = "FunnelChart_Customized.xlsx";
wb.Save(outFile);
Console.WriteLine($"Customized funnel chart saved to {outFile}");</code></pre>
<p><strong>定制亮点</strong></p>
<ul>
<li><strong>颜色</strong>–每个漏斗切片都使用不同的前景色Area.ForegroundColor。FormattingType.Custom</li>
<li><strong>数据标签</strong>– 启用后,数值和百分比显示将居中显示在每个切片内。</li>
<li><strong>图例</strong>– 位于底部,采用水平布局,以便更好地阅读。</li>
</ul>
<p>点击FunnelChart_Customized.xlsx查看完整样式的漏斗图,适用于演示或报告。</p>
<h3 id="conclusion">结论</h3>
<p><strong>使用Aspose.Cells for .NET</strong>创建漏斗图非常简单,只需几行 C# 代码即可。该库提供对数据绑定、样式和导出选项的全面控制,使其成为服务器端报表、仪表板和自动生成 Excel 表格的理想选择。您可以以上述示例为基础,并根据自己的数据源和视觉需求进行调整。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系Aspose官方授权代理商<span style="color: rgba(53, 152, 219, 1)">慧都科技</span></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/19306249
頁: [1]
查看完整版本: Excel处理控件Aspose.Cells教程:使用C#在Excel中创建漏斗图