Formatera celler

Introduktion

Hur man formaterar celler med hjälp av GetStyle och SetStyle-metoderna

Tillämpa olika typer av formateringsstilar på celler för att ställa in bakgrund eller förgrundsfärger, ramar, typsnitt, horisontell och vertikal justering, indenteringsnivå, textriktning, rotationsvinkel och mycket mer.

Hur man använder GetStyle och SetStyle-metoderna

Om utvecklare behöver tillämpa olika formateringsstilar på olika celler är det bättre att hämta Style för cellen med hjälp av Cell.GetStyle-metoden, specificera stilstilarna och sedan tillämpa formateringen med hjälp av Cell.SetStyle-metoden. Ett exempel ges nedan för att visa denna metod 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");

Hur man använder Style-objektet för att formatera olika celler

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

  1. Lägg till ett Style-objekt genom att anropa CreateStyle-metoden från Workbook-klassen
  2. Få tillgång till det nytt tillagda Style-objektet
  3. Ange de önskade egenskaperna/attributen för Style-objektet för att tillämpa önskade formateringsinställningar
  4. Tilldela konfigurerat Style-objekt till önskade celler

Denna metod kan avsevärt förbättra effektiviteten i 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");

Hur man använder Microsoft Excel 2007 Fördefinierade Stilar

Om du behöver tillämpa olika formateringsstilar för Microsoft Excel 2007, tillämpa stilar med hjälp av Aspose.Cells API. Ett exempel ges nedan för att visa denna metod 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");

Hur man formaterar valda tecken i en cell

Hantera typsnittsinstä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 vill formatera endast utvalda tecken?

Aspose.Cells stöder också denna funktion. Detta ämne förklarar hur vi använder denna funktion effektivt.

Hur man formaterar valda tecken

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

Cell-klassen tillhandahåller Characters-metoden som tar följande parametrar för att välja ett teckenintervall inne i en cell:

  • Startindex, index för tecknet som urvalet börjar från.
  • Antal tecken, antalet tecken att välja.

Characters-metoden returnerar en instans av FontSetting-klassen som låter utvecklare formatera tecknen på samma sätt som de skulle göra med en cell enligt exemplet nedan. I utdatat endast ordet ‘Besök’ i cellen A1 kommer att formateras med standardtypsnittet men ‘Aspose!’ är fetstil och blått.

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

Hur man formaterar rader och kolumner

Ibland behöver utvecklare tillämpa samma formatering på rader eller kolumner. Att tillämpa formatering på celler en i taget tar ofta längre tid och är inte en bra lösning. För att lösa detta problem tillhandahåller Aspose.Cells ett enkelt, snabbt sätt som diskuteras utförligt i den här artikeln.

Formatering av rader & kolumner

Aspose.Cells tillhandahåller en klass, Workbook som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en Worksheets-samling som gör det möjligt att komma åt varje kalkylblad i Excel-filen. Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en Cells-samling. Cells-samlingen tillhandahåller en Rows-samling.

Hur man formaterar en rad

Varje objekt i Rows-samlingen representerar ett Row-objekt. Row-objektet erbjuder metoden ApplyStyle som används för att ange radens formatering. För att tillämpa samma formatering på en rad använder man Style-objektet. Nedanstående steg visar hur man använder det.

  1. Lägg till ett Style-objekt i Workbook-klassen genom att anropa dess CreateStyle-metod.
  2. Ange Style-objektets egenskaper för att tillämpa formateringsinställningar.
  3. Gör de relevanta attributen PÅ för StyleFlag-objektet.
  4. Tilldela det konfigurerade Style-objektet till Row-objektet.
// 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");

Hur man formaterar en kolumn

Cells-samlingen tillhandahåller även en Columns-samling. Varje objekt i Columns-samlingen representerar ett Column-objekt. Liknande ett Row-objekt erbjuder även Column-objektet ApplyStyle-metoden 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");

Fortsatta ämnen