Formattazione delle celle

Formatta la cella o l’intervallo di celle

Se desideri formattare la cella o l’intervallo di celle, Aspose.Cells fornisce la classe Style. Puoi completare tutta la formattazione della cella o dell’intervallo di celle utilizzando questa classe. Alcune delle cose relative alla formattazione che possono essere realizzate con la classe IStyle sono le seguenti

  • Imposta il colore di riempimento della cella
  • Imposta il testo a capo della cella
  • Imposta i bordi delle celle come i bordi superiore, sinistro, inferiore e destro, ecc.
  • Imposta il colore del font, la dimensione del font, il nome del font, la cancelletto, grassetto, corsivo, sottolineato, ecc.
  • Imposta l’allineamento del testo orizzontale o verticale a destra, sinistra, in alto, in basso, al centro, ecc.

Se si desidera impostare lo stile di una singola cella, utilizzare il metodo Cell->SetStyle() e se si desidera impostare lo stile di un intervallo di celle, utilizzare il metodo Range->ApplyStyle().

Codice di Esempio

Il seguente codice di esempio formatta la cella C4 del foglio di lavoro in vari modi e la schermata mostra il file Excel di output generato per il riferimento.

todo:image_alt_text

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of output excel file
U16String outputFormatCellOrRangeOfCells = outPath + "outputFormatCellOrRangeOfCells.xlsx";
//Create a new workbook
Workbook wb;
//Get first worksheet which is created by default
Worksheet ws = wb.GetWorksheets().Get(0);
//Access cell C4 by cell name
Cell cell = ws.GetCells().Get(u"C4");
//Add some text in cell
cell.PutValue((U16String)u"This is sample data.");
//Access the cell style
Style st = cell.GetStyle();
//Fille the cell color to Yellow
st.SetPattern(BackgroundType::Solid);
st.SetForegroundColor(Color{ 0xff, 0xff, 0xff, 0 });//Yellow
//Set the text to wrapp
st.SetIsTextWrapped(true);
//Set the left and right border to Red
st.SetBorder(BorderType::LeftBorder, CellBorderType::Thick, Color{ 0xff, 0xff, 0, 0 });//Red
st.SetBorder(BorderType::RightBorder, CellBorderType::Thick, Color{ 0xff, 0xff, 0, 0 });//Red
//Set font color, font size, strike, bold, italic
st.GetFont().SetColor(Color{ 0xff, 0, 0, 0xff });//Blue
st.GetFont().SetSize(16);
st.GetFont().SetStrikeType(TextStrikeType::Single);
st.GetFont().SetIsBold(true);
st.GetFont().SetIsItalic(true);
//Set text horizontal and vertical alignment to center
st.SetHorizontalAlignment(TextAlignmentType::Center);
st.SetVerticalAlignment(TextAlignmentType::Center);
//Set the cell style
cell.SetStyle(st);
//Set the cell column width and row height
ws.GetCells().SetColumnWidth(cell.GetColumn(), 20);
ws.GetCells().SetRowHeight(cell.GetRow(), 70);
//Save the output excel file
wb.Save(outputFormatCellOrRangeOfCells);
Aspose::Cells::Cleanup();