Formatera celler

Introduktion

Formatera Cells med metoderna GetStyle och SetStyle

Använd olika typer av formateringsstilar på celler för att ställa in bakgrunds- eller förgrundsfärger, ramar, teckensnitt, horisontella och vertikala justeringar, indragsnivå, textriktning, rotationsvinkel och mycket mer.

Använda metoderna GetStyle och SetStyle

Om utvecklare behöver tillämpa olika formateringsstilar på olika celler är det bättre att skaffaStil av cellen som använderCell.GetStyle metod, ange stilattributen och tillämpa sedan formateringen medCell.SetStylemetod. Ett exempel ges nedan för att demonstrera detta tillvägagångssätt för att tillämpa olika formatering på en cell.

// 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();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining a Style object
Aspose.Cells.Style style;
// Get the style from A1 cell
style = cell.GetStyle();
// Setting the vertical alignment
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text
style.Font.Color = Color.Green;
// Setting to shrink according to the text contained in it
style.ShrinkToFit = true;
// Setting the bottom border color to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Applying the style to A1 cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Använda stilobjekt för att formatera olika Cells

Om utvecklare behöver tillämpa samma formateringsstil på olika celler kan de användaStil objekt. Följ stegen nedan för att användaStilobjekt:

  1. Lägg till enStil objekt genom att anropaSkapa stil metod förArbetsbokklass
  2. Få tillgång till den nyligen tillagdaStilobjekt
  3. Ställ in önskade egenskaper/attribut förStilobjekt för att tillämpa önskade formateringsinställningar
  4. Tilldela den konfigureradeStilobjekt till dina önskade celler

Detta tillvägagångssätt kan avsevärt förbättra effektiviteten för dina applikationer och spara minne också.

// 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 Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the first 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!");
// Adding a new Style
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Assigning the Style object to the "A1" cell
cell.SetStyle(style);
// Apply the same style to some other cells
worksheet.Cells["B1"].SetStyle(style);
worksheet.Cells["C1"].SetStyle(style);
worksheet.Cells["D1"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Använder Microsoft Excel 2007 fördefinierade stilar

Om du behöver använda olika formateringsstilar för Microsoft Excel 2007, tillämpa stilar med hjälp av Aspose.Cells API. Ett exempel ges nedan för att demonstrera detta tillvägagångssätt för att tillämpa en fördefinierad stil på en cell.

// 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);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Create a style object .
Style style = workbook.CreateStyle();
// Input a value to A1 cell.
workbook.Worksheets[0].Cells["A1"].PutValue("Test");
// Apply the style to the cell.
workbook.Worksheets[0].Cells["A1"].SetStyle(style);
// Save the Excel 2007 file.
workbook.Save(dataDir + "book1.out.xlsx");

Formatera valda tecken i en Cell

Att hantera teckensnittsinställningar förklarar hur man formaterar text i celler, men det förklarar bara hur man formaterar allt cellinnehåll. Vad händer om du bara vill formatera valda tecken?

Aspose.Cells stöder också denna funktion. Det här ämnet förklarar hur vi använder den här funktionen effektivt.

Formatera valda tecken

Aspose.Cells tillhandahåller en klass,Arbetsbok som representerar en Microsoft Excel-fil. DeArbetsbok klass innehållerArbetsblad samling som ger åtkomst till varje kalkylblad i en Excel-fil. Ett arbetsblad representeras avArbetsblad klass. DeArbetsblad klass ger enCells samling. Varje objekt iCells samlingen representerar ett föremål förCellklass.

DeCell klass gerTeckenmetod som använder följande parametrar för att välja ett teckenintervall inuti en cell:

  • Starta index, indexet för tecknet som valet startar från.
  • Antal tecken, antalet tecken att välja.

DeTecken metod returnerar en instans avFontSettingklass som tillåter utvecklare att formatera tecknen på samma sätt som de skulle göra en cell som visas nedan i kodexemplet. I utdatafilen, i A1-cellen, kommer ordet ‘Besök’ att formateras med standardteckensnittet men ‘Aspose!’ är fet och blå.

// 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();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the font of selected characters to bold
cell.Characters(6, 7).Font.IsBold = true;
// Setting the font color of selected characters to blue
cell.Characters(6, 7).Font.Color = Color.Blue;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Formatera rader och kolumner

Ibland måste utvecklare använda samma formatering på rader eller kolumner. Att tillämpa formatering på celler en efter en tar ofta längre tid och är ingen bra lösning. För att lösa detta problem erbjuder Aspose.Cells ett enkelt och snabbt sätt som diskuteras i detalj i den här artikeln.

Formatera rader och kolumner

Aspose.Cells tillhandahåller en klass, denArbetsbok som representerar en Microsoft Excel-fil. DeArbetsbok klass innehåller enArbetsblad samling som ger åtkomst till varje kalkylblad i Excel-filen. Ett arbetsblad representeras avArbetsblad klass. DeArbetsblad klass ger enCells samling. DeCellssamling ger enRadersamling.

Formatera en rad

Varje objekt iRader samlingen representerar enRad objekt. DeRadobjekt erbjuderApplyStyle metod som används för att ställa in radens formatering. För att tillämpa samma formatering på en rad, användStilobjekt. Stegen nedan visar hur du använder den.

  1. Lägg till enStil invända motArbetsbok klass genom att anropa dessSkapa stilmetod.
  2. Ställ inStilobjektets egenskaper för att tillämpa formateringsinställningar.
  3. Gör de relevanta attributen PÅ förStilFlaggaobjekt.
  4. Tilldela den konfigureradeStil invända motRadobjekt.
// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a row from the Rows collection
Row row = worksheet.Cells.Rows[0];
// Assigning the Style object to the Style property of the row
row.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Formatera en kolumn

DeCells samling ger också enKolumner samling. Varje objekt iKolumner samlingen representerar enKolumn objekt. Liknar enRad objekt, denKolumn objekt erbjuder ocksåApplyStylemetod för att formatera en kolumn.

// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a column from the Columns collection
Column column = worksheet.Cells.Columns[0];
// Applying the style to the column
column.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Förhandsämnen