[15] # How to Set Cell Border & Alignment in C# with IronXL
IronXL kullanarak C# Excel dosyalarında hücre kenarlıklarını ve metin hizalamasını ayarlayın. Bunu yapmak için, Microsoft Interop bağımlılıkları olmadan MediumDashed gibi kenarlık türlerini ve HorizontalAlignment.Center gibi hizalama özelliklerini doğrudan çalışma sayfası hücrelerine uygulayın.
[17] In Excel, cell borders are lines or borders that can be applied to individual cells or groups of cells, while text alignment refers to the positioning of text within a cell both vertically and horizontally. [18] IronXL provides a comprehensive API for working with Excel cell styles that allows you to programmatically control these visual elements with precision.
[19] Quickstart: Set Cell Borders & Center Text Alignment with One Call
[20] Apply a border style to one side and center text horizontally in just two lines. [21] Get up and running fast without Interop overhead.
-
IronXL aşağıdaki NuGet Paket Yöneticisi ile yükleyin
PM > Install-Package IronXL.Excel -
Bu kod parçacığını kopyalayın ve çalıştırın.
workSheet["B2"].Style.LeftBorder.Type = IronXl.Styles.BorderType.MediumDashed; workSheet["B2"].Style.HorizontalAlignment = IronXl.Styles.HorizontalAlignment.Center; -
Canlı ortamınızda test için dağıtım yapın
Ücretsiz deneme ile bugün projenizde IronXL kullanmaya başlayın
Asgari İş Akışı (5 adım)
- Arka plan deseni ve renk ayarlamak için C# kütüphanesini indirin
- Var olan bir Excel çalışma sayfasını açın veya yeni bir tane oluşturun.
- [22] Set the Type property of the border to one of the available border types in BorderType
- [23] Align horizontally or vertically using the HorizontalAlignment and VerticalAlignment properties
- [24] Set the border color from an available type or a Hex Color code
[25] Get started with IronXL.
[26] ## How Do I Set Cell Borders and Text Alignment in a Basic Example?
TopBorder, RightBorder, BottomBorder ve LeftBorder özelliklerini kullanarak kenarlıklar ekleyerek seçilen bir hücrenin, sütunun, satırın veya aralığın görünümünü özelleştirin. [BorderType] enumunda bulunan çeşitli stillerden birini seçin. [29] Explore all available border types to find the perfect match.
IronXl.Styles.BorderType HorizontalAlignment VerticalAlignment IronXl.Styles.HorizontalAlignment IronXl.Styles.VerticalAlignment
Metnin hizalamasını hassas bir şekilde ayarlamak için, Style içindeki [HorizontalAlignment] ve [VerticalAlignment] özelliklerini düzenleyerek istenen düzeni elde edin. İstenen hizalamayı ayarlamak için [HorizontalAlignment] ve [VerticalAlignment] enumlarını kullanın. [32] Discover all available alignment types to present your data flawlessly. [33] This approach is particularly useful when creating professional spreadsheets that require consistent formatting.
[34] Before applying borders and alignment, ensure you have loaded your Excel workbook properly. [35] Here's a complete example demonstrating the basic border and alignment setup:
:path=/static-assets/excel/content-code-examples/how-to/border-alignment-set-border-alignment.cs
using IronXL;
using IronXL.Styles;
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet["B2"].Value = "B2";
// Set cell border
workSheet["B2"].Style.LeftBorder.Type = BorderType.MediumDashed;
workSheet["B2"].Style.RightBorder.Type = BorderType.MediumDashed;
// Set text alignment
workSheet["B2"].Style.HorizontalAlignment = HorizontalAlignment.Center;
workBook.SaveAs("setBorderAndAlignment.xlsx");
Imports IronXL
Imports IronXL.Styles
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private workSheet("B2").Value = "B2"
' Set cell border
Private workSheet("B2").Style.LeftBorder.Type = BorderType.MediumDashed
Private workSheet("B2").Style.RightBorder.Type = BorderType.MediumDashed
' Set text alignment
Private workSheet("B2").Style.HorizontalAlignment = HorizontalAlignment.Center
workBook.SaveAs("setBorderAndAlignment.xlsx")
[37] ## What Advanced Border and Alignment Options Are Available?
[38] ### How Can I Customize Border Colors?
Varsayılan olarak kenarlık rengi siyahtır, ancak bunu Color sınıfında bulunan herhangi bir renge göre özelleştirebilir veya Hex renk kodu kullanabilirsiniz. Kenarlık rengini ayarlamak için, [Color] özelliğini istediğiniz renk veya Hex kodu ile kullanın. Ayrıca, [Color] özelliği, kenarlığın rengini almanızı sağlar. [42] This flexibility is similar to how you can set background colors for cells.
[43]
[44] The border color customization works seamlessly with other cell formatting features. [45] When combined with conditional formatting, you can create dynamic visual cues that help users quickly identify important data patterns in your spreadsheets.
:path=/static-assets/excel/content-code-examples/how-to/border-alignment-set-border-color.cs
using IronXL;
using IronXL.Styles;
using IronSoftware.Drawing;
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet["B2"].Style.LeftBorder.Type = BorderType.Thick;
workSheet["B2"].Style.RightBorder.Type = BorderType.Thick;
// Set cell border color
workSheet["B2"].Style.LeftBorder.SetColor(Color.Aquamarine);
workSheet["B2"].Style.RightBorder.SetColor("#FF7F50");
workBook.SaveAs("setBorderColor.xlsx");
Imports IronXL
Imports IronXL.Styles
Imports IronSoftware.Drawing
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private workSheet("B2").Style.LeftBorder.Type = BorderType.Thick
Private workSheet("B2").Style.RightBorder.Type = BorderType.Thick
' Set cell border color
workSheet("B2").Style.LeftBorder.SetColor(Color.Aquamarine)
workSheet("B2").Style.RightBorder.SetColor("#FF7F50")
workBook.SaveAs("setBorderColor.xlsx")
[47] ### Which Border Line Positions and Patterns Can I Apply?
[48] In total, there are six border line positions, each offering a variety of patterns or types. These positions include top, right, bottom, left, as well as diagonal lines moving forward, backward, and both. [49] This comprehensive set of options allows you to create complex visual layouts similar to those you might achieve when working with merged cells, but with more flexibility.
[50] When working with diagonal borders, understand how they interact with the cell's content and alignment settings. [51] Diagonal borders can be particularly useful for creating header sections or visual separators in your Excel reports.
:path=/static-assets/excel/content-code-examples/how-to/border-alignment-set-border-line.cs
using IronXL;
using IronXL.Styles;
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;
workSheet["B2"].StringValue = "Top";
workSheet["B4"].StringValue = "Forward";
// Set top border line
workSheet["B2"].Style.TopBorder.Type = BorderType.Thick;
// Set diagonal border line
workSheet["B4"].Style.DiagonalBorder.Type = BorderType.Thick;
// Set diagonal border direction
workSheet["B4"].Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward;
workBook.SaveAs("borderLines.xlsx");
Imports IronXL
Imports IronXL.Styles
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private workSheet("B2").StringValue = "Top"
Private workSheet("B4").StringValue = "Forward"
' Set top border line
Private workSheet("B2").Style.TopBorder.Type = BorderType.Thick
' Set diagonal border line
Private workSheet("B4").Style.DiagonalBorder.Type = BorderType.Thick
' Set diagonal border direction
Private workSheet("B4").Style.DiagonalBorderDirection = DiagonalBorderDirection.Forward
workBook.SaveAs("borderLines.xlsx")
[52] For more complex border scenarios, consider creating a reusable function that applies a consistent border style across multiple cells or ranges:
void ApplyUniformBorder(WorkSheet sheet, string range, BorderType borderType, Color borderColor)
{
var cells = sheet[range];
// Apply borders to all sides
cells.Style.TopBorder.Type = borderType;
cells.Style.RightBorder.Type = borderType;
cells.Style.BottomBorder.Type = borderType;
cells.Style.LeftBorder.Type = borderType;
// Apply color to all borders
cells.Style.TopBorder.SetColor(borderColor);
cells.Style.RightBorder.SetColor(borderColor);
cells.Style.BottomBorder.SetColor(borderColor);
cells.Style.LeftBorder.SetColor(borderColor);
}
// Usage example
ApplyUniformBorder(workSheet, "A1:D4", BorderType.Thin, Color.Black);
void ApplyUniformBorder(WorkSheet sheet, string range, BorderType borderType, Color borderColor)
{
var cells = sheet[range];
// Apply borders to all sides
cells.Style.TopBorder.Type = borderType;
cells.Style.RightBorder.Type = borderType;
cells.Style.BottomBorder.Type = borderType;
cells.Style.LeftBorder.Type = borderType;
// Apply color to all borders
cells.Style.TopBorder.SetColor(borderColor);
cells.Style.RightBorder.SetColor(borderColor);
cells.Style.BottomBorder.SetColor(borderColor);
cells.Style.LeftBorder.SetColor(borderColor);
}
// Usage example
ApplyUniformBorder(workSheet, "A1:D4", BorderType.Thin, Color.Black);
Option Strict On
Sub ApplyUniformBorder(sheet As WorkSheet, range As String, borderType As BorderType, borderColor As Color)
Dim cells = sheet(range)
' Apply borders to all sides
cells.Style.TopBorder.Type = borderType
cells.Style.RightBorder.Type = borderType
cells.Style.BottomBorder.Type = borderType
cells.Style.LeftBorder.Type = borderType
' Apply color to all borders
cells.Style.TopBorder.SetColor(borderColor)
cells.Style.RightBorder.SetColor(borderColor)
cells.Style.BottomBorder.SetColor(borderColor)
cells.Style.LeftBorder.SetColor(borderColor)
End Sub
' Usage example
ApplyUniformBorder(workSheet, "A1:D4", BorderType.Thin, Color.Black)
[53] #### Border Lines
[55] #### Border Patterns
[57] ### What Text Alignment Options Are Supported?
[58] Text alignment in IronXL provides comprehensive control over how content is positioned within cells. [59] This feature is particularly important when exporting data to different formats where maintaining visual consistency is crucial. [60] The alignment system works in conjunction with other formatting features like font styling to create professional-looking spreadsheets.
Color Color
[63] #### HorizontalAlignment Enumeration
General: Genel hizalı yatay hizalama. [65] Text data is left-aligned. [66] Numbers, dates, and times are right-aligned. [67] Boolean types are centered. [68] Changing the alignment does not affect the data type. [69] This is the default alignment when no specific alignment is set.Left: Sağdan sola modunda bile sola hizalı yatay hizalama. [71] Aligns contents at the left edge of the cell. [72] If an indent amount is specified, the cell's contents are indented from the left by the specified number of character spaces.Center: Ortalanmış yatay hizalama. [74] The text is centered across the cell. [75] This is commonly used for headers and titles.Right: Sağa hizalı yatay hizalama. [77] Cell contents are aligned at the right edge of the cell, even in Right-to-Left mode. [78] This is typically used for numeric data presentation.Fill: Hücre değeri tüm genişliği kaplar. [80] If adjacent cells to the right also have the same fill alignment, they will be filled as well. [81] Additional rules: [82] - Only whole values can be appended, not partial values. [83] - The column will not be widened to 'best fit' the filled value. [84] - If appending an additional occurrence of the value exceeds the boundary of the cell's left/right edge, it will not be added. [85] - The display value of the cell is filled, not the underlying raw number.Justify: Her iki kenara hizalanmış (sol ve sağa hizalanmış) yatay hizalama. [87] Applies wrap text to the cell and ensures that each line aligns the first word with the left edge and the last word with the right edge of the cell (except for the last line). [88] This is particularly useful for text-heavy cells containing paragraphs.CenterSelection: En soldaki hücrenin içeriğini birden fazla hücre boyunca ortaya yatay olarak hizalar. [90] It visually appears similar to merging cells, but without actually merging them. [91] Using this option helps prevent potential issues that may arise from merged cells.Distributed: Hücre içindeki her satırdaki her 'WORD', hücrenin genişliği boyunca eşit olarak dağıtılır ve sağ ve sol kenar boşlukları hizalanır. [93] If there is an indent value to apply, both the left and right sides of the cell are padded by the indent value.
[94] #### VerticalAlignment Enumeration
None: Varsayılan hizalama, genellikle alt hizalama davranışıyla sonuçlanır.Top: İçeriği hücrenin üst kısmına hizalar. [97] Useful for cells with varying content heights.Center: Hücre içindeki içeriği dikey olarak ortalar. [99] This creates a balanced appearance, especially when combined with horizontal centering.Bottom: İçeriği hücrenin altına hizalar. [101] This is the standard behavior for most spreadsheet applications.Justify: Metin satırlarını hücrenin yüksekliği boyunca eşit olarak dağıtır; üst ve alt kenar boşlukları hizalanır. [103] Works similarly to horizontal justification by wrapping text and adjusting the spaces between lines to occupy the entire row's height.Distributed: Her 'WORD'u, satırdaki her WORD'u hücrenin yüksekliği boyunca eşit olarak dağıtır; yatay metin yönünde üst ve alt kenar boşlukları hizalanır. [105] In vertical text direction, it behaves exactly as distributed horizontal alignment, evenly distributing the lines of text from top to bottom.
[106] When working with alignment settings, consider how they interact with other cell properties. [107] For example, when you autosize rows and columns, the alignment settings will affect how the content fits within the automatically sized cells. [108] Similarly, when working with formulas, proper alignment can make your spreadsheet calculations more readable and professional.
Sıkça Sorulan Sorular
C#'ta Microsoft Office olmadan Excel hücrelerine nasıl kenarlık eklerim?
Excel hücrelerine IronXL'nin Stil API'si kullanarak kenarlık ekleyebilirsiniz. Sadece hücrenin Stil özelliğine erişin ve MediumDashed gibi sol, sağ, üst ve alt kenarları için tür özelliklerini ayarlayın. IronXL bağımsız olarak çalışır, Microsoft Office veya Interop bağımlılıklarına gerek duymaz.
C#'ta Excel hücreleri için hangi kenarlık stilleri mevcuttur?
IronXL, IronXl.Styles.BorderType enum'u aracılığıyla MediumDashed, İnce, Kalın, Çift ve daha fazlası gibi seçenekler dahil çeşitli kenarlık stilleri sağlar. Bu stilleri hücrenin her iki yanına (üst, sağ, alt, sol) profesyonel görünüm sağlamak için uygulayabilirsiniz.
Excel hücrelerinde metni programlı olarak nasıl merkezlerim?
IronXL kullanarak Excel hücrelerindeki metni ortalamak için, Style.HorizontalAlignment özelliğini IronXl.Styles.HorizontalAlignment.Center olarak ayarlayın. Metni tam olarak konumlandırmak için VerticalAlignment özelliğini de kullanarak dikey hizalamayı kontrol edebilirsiniz.
Tek seferde birden çok hücreye kenarlık ve hizalama uygulayabilir miyim?
Evet, IronXL, tüm aralıklara, sütunlara veya satırlara kenarlık ve hizalama uygulamanıza olanak sağlar. İstediğiniz aralığı seçin ve birden fazla hücreyi aynı anda formatlamak için Stil özelliklerini uygulayın, toplu biçimlendirme işlemlerini verimli hale getirir.
C#'ta Excel hücreleri için kenarlık renkleri nasıl ayarlanır?
IronXL, önceden tanımlanmış renk türlerini veya özel Hex renk kodlarını kullanarak kenar renklerini ayarlamanıza imkan tanır. Hücre kenarlarının görünümünü özelleştirmek için Stil nesnesi aracılığıyla kenar renk özelliğine erişin.
Kenarlık eklemek ve metni merkezlendirmek için gereken en minimal kod nedir?
IronXL ile kenarlık ekleyip metni sadece iki satırda merkezleyebilirsiniz: workSheet["B2"].Style.LeftBorder.Type = IronXl.Styles.BorderType.MediumDashed; ve workSheet["B2"].Style.HorizontalAlignment = IronXl.Styles.HorizontalAlignment.Center; Bu, Interop yükü olmadan hızlı biçimlendirme sağlar.

