
將股票價格、最高價、最低價、收盤價和交易量等財務數據可視化是分析師和開發人員的常見需求。藉助Aspose.Cells for .NET,您可以直接從 C# 應用程序生成股票最高價、最低價和收盤價圖表,而無需安裝 Microsoft Excel。
Aspose.Cells官方試用版免費下載,請聯繫Aspose官方授權代理商慧都科技
加入Aspose技術交流QQ羣(1041253375),與更多小夥伴一起探討提升開發技能。
用於創建股票最高價最低價收盤圖的 C# Excel 庫
Aspose.Cells for .NET是一個功能強大的全託管庫,使開發人員能夠以編程方式創建、修改和呈現 Excel 文件。它支持所有主流 Excel 格式,並提供豐富的 API 來處理工作表、單元格、圖表、數據透視表等。
圖表生成的主要優勢:
- 無需Excel互操作——可在任何運行.NET的平台上運行。
- 支持所有圖表類型——包括股票圖表、蠟燭圖、OHLC圖表和股票最高價最低價收盤圖。
- 豐富的自定義選項——顏色、標記、座標軸標題、輔助座標軸等。
- 高性能——適用於大型數據集。
入門
-
從慧都網下載最新版 Aspose.Cells for .NET 。
-
安裝NuGet 包:
-
Aspose.Cells在你的 C# 項目中添加對它的引用。
PM> Install-Package Aspose.Cells
使用 C# 在 Excel 中創建股票最高價最低價收盤圖
下面是一個完整的、可直接運行的 C# 控制枱程序,它創建一個工作表,用示例 OHLC 數據填充它,添加一個StockHighLowClose圖表,並將工作簿保存為StockChart_Output.xlsx。
// --------------------------------------------------------------------
// 1. Create a new workbook and obtain the first worksheet.
// --------------------------------------------------------------------
var workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
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 < dates.Length; i++)
{
int row = i + 1; // 0?based index; first data row is row 1 (Excel row 2)
sheet.Cells[row, 0].PutValue(DateTime.Parse(dates[i])); // Date
sheet.Cells[row, 1].PutValue(opens[i]); // Open
sheet.Cells[row, 2].PutValue(highs[i]); // High
sheet.Cells[row, 3].PutValue(lows[i]); // Low
sheet.Cells[row, 4].PutValue(closes[i]); // 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[chartIndex];
stockChart.Title.Text = "Sample Stock High?Low?Close Chart";
// --------------------------------------------------------------------
// 4. Set the data range for the chart.
// A2:A6 -> Category axis (dates)
// B2:E6 -> 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[index].Name = "Open";
index = stockChart.NSeries.Add("=FinancialData!$C$2:$C$6", true);
stockChart.NSeries[index].Name = "High";
index = stockChart.NSeries.Add("=FinancialData!$D$2:$D$6", true);
stockChart.NSeries[index].Name = "Low";
index = stockChart.NSeries.Add("=FinancialData!$E$2:$E$6", true);
stockChart.NSeries[index].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}");
代碼説明
| 步 | 代碼的作用 |
|---|---|
| 1️⃣ | 實例化一個新的Workbook工作表並訪問第一個工作表。 |
| 2️⃣ | 寫入標題,並用日期、開盤價、最高價、最低價和收盤價填充行。 |
| 3️⃣ | ChartType.StockHighLowClose在數據表下方添加圖表。 |
| 4️⃣ | 將圖表鏈接到工作表範圍,並定義類別(日期)軸。 |
| 5️⃣ | 設置座標軸標題並啓用網格線,以便更輕鬆地讀取價格水平。 |
| 6️⃣ | 在右側顯示圖例,並可自定義數據點標記。 |
| 7️⃣ | 將工作簿另存為StockChart_Output.xlsx. |
運行該程序會生成一個 Excel 文件,其中包含一個功能齊全的 StockHighLowClose 圖表,可以在 Microsoft Excel、LibreOffice 或任何其他支持 XLSX 格式的查看器中打開。
結論
使用 Aspose.Cells for .NET創建股票最高價-最低價-收盤價圖表非常簡單,無需在服務器上安裝 Microsoft Excel。按照上述完整示例操作,您可以生成專業美觀的財務圖表,並根據您的品牌進行自定義,然後將其集成到報表流程或 Web 應用程序中。
Aspose.Cells官方試用版免費下載,請聯繫Aspose官方授權代理商慧都科技
加入Aspose技術交流QQ羣(1041253375),與更多小夥伴一起探討提升開發技能。