Casos d'ús per a Aspose.Cells FOSS per a .NET
Aspose.Cells FOSS per a .NET és una biblioteca .NET totalment gestionada que llegeix i escriu fitxers XLSX sense requerir Microsoft Office ni cap dependència externa. Els següents casos d’ús il·lustren on la biblioteca s’adapta a aplicacions .NET del món real.
Generació d’informes
Genereu informes XLSX de manera programàtica en aplicacions del costat del servidor o per lots. Utilitzeu Workbook, Worksheet i Cell.PutValue() per escriure dades estructurades, i després crideu Workbook.Save() per generar el fitxer.
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");Aquest patró escala a qualsevol nombre de files i pot ser alimentat per dades d’una base de dades o resposta d’API.
Processant fulls de càlcul carregats
Llegir i validar fitxers XLSX enviats per usuaris a través d’un formulari web o d’un punt final d’API. El constructor Workbook accepta un Stream, de manera que no cal escriure cap fitxer temporal al disc.
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 });
}Utilitzeu LoadDiagnostics.HasRepairs per detectar fitxers que requerien reparació estructural i mostrar aquesta informació als usuaris.
Extracció de la canalització de dades
Extreu dades de cel·les d’exportacions XLSX produïdes per sistemes de tercers i alimenta els valors a una base de dades o servei downstream. Llegeix els valors de les cel·les mitjançant Cell.StringValue i Cell.Value utilitzant coordenades de fila i columna.
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");Aplicació de la validació de dades abans de desar
Afegeix regles de validació de llista desplegable o d’interval abans de lliurar una plantilla XLSX als usuaris finals, assegurant que només introdueixin valors acceptables.
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");Estilitzar la sortida per a la llegibilitat
Aplica estils de cel·la — tipografies, colors de farciment, formats numèrics, vores — per produir una sortida polida. Crea una instància Style directament, configura les seves propietats i aplica-la amb 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");