娜小娜 發表於 2025-12-3 11:50:00

Excel处理控件Aspose.Cells教程:使用C#在Exce中创建帕累托线图

<p><img src="https://image.evget.com/attachment/keditor/image/20251203/100258_9.png"></p>
<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官方授权代理商慧都科技</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>
<h2 id="csharp-excel-library">用于创建帕累托线图的 C# Excel 库</h2>
<p><strong>Aspose.Cells for .NET</strong>是一个功能全面的 Excel 操作库,它允许开发人员在无需安装 Microsoft Office 的情况下创建、修改和渲染 Excel 文件。它支持多种图表类型,包括<strong>帕累托线</strong>图(ParetoLine chart),该图表将柱状图与累计百分比线相结合。</p>
<p>使用<strong>&nbsp;Aspose.Cells for .NET</strong>&nbsp;的主要优势:</p>
<ul>
<li><strong>丰富的 API</strong>&nbsp;– 完全访问 Excel 功能,包括高级图表类型。</li>
<li><strong>高性能</strong>——高效处理大型工作簿和数据集。</li>
<li><strong>多种输出格式</strong>– 保存为 XLSX、XLS、CSV、PDF、PNG 等格式。</li>
<li><strong>不支持 COM 互操作</strong>– 可在任何支持 .NET 的平台上运行。</li>
</ul>
<p>快速入门:</p>
<pre class="prettyprint highlighter-hljs"><code>PM&gt; Install-Package Aspose.Cells</code></pre>
<h2 id="create-partoinline-chart-in-excel">使用 C# 在 Excel 中创建帕累托线图</h2>
<h3 id="overview">概述</h3>
<p><img src="https://image.evget.com/attachment/keditor/image/20251203/101513_8.png"></p>
<p>帕累托图本质上是<strong>由柱状图</strong>(显示原始值)和<strong>折线</strong>图(显示这些值的累计百分比)组成。步骤如下:</p>
<ol>
<li>创建Workbook并获取第一个工作表。</li>
<li>在工作表中填充分类数据及其对应的数值。</li>
<li>添加<strong>柱状图</strong>。</li>
<li>添加第二个系列(累计百分比),并将其图表类型设置为<strong>折线图</strong>。</li>
<li>启用线条系列的辅助坐标轴。</li>
<li>格式化坐标轴、标题和数据标签。</li>
<li>保存工作簿。</li>
</ol>
<h3 id="complete-c-code-example">完整的 C# 代码示例</h3>
<pre class="prettyprint lang-cs highlighter-hljs"><code>// 1. Create a new workbook and get the first worksheet.
var workbook = new Workbook();
var sheet = workbook.Worksheets;
sheet.Name = "Defects";

// ------------------------------------------------------------
// 2. Populate worksheet with sample data (Category | Defect Count)
// ------------------------------------------------------------
//    A          B
// 1Defect   Count
// 2A          120
// 3B          90
// 4C          45
// 5D          30
// 6E          15
// ------------------------------------------------------------
sheet.Cells["A1"].Value = "Defect";
sheet.Cells["B1"].Value = "Count";

string[] categories = { "A", "B", "C", "D", "E" };
double[] counts = { 120, 90, 45, 30, 15 };

for (int i = 0; i &lt; categories.Length; i++)
{
    sheet.Cells.Value = categories;
    sheet.Cells.Value = counts;
}

// 3. Calculate cumulative percentages (required for Pareto line)
double total = 0;
foreach (var c in counts) total += c;

double cumulative = 0;
for (int i = 0; i &lt; counts.Length; i++)
{
    cumulative += counts;
    // Store cumulative percentage in column C (as a decimal, e.g., 0.75)
    sheet.Cells.Value = cumulative / total;
}
sheet.Cells["C1"].Value = "Cumulative %";

// 4. Add a Column chart (base type)
int chartIndex = sheet.Charts.Add(ChartType.Column, 7, 0, 25, 15);
Chart chart = sheet.Charts;
chart.Title.Text = "ParetoLine Chart ¨C Defect Distribution";

// 5. Add the primary series (Column) ¨C Defect Count
int columnSeriesIdx = chart.NSeries.Add("=Defects!$B$2:$B$6", true);
chart.NSeries.Name = "Count";
chart.NSeries.Type = ChartType.Column; // Explicitly set Column

// 6. Add the secondary series (Line) ¨C Cumulative Percentage
int lineSeriesIdx = chart.NSeries.Add("=Defects!$C$2:$C$6", true);
chart.NSeries.Name = "Cumulative %";
chart.NSeries.Type = ChartType.Line; // Set to Line chart
chart.NSeries.PlotOnSecondAxis = true; // Use secondary axis

// 7. Configure the secondary axis to display percentage format
chart.SecondValueAxis.TickLabels.NumberFormat = "0%";
chart.SecondValueAxis.Title.Text = "Cumulative Percentage";

// 8. Optional: Show data labels for better readability
chart.NSeries.DataLabels.ShowValue = true;
chart.NSeries.DataLabels.ShowValue = true;
chart.NSeries.DataLabels.NumberFormat = "0%";

// 9. Set the primary axis title
chart.CategoryAxis.Title.Text = "Defect Type";
chart.ValueAxis.Title.Text = "Count";

// 10. Save the workbook in XLSX format (can also be saved as PDF, PNG, etc.)
string outputPath = "ParetoLineChart_Output.xlsx";
workbook.Save(outputPath);
Console.WriteLine($"ParetoLine chart created successfully. File saved at: {outputPath}");</code></pre>
<h4 id="explanation-of-key-steps">关键步骤说明</h4>
<table>
<thead>
<tr>
<th>步</th>
<th>目的</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>2</strong></td>
<td>插入帕累托图将要表示的原始数据。</td>
</tr>
<tr>
<td><strong>3</strong></td>
<td>计算该系列所需的累计百分比。</td>
</tr>
<tr>
<td><strong>4-5</strong></td>
<td>创建基本<strong>柱形图</strong>并添加第一个系列(缺陷计数)。</td>
</tr>
<tr>
<td><strong>6-7</strong></td>
<td>添加第二个系列,将其类型设置为<strong>“线”</strong>,并将其放置在辅助坐标轴上。</td>
</tr>
<tr>
<td><strong>8</strong></td>
<td>将辅助坐标轴格式设置为百分比,并添加数据标签以提高清晰度。</td>
</tr>
<tr>
<td><strong>10</strong></td>
<td>保存工作簿;Aspose.Cells 还可以通过更改文件扩展名将图表渲染为 PDF、PNG 等格式。</td>
</tr>
</tbody>
</table>
<h2 id="conclusion">结论</h2>
<p>使用&nbsp;<strong>Aspose.Cells for .NET</strong>&nbsp;创建帕累托折线图非常简单:准备数据、添加柱状图、叠加用于显示累计百分比的折线图,并微调外观即可。提供的代码可直接编译运行,生成专业美观的帕累托分析图,并可保存为任何受支持的格式。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Aspose.Cells官方试用版免费下载,请联系Aspose官方授权代理商慧都科技</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/19301471
頁: [1]
查看完整版本: Excel处理控件Aspose.Cells教程:使用C#在Exce中创建帕累托线图