Automatisk anpassning av rader och kolumner

Autostorlek

Aspose.Cells tillhandahåller en Workbook-klass som representerar en Microsoft Excel-fil. Workbook-klassen innehåller en Worksheets-kollektion som möjliggör åtkomst till varje kalkylblad i en Excel-fil. Ett kalkylblad representeras av Worksheet-klassen. Worksheet-klassen tillhandahåller ett brett utbud av egenskaper och metoder för hantering av ett kalkylblad. Den här artikeln tittar på användningen av Worksheet-klassen för att automatiskt anpassa rader eller kolumner.

AutoFit Row - Enkelt

Det mest raka tillvägagångssättet för att automatiskt justera bredd och höjd för en rad är att anropa klassen Worksheet metoden AutoFitRow. Metoden AutoFitRow tar en radindex (av raden som ska ändras) som 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();

Hur man Autofitrad i ett område av celler

En rad består av många kolumner. Aspose.Cells tillåter utvecklare att automatiskt justera en rad baserad på innehållet i en rad av celler genom att anropa en överlagrad version av metoden AutoFitRow. Den tar följande parametrar:

  • Radindex, index för raden som ska autofit.
  • Första kolumnindex, index för radens första kolumn.
  • Sista kolumnindex, index för radens sista kolumn.

Metoden AutoFitRow 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();

Hur man Autofittar kolumn i ett område av celler

En kolumn består av många rader. Det är möjligt att automatiskt justera en kolumn baserad på innehållet i ett område av celler i kolumnen genom att anropa en överlagrad version av metoden AutoFitColumn som tar följande parametrar:

  • Kolumnindex, index för kolumnen som ska autofit.
  • Första radindex, index för kolumnens första rad.
  • Sista radindex, index för kolumnens sista rad.

Metoden AutoFitColumn 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();

Hur man Autofittar rader för sammanfogade celler

Med Aspose.Cells är det möjligt att automatisera rader även för celler som har sammanfogats med hjälp av AutoFitterOptions API. AutoFitterOptions klass tillhandahåller AutoFitMergedCellsType egenskap som kan användas för att automatisera rader för sammanfogade celler. AutoFitMergedCellsType accepterar AutoFitMergedCellsType uppräkneligt som har följande medlemmar.

  • Ingen: Ignorera sammanfogade celler.
  • FörstaLinje: Endast expanderar höjden på första raden.
  • SistaLinje: Endast expanderar höjden på sista raden.
  • VarjeLinje: Endast expanderar 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");

Viktigt att veta

Fortsatta ämnen