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 en klass, Workbook, som representerar en Excel-fil. Workbook klassen innehåller en Worksheets samling som tillåter åtkomst till varje arbetsblad i Excel-filen. Ett arbetsblad representeras av Worksheet klassen. Worksheet klassen tillhandahåller en Cells samling. Varje objekt i Cells samlingen representerar ett objekt av Cell klassen.

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 justering

Använd Style objektets HorizontalAlignment egenskap för att justera texten horisontellt.

// 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 worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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();
style.HorizontalAlignment = TextAlignmentType.Center;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Vertikal justering

Liknande horisontell justering, använd Style objektets VerticalAlignment egenskap för att justera texten vertikalt.

// 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();
// Clearing all the worksheets
workbook.Worksheets.Clear();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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 vertical alignment of the text in a cell
style.VerticalAlignment = TextAlignmentType.Center;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Indrag

Det är möjligt att ange indelningsnivån för texten i en cell med Style objektets IndentLevel egenskap.

// 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 worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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 indentation level of the text (inside the cell) to 2
style.IndentLevel = 2;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Orientering

Ange orienteringen (rotationen) för texten i en cell med Style objektets RotationAngle egenskap.

// 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 worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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.RotationAngle = 25;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Textkontroll

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

Textindrag

Att rada text 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 få den att rinna över i intilliggande celler. Ange textombrytning på eller av med Style objektets IsTextWrapped egenskap.

// 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 Workbook Object
Workbook wb = new Workbook();
// Open first Worksheet in the workbook
Worksheet ws = wb.Worksheets[0];
// Get Worksheet Cells Collection
Aspose.Cells.Cells cell = ws.Cells;
// Increase the width of First Column Width
cell.SetColumnWidth(0, 35);
// Increase the height of first row
cell.SetRowHeight(0, 36);
// Add Text to the Firts Cell
cell[0, 0].PutValue("I am using the latest version of Aspose.Cells to test this functionality");
// Make Cell's Text wrap
Style style = cell[0, 0].GetStyle();
style.IsTextWrapped = true;
cell[0, 0].SetStyle(style);
// Save Excel File
wb.Save(dataDir+ "WrappingText.out.xlsx");
Krympa passande

Ett alternativ för att rada text i ett fält är att krympa textstorleken för att passa en cells dimensioner. Detta görs genom att ställa in Style objektets IsTextWrapped egenskap till true.

// 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 worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["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();
// Shrinking the text to fit according to the dimensions of the cell
style.ShrinkToFit = true;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);
Sammanfoga celler

Liksom Microsoft Excel stöder Aspose.Cells att sammanfoga flera celler till en. Aspose.Cells tillhandahåller två tillvägagångssätt för denna uppgift. Ett sätt är att anropa Cells samlingen Merge metod. 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.
// 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);
// Create a Workbook.
Workbook wbk = new Workbook();
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.Worksheets[0];
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.Cells;
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.Merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.Cells[5, 2].PutValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.Cells[5, 2].GetStyle();
// Create a Font object
Font font = style.Font;
// Set the name.
font.Name = "Times New Roman";
// Set the font size.
font.Size = 18;
// Set the font color
font.Color = System.Drawing.Color.Blue;
// Bold the text
font.IsBold = true;
// Make it italic
font.IsItalic = true;
// Set the backgrond color of C6 Cell to Red
style.ForegroundColor = System.Drawing.Color.Red;
style.Pattern = BackgroundType.Solid;
// Apply the Style to C6 Cell.
cells[5, 2].SetStyle(style);
// Save the Workbook.
wbk.Save(dataDir + "mergingcells.out.xls");

Det andra sättet är att först anropa Cells samlingen CreateRange metod för att skapa en räckvidd av celler som ska sammanfogas. CreateRange metoden tar samma parametrar som Merge metoden som diskuterades ovan och returnerar ett Range objekt. Range objektet tillhandahåller också en Merge metod som sammanfogar den angivna räckvidden i Range objektet.

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 Style objektets TextDirection egenskap. Aspose.Cells tillhandahåller fördefinierade textriktningstyper i TextDirectionType uppräkning.

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.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["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.TextDirection = (TextDirectionType.LeftToRight);
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("book1.xlsx");

Fortsatta ämnen