同一条船上的人 發表於 2025-12-8 14:08:00

Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中创建股票高低收盘图

<p><img src="https://image.evget.com/attachment/keditor/image/20251208/134522_1.png"></p>
<p>将股票价格、最高价、最低价、收盘价和交易量等财务数据可视化是分析师和开发人员的常见需求。借助<strong>Aspose.Cells for .NET</strong>,您可以直接从 C# 应用程序生成<strong>股票最高价、最低价</strong>和收盘价图表,而无需安装 Microsoft 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 文件。它支持所有主流 Excel 格式,并提供丰富的 API 来处理工作表、单元格、图表、数据透视表等。</p>
<p>图表生成的主要优势:</p>
<ul>
<li><strong>无需Excel互操作</strong>——可在任何运行.NET的平台上运行。</li>
<li><strong>支持所有图表类型</strong>——包括股票图表、蜡烛图、OHLC图表和股票最高价最低价收盘图。</li>
<li><strong>丰富的自定义选项</strong>——颜色、标记、坐标轴标题、辅助坐标轴等。</li>
<li><strong>高性能</strong>——适用于大型数据集。</li>
</ul>
<h3 id="getting-started">入门</h3>
<ol>
<li>
<p><strong>从慧都网下</strong><strong>载</strong>最新版&nbsp;<strong>Aspose.Cells for .NET</strong>&nbsp;。</p>
</li>
<li>
<p><strong>安装</strong>NuGet 包:</p>
</li>
<li>
<p>Aspose.Cells在你的 C# 项目中添加对它的引用。</p>
</li>
</ol>
<pre class="prettyprint highlighter-hljs"><code>PM&gt; Install-Package Aspose.Cells</code></pre>
<h2 id="create-stockhighlowclose-chart">使用 C# 在 Excel 中创建股票最高价最低价收盘图</h2>
<p>下面是一个完整的、可直接运行的 C# 控制台程序,它创建一个工作表,用示例 OHLC 数据填充它,添加一个<strong>StockHighLowClose</strong>图表,并将工作簿保存为StockChart_Output.xlsx。</p>
<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 = "FinancialData";

// --------------------------------------------------------------------
// 2. Populate the worksheet with sample data.
//    Column A ¨C Date
//    Column B ¨C Open
//    Column C ¨C High
//    Column D ¨C Low
//    Column E ¨C Close
// --------------------------------------------------------------------
string[] dates = { "2025-10-01", "2025-10-02", "2025-10-03", "2025-10-04", "2025-10-05" };
double[] opens = { 150.2, 152.5, 151.0, 153.3, 154.8 };
double[] highs = { 155.0, 156.2, 154.0, 156.5, 158.0 };
double[] lows = { 148.5, 149.8, 149.0, 151.2, 152.1 };
double[] closes = { 152.0, 154.0, 150.5, 155.8, 157.3 };

// Write headers
sheet.Cells["A1"].PutValue("Date");
sheet.Cells["B1"].PutValue("Open");
sheet.Cells["C1"].PutValue("High");
sheet.Cells["D1"].PutValue("Low");
sheet.Cells["E1"].PutValue("Close");

// Fill data rows
for (int i = 0; i &lt; dates.Length; i++)
{
    int row = i + 1; // 0?based index; first data row is row 1 (Excel row 2)
    sheet.Cells.PutValue(DateTime.Parse(dates)); // Date
    sheet.Cells.PutValue(opens);                // Open
    sheet.Cells.PutValue(highs);                // High
    sheet.Cells.PutValue(lows);               // Low
    sheet.Cells.PutValue(closes);               // Close
}

// Format the date column (optional, makes Excel display dates nicely)
Style dateStyle = sheet.Cells["A2"].GetStyle();
dateStyle.Number = 14; // Built?in date format
sheet.Cells.CreateRange("A2", "A" + (dates.Length + 1)).SetStyle(dateStyle);

// --------------------------------------------------------------------
// 3. Add a StockHighLowClose chart.
// --------------------------------------------------------------------
// Parameters: chart type, upper?left row, upper?left column,
// lower?right row, lower?right column (all zero?based indexes)
int chartIndex = sheet.Charts.Add(ChartType.StockHighLowClose, 7, 0, 26, 10);
Chart stockChart = sheet.Charts;
stockChart.Title.Text = "Sample Stock High?Low?Close Chart";

// --------------------------------------------------------------------
// 4. Set the data range for the chart.
//    A2:A6   -&gt; Category axis (dates)
//    B2:E6    -&gt; Series data (Open, High, Low, Close)
// --------------------------------------------------------------------
stockChart.SetChartDataRange("A1:E6", true);
stockChart.NSeries.CategoryData = "A2:A6"; // Dates

// Add individual series. The order must match the chart type (Open, High, Low, Close)
int index = stockChart.NSeries.Add("=FinancialData!$B$2:$B$6", true);
stockChart.NSeries.Name = "Open";

index = stockChart.NSeries.Add("=FinancialData!$C$2:$C$6", true);
stockChart.NSeries.Name = "High";

index = stockChart.NSeries.Add("=FinancialData!$D$2:$D$6", true);
stockChart.NSeries.Name = "Low";

index = stockChart.NSeries.Add("=FinancialData!$E$2:$E$6", true);
stockChart.NSeries.Name = "Close";

// --------------------------------------------------------------------
// 5. Customize axes (optional but recommended for financial charts)
// --------------------------------------------------------------------
stockChart.CategoryAxis.Title.Text = "Date";
stockChart.ValueAxis.Title.Text = "Price";

// Display major grid lines on the value axis
stockChart.ValueAxis.MajorGridLines.IsVisible = true;
stockChart.ValueAxis.MajorGridLines.Weight = WeightType.SingleLine;
stockChart.ValueAxis.MajorGridLines.Color = Color.LightGray;

// --------------------------------------------------------------------
// 6. Adjust legend position and marker style.
// --------------------------------------------------------------------
stockChart.ShowLegend = true;
stockChart.Legend.Position = LegendPositionType.Right;

// Set marker style for better visibility of data points
foreach (Series series in stockChart.NSeries)
{
    series.Marker.MarkerStyle = ChartMarkerType.Circle;
    series.Marker.MarkerSize = 8;
    series.Marker.Area.Formatting = FormattingType.Custom;
    series.Marker.Area.ForegroundColor = Color.White;
    series.Marker.Border.IsVisible = true;
    series.Marker.Border.Color = Color.DarkBlue;
}

// --------------------------------------------------------------------
// 7. Save the workbook.
// --------------------------------------------------------------------
string outputPath = "StockChart_Output.xlsx";
workbook.Save(outputPath);
Console.WriteLine($"Workbook saved to {outputPath}");</code></pre>
<h3 id="explanation-of-the-code">代码说明</h3>
<table>
<thead>
<tr>
<th>步</th>
<th>代码的作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>1️⃣</td>
<td>实例化一个新的Workbook工作表并访问第一个工作表。</td>
</tr>
<tr>
<td>2️⃣</td>
<td>写入标题,并用日期、开盘价、最高价、最低价和收盘价填充行。</td>
</tr>
<tr>
<td>3️⃣</td>
<td>ChartType.StockHighLowClose在数据表下方添加图表。</td>
</tr>
<tr>
<td>4️⃣</td>
<td>将图表链接到工作表范围,并定义类别(日期)轴。</td>
</tr>
<tr>
<td>5️⃣</td>
<td>设置坐标轴标题并启用网格线,以便更轻松地读取价格水平。</td>
</tr>
<tr>
<td>6️⃣</td>
<td>在右侧显示图例,并可自定义数据点标记。</td>
</tr>
<tr>
<td>7️⃣</td>
<td>将工作簿另存为StockChart_Output.xlsx.</td>
</tr>
</tbody>
</table>
<p>运行该程序会生成一个 Excel 文件,其中包含一个功能齐全的 StockHighLowClose 图表,可以在 Microsoft Excel、LibreOffice 或任何其他支持 XLSX 格式的查看器中打开。</p>
<h2 id="conclusion">结论</h2>
<p>使用<strong>&nbsp;Aspose.Cells for .NET</strong>创建<strong>股票最高价-最低价</strong>-收盘价图表非常简单,无需在服务器上安装 Microsoft Excel。按照上述完整示例操作,您可以生成专业美观的财务图表,并根据您的品牌进行自定义,然后将其集成到报表流程或 Web 应用程序中。</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/19321498
頁: [1]
查看完整版本: Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中创建股票高低收盘图