Các trường hợp sử dụng Aspose.Cells FOSS cho .NET
Aspose.Cells FOSS for .NET là một thư viện .NET thuần quản lý, có khả năng đọc và ghi các tệp XLSX mà không cần Microsoft Office hay bất kỳ phụ thuộc bên ngoài nào. Các trường hợp sử dụng sau đây minh họa vị trí của thư viện trong các ứng dụng .NET thực tế.
Tạo báo cáo
Tạo báo cáo XLSX một cách lập trình trong các ứng dụng phía máy chủ hoặc batch. Sử dụng Workbook, Worksheet và Cell.PutValue() để ghi dữ liệu có cấu trúc, sau đó gọi Workbook.Save() để tạo tệp.
using Aspose.Cells_FOSS;
var wb = new Workbook();
var ws = wb.Worksheets[0];
ws.Name = "Monthly Report";
ws.Cells["A1"].PutValue("Product");
ws.Cells["B1"].PutValue("Revenue");
ws.Cells["A2"].PutValue("Widget A");
ws.Cells["B2"].PutValue(12500.00m);
ws.Cells["A3"].PutValue("Widget B");
ws.Cells["B3"].PutValue(8750.00m);
ws.Cells["B4"].Formula = "=SUM(B2:B3)";
wb.Save("monthly-report.xlsx");Mẫu này có thể mở rộng cho bất kỳ số lượng hàng nào và có thể được điều khiển bằng dữ liệu từ cơ sở dữ liệu hoặc phản hồi API.
Đang xử lý bảng tính đã tải lên
Đọc và xác thực các tệp XLSX do người dùng gửi qua biểu mẫu web hoặc điểm cuối API. Trình khởi tạo Workbook chấp nhận một Stream, vì vậy không cần ghi tệp tạm thời vào đĩa.
using Aspose.Cells_FOSS;
// IFormFile from ASP.NET Core controller
public async Task<IActionResult> Upload(IFormFile file)
{
using var stream = file.OpenReadStream();
var opts = new LoadOptions { TryRepairPackage = true };
var wb = new Workbook(stream, opts);
var ws = wb.Worksheets[0];
var firstRow = ws.Cells["A1"].StringValue;
// ... validate and process rows
return Ok(new { sheets = wb.Worksheets.Count, firstCell = firstRow });
}Sử dụng LoadDiagnostics.HasRepairs để phát hiện các tệp cần sửa chữa cấu trúc và hiển thị thông tin đó cho người dùng.
Trích xuất Đường ống Dữ liệu
Trích xuất dữ liệu ô từ các tệp xuất XLSX do các hệ thống bên thứ ba tạo ra và đưa các giá trị vào cơ sở dữ liệu hoặc dịch vụ hạ nguồn. Đọc giá trị ô thông qua Cell.StringValue và Cell.Value bằng cách sử dụng tọa độ hàng và cột.
using Aspose.Cells_FOSS;
var wb = new Workbook("export.xlsx");
var ws = wb.Worksheets[0];
var records = new List<(string sku, double qty)>();
for (int row = 1; row <= 100; row++) // iterate known data range
{
var sku = ws.Cells[row, 0].StringValue;
if (string.IsNullOrEmpty(sku)) break;
var qty = (double)ws.Cells[row, 1].Value;
records.Add((sku, qty));
}
Console.WriteLine($"Extracted {records.Count} records");Áp dụng Kiểm tra Dữ liệu Trước khi Lưu
Thêm các quy tắc xác thực dạng danh sách thả xuống hoặc phạm vi trước khi cung cấp mẫu XLSX cho người dùng cuối, đảm bảo họ chỉ nhập các giá trị chấp nhận được.
using Aspose.Cells_FOSS;
var wb = new Workbook();
var ws = wb.Worksheets[0];
// Status column: dropdown
var statusVal = ws.Validations[ws.Validations.Add(CellArea.CreateCellArea("A2", "A100"))];
statusVal.Type = ValidationType.List;
statusVal.Formula1 = "\"Open,In Progress,Closed\"";
statusVal.InCellDropDown = true;
// Score column: 0–10 decimal range
var scoreVal = ws.Validations[ws.Validations.Add(CellArea.CreateCellArea("B2", "B100"))];
scoreVal.Type = ValidationType.Decimal;
scoreVal.Operator = OperatorType.Between;
scoreVal.Formula1 = "0";
scoreVal.Formula2 = "10";
scoreVal.ShowError = true;
wb.Save("template-with-validation.xlsx");Định dạng đầu ra để dễ đọc
Áp dụng các kiểu ô — phông chữ, màu nền, định dạng số, viền — để tạo ra đầu ra được tinh chỉnh. Tạo một thể hiện Style trực tiếp, cấu hình các thuộc tính của nó và áp dụng nó bằng Cell.SetStyle().
using Aspose.Cells_FOSS;
var wb = new Workbook();
var ws = wb.Worksheets[0];
// Header style
var headerStyle = new Style();
headerStyle.Font.IsBold = true;
headerStyle.Font.Size = 12;
headerStyle.ForegroundColor = System.Drawing.Color.FromArgb(0x4F, 0x81, 0xBD);
headerStyle.Pattern = FillPattern.Solid;
ws.Cells["A1"].PutValue("Name");
ws.Cells["B1"].PutValue("Score");
ws.Cells["A1"].SetStyle(headerStyle);
ws.Cells["B1"].SetStyle(headerStyle);
// Number format for score column
var numStyle = new Style();
numStyle.Custom = "0.00";
ws.Cells["B2"].SetStyle(numStyle);
ws.Cells["B2"].PutValue(98.5m);
wb.Save("styled-report.xlsx");