Fyllningsinställningar

Färger och bakgrundsmönster

Microsoft Excel kan ställa in förgrund (omridning) och bakgrundsfärger (fyllning) för celler och bakgrundsmönster.

Aspose.Cells stöder även dessa funktioner på ett flexibelt sätt. I det här avsnittet lär vi oss att använda dessa funktioner med hjälp av Aspose.Cells.

Inställning av färger och bakgrundsmönster

Aspose.Cells tillhandahåller en klass, Workbook som representerar en Microsoft Excel-fil. Klassen Workbook innefattar en Worksheets-samling som tillåter åtkomst till varje kalkylblad i Excel-filen. Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en Cells-samling. Varje objekt i Cells-samlingen representerar ett objekt av klassen Cell.

The Cell har GetStyle och SetStyle metoder som används för att hämta och sätta en cells formatering. Klassen Style tillhandahåller egenskaper för att ange förgrund och bakgrundsfärger för cellerna. Aspose.Cells tillhandahåller en BackgroundType uppräkning som innehåller en uppsättning fördefinierade typer av bakgrundsmönster som ges nedan.

Bakgrundsmönster Beskrivning
DiagonalCrosshatch Representerar diagonalt kryssmönster
DiagonalStripe Representerar diagonalt rutmönster
Gray6 Representerar 6,25% grått mönster
Gray12 Representerar 12,5% grått mönster
Gray25 Representerar 25% grått mönster
Gray50 Representerar 50% grått mönster
Gray75 Representerar 75% grått mönster
HorizontalStripe Representerar horisontellt rutmönster
None Representerar ingen bakgrund
ReverseDiagonalStripe Representerar omvänd diagonalt rutmönster
Solid Representerar enfärgat mönster
ThickDiagonalCrosshatch Representerar tjockt diagonalt kryssmönster
ThinDiagonalCrosshatch Representerar tunt diagonalt kryssmönster
ThinDiagonalStripe Representerar tunt diagonalt rutmönster
ThinHorizontalCrosshatch Representerar tunt horisontellt kryssmönster
ThinHorizontalStripe Representerar tunt horisontellt rutmönster
ThinReverseDiagonalStripe Representerar tunt omvänt diagonalt rutmönster
ThinVerticalStripe Representerar tunt vertikalt rutmönster
VerticalStripe Representerar vertikalt rutmönster

I exemplet nedan är förgrundsfärgen för cellen A1 inställd men A2 är konfigurerad för att ha både förgrund och bakgrundsfärger med ett bakgrundsmönster med vertikal rand.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Define a Style and get the A1 cell style
Style style = worksheet.Cells["A1"].GetStyle();
// Setting the foreground color to yellow
style.ForegroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);
// Get the A2 cell style
style = worksheet.Cells["A2"].GetStyle();
// Setting the foreground color to blue
style.ForegroundColor = Color.Blue;
// Setting the background color to yellow
style.BackgroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Viktigt att veta

Tillämpning av gradientfylleffekter

För att applicera önskade gradientfylleffekter på cellen, använd Style objektets SetTwoColorGradient metod enligt behov.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.Worksheets[0];
// Input a value into B3 cell
worksheet.Cells[2, 1].PutValue("test");
// Get the Style of the cell
Style style = worksheet.Cells["B3"].GetStyle();
// Set Gradient pattern on
style.IsGradient = true;
// Specify two color gradient fill effects
style.SetTwoColorGradient(Color.FromArgb(255, 255, 255), Color.FromArgb(79, 129, 189), GradientStyleType.Horizontal, 1);
// Set the color of the text in the cell
style.Font.Color = Color.Red;
// Specify horizontal and vertical alignment settings
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
// Apply the style to the cell
worksheet.Cells["B3"].SetStyle(style);
// Set the third row height in pixels
worksheet.Cells.SetRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.Cells.Merge(2, 1, 1, 2);
// Save the Excel file
workbook.Save(dataDir + "output.xlsx");

Färger och Palett

En palett är antalet färger som är tillgängliga för att skapa en bild. Användningen av en standardiserad palett i en presentation gör att användaren kan skapa en enhetlig look. Varje Microsoft Excel (97-2003) fil har en palett med 56 färger som kan tillämpas på celler, teckensnitt, rutnät, grafiska objekt, fyllningar och linjer i en graf.

Med Aspose.Cells är det möjligt att inte bara använda palettens befintliga färger utan också anpassade färger. Innan du använder en anpassad färg, lägg till den först i paletten.

Detta ämne diskuterar hur man lägger till anpassade färger i paletten.

Lägga till Anpassade Färger i Paletten

Aspose.Cells stöder Microsoft Excels 56-färgspalett. För att använda en anpassad färg som inte är definierad i paletten, lägg till färgen i paletten.

Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Klassen Workbook tillhandahåller en ChangePalette-metod som tar följande parametrar för att lägga till en anpassad färg för att ändra paletten:

  • Anpassad färg, den anpassade färgen som ska läggas till.
  • Index, index för färgen i paletten som den anpassade färgen kommer att ersätta. Ska vara mellan 0-55.

Exemplet nedan lägger till en anpassad färg (Orchid) i paletten innan den tillämpas på en font.

// Instantiating an Workbook object
Workbook workbook = new Workbook();
//Checks if a color is in the palette for the spreadsheet.
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.ChangePalette(Color.Orchid, 55);
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining new Style object
Style styleObject = workbook.CreateStyle();
// Setting the Orchid (custom) color to the font
styleObject.Font.Color = workbook.Colors[55];
// Applying the style to the cell
cell.SetStyle(styleObject);
// Saving the Excel file
workbook.Save("out.xlsx");