Justeringsinställningar

Konfigurera justeringsinställningar

Justeringsinställningar i Microsoft Excel

Alla som har använt Microsoft Excel för att formatera celler kommer att vara bekanta med justeringsinställningarna i Microsoft Excel.

Som du kan se från figuren ovan, finns det olika typer av justeringsalternativ:

  • Textjustering (horisontell & vertikal)
  • Indrag.
  • Orientering.
  • Textkontroll.
  • Textriktning.

Alla dessa justeringsinställningar stöds fullt ut av Aspose.Cells och diskuteras mer detaljerat nedan.

Justeringsinställningar i Aspose.Cells

Aspose.Cells tillhandahåller GetStyle och SetStyle metoder för klassen Cell som används för att få och ställa in en cells formatering. Klassen Style tillhandahåller användbara egenskaper för att konfigurera justeringsinställningar.

Välj vilken som helst textjusteringstyp med hjälp av uppräkningen TextAlignmentType. De fördefinierade textjusteringstyperna i uppräkningen TextAlignmentType är:

Textjusteringstyper Beskrivning
Bottom Representerar bottenjustering av text
Center Representerar mittenjustering av text
CenterAcross Representerar mittenöverjustering av text
Distributed Representerar fördelad textjustering
Fill Representerar fyll textjustering
General Representerar generell textjustering
Justify Representerar rättfärdig textjustering
Left Representerar vänsterjustering av text
Right Representerar högerjustering av text
Top Representerar toppjustering av text
JustifiedLow Justerar texten med en justerad kashida-längd för arabisk text.
ThaiDistributed Distribuerar thailändsk text särskilt, eftersom varje tecken behandlas som ett ord.

Horisontell, vertikal justering och indrag

Använd egenskapen HorizontalAlignment för att justera texten horisontellt och egenskapen VerticalAlignment för att justera texten vertikalt. Det är möjligt att ställa in indelningsnivån för texten i en cell med hjälp av egenskapen IndentLevel och det påverkar endast när horisontell justering är vänster eller höger.

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.getStyle();
//Set text left horizontal alignment
style.setHorizontalAlignment(TextAlignmentType.RIGHT);
//Set indent
style.setIndentLevel(4);
//Set text top vertical alignment
style.setVerticalAlignment(TextAlignmentType.TOP);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

Orientering

Ställ in orienteringen (rotationen) för texten i en cell med hjälp av egenskapen RotationAngle.

// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.getStyle();
// Setting the rotation of the text (inside the cell) to 25
style.setRotationAngle(25);
cell.setStyle(style);
//Accessing the "A2" cell from the worksheet
cell = worksheet.getCells().get("A2");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A2" cell
style = cell.getStyle();
// Setting the orientation of the text from top to bottom
style.setRotationAngle(255);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

Textkontroll

I följande avsnitt diskuteras hur man kontrollerar text genom att ställa in textbrytning, krympa till passa och andra formateringsalternativ.

Textindrag

Textindrag i en cell gör det lättare att läsa: cellens höjd justeras för att passa all text istället för att klippa av den eller spilla över i intilliggande celler. Aktivera eller inaktivera textindrag med egenskapen IsTextWrapped.

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style
Style style = cell.getStyle();
// Wrap Cell's Text wrap
style.setTextWrapped( true);
//Set style.
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

Krympa passande

Ett alternativ till att göra textindrag i en cell är att minska textstorleken för att passa en cells dimensioner. Detta görs genom att ställa in egenskapen ShrinkToFit till true.

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style in the "A1" cell
Style style = cell.getStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.setShrinkToFit(true);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

Sammanfoga celler

Precis som Microsoft Excel stöder Aspose.Cells sammanfogning av flera celler till en. Aspose.Cells tillhandahåller två metoder för detta ändamål. Ett sätt är att anropa metoden Merge. Metoden tar följande parametrar för att sammanfoga cellerna:

  • Första rad: den första raden från vilken sammanfogningen ska börja.
  • Första kolumn: den första kolumnen från vilken sammanfogningen ska börja.
  • Antal rader: antalet rader att sammanfoga.
  • Antal kolumner: antalet kolumner att sammanfoga.
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.getCells();
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.getCells().get(5, 2).putValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.getCells().get(5, 2).getStyle();
// Create a Font object
Font font = style.getFont();
// Set the name.
font.setName("Times New Roman");
// Set the font size.
font.setSize(18);
// Set the font color
font.setColor(Color.getBlue());
// Bold the text
font.setBold(true);
// Make it italic
font.setItalic(true);
// Set the backgrond color of C6 Cell to Red
style.setForegroundColor(Color.getRed());
style.setPattern(BackgroundType.SOLID);
// Apply the Style to C6 Cell.
cells.get(5, 2).setStyle(style);

Textriktning

Det är möjligt att ställa in läsordningen för text i celler. Läsordningen är den visuella ordningen där tecken, ord osv. visas. Till exempel är engelska ett vänster-till-höger-språk medan arabiska är ett höger-till-vänster-språk.

Läsordningen ställs in med egenskapen TextDirection. Aspose.Cells tillhandahåller fördefinierade typer av textriktning i uppräkningen TextDirectionType.

Textriktningstyper Beskrivning
Context Läsordningen som är konsekvent med språket för det första inmatade tecknet
LeftToRight Vänster till höger-läsordning
RightToLeft Höger till vänster-läsordning
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("A1");
// Adding some value to the "A1" cell
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style in the "A1" cell
Style style = cell.getStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.setTextDirection(TextDirectionType.LEFT_TO_RIGHT);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

Fortsatta ämnen