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.get_style-metoden, specificera stilstilarna och sedan tillämpa formateringen med hjälp av Cell.set_style-metoden. Ett exempel ges nedan för att visa denna metod att tillämpa olika formatering på en cell.

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Get the style from A1 cell
style = cell.get_style()
# Setting the vertical alignment
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Applying the style to A1 cell
cell.set_style(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 create_style-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å.

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Excel object
i = workbook.worksheets.add()
# Obtaining the reference of the first worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Adding a new Style
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Assigning the Style object to the "A1" cell
cell.set_style(style)
# Apply the same style to some other cells
worksheet.cells.get("B1").set_style(style)
worksheet.cells.get("C1").set_style(style)
worksheet.cells.get("D1").set_style(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, använd stilar via Aspose.Cells för Python via .NET API. Ett exempel nedan demonstrerar denna metod för att tillämpa en fördefinierad stil på en cell.

from aspose.cells import Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiate a new Workbook.
workbook = Workbook()
# Create a style object .
style = workbook.create_style()
# Input a value to A1 cell.
workbook.worksheets[0].cells.get("A1").put_value("Test")
# Apply the style to the cell.
workbook.worksheets[0].cells.get("A1").set_style(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 för Python via .NET stöder denna funktion också. Denna artikel förklarar hur man använder denna funktion effektivt.

Hur man formaterar valda tecken

Aspose.Cells för Python via .NET tillhandahåller en klass, Workbook som representerar en Microsoft Excel-fil. Workbook klassen innehåller en worksheets samling som ger å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.

from aspose.cells import Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first(default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the font of selected characters to bold
cell.characters(6, 7).font.is_bold = 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 åtgärda detta ger Aspose.Cells för Python via .NET ett enkelt, snabbt sätt som diskuteras i detalj i denna artikel.

Formatering av rader & kolumner

Aspose.Cells för Python via .NET tillhandahåller en klass, Workbook som representerar en Microsoft Excel-fil. Workbook klassen innehåller en worksheets samling som ger åtkomst till varje kalkylblad i Excel-filen. Ett kalkylblad representeras av Worksheet klassen. Worksheet klassen tillhandahåller en cells samling. cells samlingen ger en rows samling.

Hur man formaterar en rad

Varje objekt i rows-samlingen representerar ett Row-objekt. Row-objektet erbjuder metoden apply_style 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 create_style-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.
from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a row from the Rows collection
row = worksheet.cells.rows[0]
# Assigning the Style object to the Style property of the row
row.apply_style(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 apply_style-metoden för att formatera en kolumn.

from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a column from the Columns collection
column = worksheet.cells.columns[0]
# Applying the style to the column
column.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

Fortsatta ämnen