Kantinställningar
Lägga till ramar till celler
Microsoft Excel tillåter användare att formatera celler genom att lägga till kanter. Kanttypen beror på var den läggs till. Till exempel är en övre kant en som läggs till på cellens övre position. Användare kan också modifiera kantens linjestil och färg.
Med Aspose.Cells kan utvecklare lägga till kanter och anpassa hur de ser ut på samma flexibla sätt som i Microsoft Excel.
Lägga till ramar till celler
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 arbetsblad i Excel-filen. Ett arbetsblad representeras av Worksheet klassen. Worksheet klassen ger en Cells samling. Varje objekt i Cells samlingen representerar ett objekt av Cell klassen.
Aspose.Cells tillhandahåller GetStyle metoden i Cell klassen. SetStyle metoden används för att ställa in en cells formateringsstil. Style klassen tillhandahåller egenskaper för att lägga till kanter till celler.
Lägga till ramar till en cell
Utvecklare kan lägga till kanter till en cell genom att använda Style objektets Borders samling. Kantttypen skickas som en index till Borders samlingen. Alla kanttyper är fördefinierade i BorderType uppräkning.
Kantuppräkning
Ramtyper | Beskrivning |
---|---|
BottomBorder | En nederkantslinje |
DiagonalDown | En diagonal linje från övre vänster till höger nedan |
DiagonalUp | En diagonal linje från nedre vänster till höger upp |
LeftBorder | En vänsterkantlinje |
RightBorder | En högerkantlinje |
TopBorder | En övre kantlinje |
The Borders collection stores all borders. Each border in the Borders collection is represented by a Border object which provides two properties, Color and LineStyle to set a border’s line color and style respectively.
För att ange en kantlinjens färg, välj en färg med hjälp av Color-enumen (en del av .NET Framework) och tilldela den till Border-objektets Color-egenskap.
Kantlinjens linjestil ställs in genom att välja en linjestil från CellBorderType-uppräkningen.
CellBorderType-enumen
Linjestilar | Beskrivning |
---|---|
DashDot | Tunn streckpunktad linje |
DashDotDot | Tunn streck-punktpunktad linje |
Dashed | Streckad linje |
Dotted | Punkterad linje |
Double | Dubbel linje |
Hair | Hårlinje |
MediumDashDot | Medium streckpunktad linje |
MediumDashDotDot | Medium streck-punktpunktad linje |
MediumDashed | Medium streckad linje |
None | Ingen linje |
Medium | Medium linje |
SlantedDashDot | Snedstreckad mediumstreckpunktad linje |
Thick | Tjock linje |
Thin | Tunn linje |
Välj en av linjestilarna och tilldela den sedan till Border-objektets LineStyle-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 first (default) worksheet by passing its sheet index | |
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!"); | |
// Create a style object | |
Style style = cell.GetStyle(); | |
// Setting the line style of the top border | |
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the top border | |
style.Borders[BorderType.TopBorder].Color = Color.Black; | |
// Setting the line style of the bottom border | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the bottom border | |
style.Borders[BorderType.BottomBorder].Color = Color.Black; | |
// Setting the line style of the left border | |
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the left border | |
style.Borders[BorderType.LeftBorder].Color = Color.Black; | |
// Setting the line style of the right border | |
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the right border | |
style.Borders[BorderType.RightBorder].Color = Color.Black; | |
// Apply the border styles to the cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Lägga till Gränser till en Rad av Celler
Det är också möjligt att lägga till ramar till en rad celler istället för bara en enskild cell. För att göra det, skapa först en samling av celler genom att anropa Cells-objektets CreateRange-metod. Den tar följande parametrar:
- Första rad, den första raden av området.
- Första kolumn, representerar den första kolumnen av området.
- Antal rader, antalet rader i området.
- Antal kolumner, antalet kolumner i området.
CreateRange-metoden returnerar ett Range-objekt, som innehåller det angivna cellområdet. Range-objektet tillhandahåller en SetOutlineBorder-metod som tar följande parametrar för att lägga till en ram till cellområdet:
- Border Typ, typen av kant, vald från BorderType uppräkningen.
- Linjestil, kantens linjestil, vald från CellBorderType uppräkningen.
- Färg, linjens färg, vald från Färg uppräkningen.
// 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("Hello World From Aspose"); | |
// Creating a range of cells starting from "A1" cell to 3rd column in a row | |
Range range = worksheet.Cells.CreateRange(0, 0, 1, 3); | |
// Adding a thick top border with blue line | |
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick bottom border with blue line | |
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick left border with blue line | |
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick right border with blue line | |
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |