Introducere

Aspose.Cells FOSS for .NET is a pure managed-code, MIT-licensed library for reading, writing, şi manipularea Excel-ului. .xlsx cartile de lucru fără dependență Microsoft Office. Instalat ca în cazul Aspose.Cells_FOSS NuGet pachet, se integrează în orice proiect .NET aplicații de console, web API-uri, lucrători în fundal şi funcţii fără server.

Acest articol oferă o prezentare caracteristică-pe-feature a API public, arătând ce este posibil și cum să invocați fiecare capacitate din codul C #.


Opinie generală asupra caracteristicilor

Cartea de lucru și fișele de muncă Ciclul vieții

Punctul de intrare pentru fiecare operaţiune este: Workbook Construieste unul de la zero cu new Workbook(), sau încărcaţi o instalaţie existentă. .xlsx cu: new Workbook(filePath) sau de: new Workbook(stream).Navigarea fișelor prin: Workbook.Worksheets, setate Worksheet.Name și de: Worksheet.VisibilityType, şi persistă cu Workbook.Save().Pentru fişierele deteriorate, treceţi un LoadOptions În cazul în care: TryRepairPackage = true și de: TryRepairXml = true.

using Aspose.Cells_FOSS;

var options = new LoadOptions
{
    TryRepairPackage = true,
    TryRepairXml = true,
};

try
{
    _ = new Workbook("sample.xlsx", options);
}
catch (WorkbookLoadException exception)
{
    Console.WriteLine(exception.Message);
}

Datele și formulele celulelor

Scriu valori în celule cu: Cell.PutValue() acceptarea supraîncărcăturilor string, int, double, decimal, bool, și DateTime. Citeste valorile înapoi prin intermediul: Cell.Value, Cell.StringValue, și Cell.Formula.Setare formule Excel cu Cell.Formula proprietate; șirul de formule este: conservat la salvare și supraviețuiește unui ciclu de reîncărcare.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Cells["A1"].PutValue("Hello");
sheet.Cells["B1"].PutValue(123);
sheet.Cells["C1"].Formula = "=B1*2";

workbook.Save("hello.xlsx");

var loaded = new Workbook("hello.xlsx");
Console.WriteLine(loaded.Worksheets[0].Cells["C1"].Formula);
Console.WriteLine(loaded.Worksheets[0].Cells["C1"].StringValue);

Styling de celule

Aplicarea Style Obiecte pentru a controla fontul, umplerea, marjele, formatul numeric și alinierea. Obține stilul curent cu: Cell.GetStyle(), mutate-l, apoi să se angajeze cu Cell.SetStyle().Pentru umplerea fundalului, setat Style.Pattern = FillPattern.Solid și să atribuie o Color la … în Style.ForegroundColor.- Foloseste-l . Cells.Merge() să se întindă pe celule.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var cell = workbook.Worksheets[0].Cells["A1"];
cell.PutValue("Styled");

var style = cell.GetStyle();
style.Font.Bold = true;
style.Pattern = FillPattern.Solid;
style.ForegroundColor = Color.FromArgb(255, 241, 196, 15);
cell.SetStyle(style);

Console.WriteLine($"{cell.StringValue} / Bold={cell.GetStyle().Font.Bold}");

Formatare condiționată

Atașează regulile de formatare condiționată la intervale de celule prin: ConditionalFormattingCollection. Suport al normelor FormatConditionType.CellValue, Expression, ColorScale, DataBar, și IconSet.- Foloseste-l . OperatorType să specifice operatorii de comparație (Between, Equal, GreaterThan, etc.), să atribuie o Style la celulele potrivite și să configureze prioritatea și comportamentul de oprire în cazul adevărului per regulă.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];

var cfCollection = sheet.ConditionalFormattings[sheet.ConditionalFormattings.Add()];
cfCollection.AddArea(CellArea.CreateCellArea("A1", "A10"));
var rule = cfCollection[cfCollection.AddCondition(
    FormatConditionType.CellValue, OperatorType.Between, "3", "7")];
var style = rule.Style;
style.Pattern = FillPattern.Solid;
style.ForegroundColor = Color.FromArgb(255, 255, 199, 206);
style.Font.Bold = true;
rule.Style = style;

workbook.Save("conditional-formatting.xlsx");

Validarea datelor

Adăugați restricții de intrare la celule folosind Worksheet.Validations.Tipuri de validare acceptate includ: ValidationType.List, Decimal, Custom,Configurează instrucţiunile de intrare (InputTitle, InputMessage) și mesajele de eroare (ErrorTitle, ErrorMessage, ValidationAlertType) Utilizare CellArea.CreateCellArea() pentru a defini intervalul validat și Validation.AddArea() să aplice o regulă la intervale suplimentare.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Name = "Validation Sheet";

