Formatera rader och kolumner
Arbeta med rader
Hur man justerar radhöjden
Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Klassen Workbook innehåller en WorksheetCollection som ger åtkomst till varje kalkylblad i Excel-filen. Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller en Cells-samling som representerar alla celler i kalkylbladet.
Cells samlingen tillhandahåller flera metoder för att hantera rader eller kolumner i ett kalkylblad. Några av dessa diskuteras nedan mer detaljerat.
Hur man ställer in höjden på en rad
Det är möjligt att ställa in höjden på en enskild rad genom att anropa SetRowHeight metoden på Cells samlingen. Metoden SetRowHeight tar följande parametrar som följer:
- Radindex, index för den rad vars höjd du ändrar.
- Radhöjd, radhöjden som ska tillämpas på raden.
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting the height of the second row to 13 | |
worksheet.Cells.SetRowHeight(1, 13); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Hur man ställer in höjden på alla rader i ett kalkylblad
Om utvecklare behöver ange samma radhöjd för alla rader i kalkylbladet kan de göra det genom att använda Cells egenskapen för StandardHeight samlingen.
Exempel:
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting the height of all rows in the worksheet to 15 | |
worksheet.Cells.StandardHeight = 15; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Arbeta med kolumner
Hur man ställer in bredden på en kolumn
Ställ in bredden på en kolumn genom att anropa SetColumnWidth metoden på Cells samlingen. Metoden SetColumnWidth tar följande parametrar:
- Kolumnindex, index för den kolumn vars bredd du ändrar.
- Kolumnbredd, önskad kolumnbredd.
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting the width of the second column to 17.5 | |
worksheet.Cells.SetColumnWidth(1, 17.5); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Hur man ställer in kolumnbredd i pixlar
Ange kolumnbredden genom att ringa in samlingens Cells metoden SetColumnWidthPixel. Metoden SetColumnWidthPixel tar följande parametrar:
- Kolumnindex, index för den kolumn vars bredd du ändrar.
- Kolumnbredd, önskad kolumnbredd i pixlar.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
string outDir = RunExamples.Get_OutputDirectory(); | |
//Load source Excel file | |
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx"); | |
//Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Set the width of the column in pixels | |
worksheet.Cells.SetColumnWidthPixel(7, 200); | |
workbook.Save(outDir + "SetColumnWidthInPixels_Out.xlsx"); |
Hur man ställer in bredden på alla kolumner i en arbetsbok
För att ställa in samma kolumnbredd för alla kolumner i arbetsboken, använd samlingens Cells egenskap StandardWidth.
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting the width of all columns in the worksheet to 20.5 | |
worksheet.Cells.StandardWidth = 20.5; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
Fortsatta ämnen
- AutoAnpassa rader och kolumner
- Konvertera Text till Kolumner med Aspose.Cells
- Kopiera rader och kolumner
- Ta bort tomma rader och kolumner i en arbetsbok
- Gruppering och avgruppering av rader och kolumner
- Dölja och visa rader och kolumner
- Infoga eller ta bort rader i en Excel-arbetsbok
- Infoga och ta bort rader och kolumner i Excel-fil
- Ta bort dubblettrader i ett kalkylblad
- Uppdatera referenser i andra arbetsblad samtidigt som tomma kolumner och rader tas bort i ett arbetsblad