Casi d'uso per Aspose.Cells FOSS per .NET

Casi d'uso per Aspose.Cells FOSS per .NET

Aspose.Cells FOSS for .NET è una libreria .NET gestita pura che legge e scrive file XLSX senza richiedere Microsoft Office o alcuna dipendenza esterna. I seguenti casi d’uso illustrano dove la libreria si inserisce nelle applicazioni .NET del mondo reale.


Generazione di report

Genera report XLSX in modo programmatico in applicazioni lato server o batch. Usa Workbook, Worksheet e Cell.PutValue() per scrivere dati strutturati, quindi chiama Workbook.Save() per produrre il file.

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");

Questo modello si adatta a qualsiasi numero di righe e può essere alimentato da dati provenienti da un database o da una risposta API.


Elaborazione dei fogli di calcolo caricati

Leggi e valida i file XLSX inviati dagli utenti tramite un modulo web o un endpoint API. Il costruttore Workbook accetta un Stream, quindi non è necessario scrivere alcun file temporaneo su disco.

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 });
}

Usa LoadDiagnostics.HasRepairs per rilevare i file che richiedono riparazioni strutturali e mostrare tali informazioni agli utenti.


Estrazione del Data Pipeline

Estrai i dati delle celle dalle esportazioni XLSX generate da sistemi di terze parti e inserisci i valori in un database o in un servizio a valle. Leggi i valori delle celle tramite Cell.StringValue e Cell.Value usando le coordinate di riga e colonna.

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");

Applicare la convalida dei dati prima del salvataggio

Aggiungi regole di convalida a menu a tendina o di intervallo prima di consegnare un modello XLSX agli utenti finali, assicurandoti che inseriscano solo valori accettabili.

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");

Formattazione dell’output per la leggibilità

Applica stili di cella — caratteri, colori di riempimento, formati numerici, bordi — per produrre un output rifinito. Crea un’istanza Style direttamente, configura le sue proprietà e applicala con 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");

Vedi anche

 Italiano