var listIdx = sheet.Validations.Add(CellArea.CreateCellArea("A1", "A3"));
var listVal = sheet.Validations[listIdx];
listVal.Type = ValidationType.List;
listVal.Formula1 = "\"Open,Closed\"";
listVal.IgnoreBlank = true;
listVal.InCellDropDown = true;
listVal.ShowInput = true;
listVal.InputTitle = "Status";
listVal.InputMessage = "Pick a status";
listVal.ShowError = true;
listVal.ErrorTitle = "Invalid";
listVal.ErrorMessage = "Choose from the list";

workbook.Save("validations-sample.xlsx");

Opțiuni de configurare a paginii și imprimare

Configurați layout-ul de imprimare prin: Worksheet.PageSetup.- stabilirea marjelor (LeftMarginInch, RightMarginInch, TopMarginInch, BottomMarginInch), Orientation, PaperSize, scară, Printarea, rândurile și coloanele de titlu, capetele și picioarele paginii și pauza. Toate setările supraviețuiesc o călătorie de întoarcere cu un număr redus.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Name = "Print Sheet";
sheet.Cells["A1"].PutValue("Title");

var pageSetup = sheet.PageSetup;
pageSetup.Orientation = PageOrientationType.Landscape;
pageSetup.PaperSize = PaperSizeType.PaperA4;
pageSetup.PrintArea = "$A$1:$C$10";
pageSetup.PrintTitleRows = "$1:$2";
pageSetup.LeftHeader = "Left Header";
pageSetup.CenterFooter = "Center Footer";
pageSetup.PrintGridlines = true;
pageSetup.CenterHorizontally = true;

workbook.Save("page-setup-sample.xlsx");

Setări și protecție a fișei de lucru

Display de control pe folie cu: Worksheet.ShowGridlines, ShowRowColumnHeaders, ShowZeros, RightToLeft, Zoom, și TabColor. Protejează foile cu: Worksheet.Protect() și configurează granulat fin Protection steaguri (Objects, FormatCells, InsertRows, AutoFilter, SelectLockedCells) Gestionarea înălțimii rândurilor, lățime a coloanelor, linii/colonele ascunse și regiuni fusionate prin intermediul Cells.Rows, Cells.Columns, și Cells.Merge().

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var layout = workbook.Worksheets[0];
layout.Name = "Layout";
layout.VisibilityType = VisibilityType.Hidden;
layout.TabColor = Color.FromArgb(255, 34, 68, 102);
layout.ShowGridlines = false;
layout.Zoom = 85;
layout.Protect();
layout.Protection.FormatCells = true;
layout.Protection.InsertRows = true;

layout.Cells["A1"].PutValue("Merged");
layout.Cells.Rows[1].Height = 22.5d;
layout.Cells.Columns[0].Width = 18.25d;
layout.Cells.Merge(0, 0, 2, 2);

workbook.Save("worksheet-settings-sample.xlsx");

Adăugați exterioare, interne și mailto: hiperliguri prin intermediul: HyperlinkCollection.Add().- Setat . Hyperlink.TextToDisplay și de: Hyperlink.ScreenTip pentru etichetele vizibile utilizatorului. Intervale la scara cărții de lucru sau a fișei folosind: DefinedNameCollection.Add() intervalele numite servesc ca Ancorarea formulă stabilă și sursele de date ale diagramelor care supraviețuiesc încărcării fișierului.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];

sheet.Cells["A1"].PutValue("Docs");
var link = sheet.Hyperlinks[sheet.Hyperlinks.Add("A1", 1, 1, "https://example.com/docs")];
link.TextToDisplay = "Docs";
link.ScreenTip = "External documentation";

var name = workbook.DefinedNames[workbook.DefinedNames.Add("PrimaryRange", "='Sheet1'!$A$1:$D$5")];
name.Comment = "Primary data range";

workbook.Save("hyperlinks-names.xlsx");
Console.WriteLine("Hyperlinks: " + sheet.Hyperlinks.Count);
Console.WriteLine("Defined names: " + workbook.DefinedNames.Count);

Început rapid

dotnet add package Aspose.Cells_FOSS
using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Cells["A1"].PutValue("Hello");
sheet.Cells["B1"].PutValue(123);
sheet.Cells["C1"].Formula = "=B1*2";
workbook.Save("output.xlsx");

Formatele suportate

FormatulExtindereCiteşte.Scrie .
Xlsx.xlsx
Auto-încărcat.(diverse)

Open Source & Licențiere

Aspose.Cells FOSS for .NET is published under the MIT license and distributed via NuGet as Aspose.Cells_FOSS.Este potrivit atât pentru proiecte open-source, cât și pentru cele comerciale.


Cum să începem?