Autopassa rader och kolumner
Autopassning
Aspose.Cells tillhandahåller enArbetsbokklass som representerar en Microsoft Excel-fil. DeArbetsbokklass innehåller enArbetsbladsamling som ger åtkomst till varje kalkylblad i en Excel-fil. Ett arbetsblad representeras avArbetsblad klass. DeArbetsblad klass tillhandahåller ett brett utbud av egenskaper och metoder för att hantera ett kalkylblad. Den här artikeln handlar om att användaArbetsbladklass för att automatiskt anpassa rader eller kolumner.
AutoFit Row - Enkel
Det enklaste sättet att automatiskt anpassa bredden och höjden på en rad är att anropaArbetsblad klassAutoFitRow metod. DeAutoFitRowmetoden tar ett radindex (för raden som ska storleksändras) som en parameter.
// 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); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// 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]; | |
// Auto-fitting the 3rd row of the worksheet | |
worksheet.AutoFitRow(1); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
AutoFit-rad i intervallet Cells
En rad består av många kolumner. Aspose.Cells tillåter utvecklare att automatiskt anpassa en rad baserat på innehållet i ett antal celler inom raden genom att anropa en överbelastad version avAutoFitRowmetod. Den kräver följande parametrar:
- Radindex, indexet för raden som ska anpassas automatiskt.
- Första kolumnindex, indexet för radens första kolumn.
- Sista kolumnindex, indexet för radens sista kolumn.
DeAutoFitRowmetod kontrollerar innehållet i alla kolumner i raden och anpassar sedan raden automatiskt.
// 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); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// 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]; | |
// Auto-fitting the 3rd row of the worksheet | |
worksheet.AutoFitRow(1, 0, 5); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
AutoFit-kolumn i intervallet Cells
En kolumn består av många rader. Det är möjligt att automatiskt anpassa en kolumn baserat på innehållet i ett antal celler i kolumnen genom att anropa en överbelastad version avAutoFitColumnmetod som tar följande parametrar:
- Kolumnindexindexet för kolumnen som ska anpassas automatiskt.
- Första radens index, indexet för kolumnens första rad.
- Sista radens index, indexet för kolumnens sista rad.
DeAutoFitColumnmetod kontrollerar innehållet i alla rader i kolumnen och anpassar sedan kolumnen automatiskt.
// 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); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// 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]; | |
// Auto-fitting the Column of the worksheet | |
worksheet.AutoFitColumn(4, 4, 6); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
AutoFit-rader för sammanslagna Cells
Med Aspose.Cells är det möjligt att autopassa rader även för celler som har slagits samman med hjälp avAutoFitterOptions API. AutoFitterOptionsklass gerAutoFitMergedCellsType egenskap som kan användas för att automatiskt anpassa rader för sammanslagna celler.AutoFitMergedCellsTypeaccepterarAutoFitMergedCellsType enumerable som har följande medlemmar.
- Ingen: Ignorera sammanslagna celler.
- FirstLine: Expanderar endast höjden på den första raden.
- LastLine: Expanderar endast höjden på den sista raden.
- Varje rad: utökar endast höjden på varje rad.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiate a new Workbook | |
Workbook wb = new Workbook(); | |
// Get the first (default) worksheet | |
Worksheet _worksheet = wb.Worksheets[0]; | |
// Create a range A1:B1 | |
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2); | |
// Merge the cells | |
range.Merge(); | |
// Insert value to the merged cell A1 | |
_worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end"; | |
// Create a style object | |
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle(); | |
// Set wrapping text on | |
style.IsTextWrapped = true; | |
// Apply the style to the cell | |
_worksheet.Cells[0, 0].SetStyle(style); | |
// Create an object for AutoFitterOptions | |
AutoFitterOptions options = new AutoFitterOptions(); | |
// Set auto-fit for merged cells | |
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine; | |
// Autofit rows in the sheet(including the merged cells) | |
_worksheet.AutoFitRows(options); | |
// Save the Excel file | |
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx"); |
Du kan också försöka använda de överbelastade versionerna avAutoFitRows & AutoFitColumns metoder som accepterar ett intervall av rader/kolumner och en instans avAutoFitterOptions för att automatiskt anpassa de valda raderna/kolumnerna med dina önskadeAutoFitterOptionsföljaktligen.
Signaturerna för ovannämnda metoder är följande:
- AutoFitRows(int startRow, int endRow,AutoFitterOptionsalternativ)
- AutoFitColumns(int firstColumn, int lastColumn,AutoFitterOptionsalternativ)