Auto Anpassa rader och kolumner med C++

Autostorlek

Aspose.Cells tillhandahåller en Workbook klass som representerar en Microsoft Excel-fil. Workbook klassen innehåller en Worksheets samling som ger tillgång till varje kalkblad i en Excel-fil. Ett kalkblad representeras av Worksheet klassen. Worksheet klassen erbjuder ett brett utbud av metoder för att hantera ett kalkblad. Den här artikeln tittar på att använda Worksheet klassen för att autofitta rader eller kolumner.

AutoFit Row - Enkelt

Det enklaste sättet att auto-anpassa bredd och höjd på en rad är att anropa Worksheet-klassens AutoFitRow-metod. AutoFitRow-metoden tar ett radindex (på raden som ska ändras storlek på) som parameter.

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"Book1.xlsx";

    // Create workbook from file
    Workbook workbook(inputFilePath);

    // Access the first worksheet in the Excel file
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Auto-fit the 2nd row (index 1) of the worksheet
    worksheet.AutoFitRow(1);

    // Save the modified Excel file
    U16String outputFilePath = srcDir + u"output.xlsx";
    workbook.Save(outputFilePath);

    std::cout << "Row auto-fitted and file saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
    return 0;
}

Hur man Autofitrad i ett område av celler

En rad består av många kolumner. Aspose.Cells tillåter utvecklare att autofit-a en rad baserat på innehållet i ett cellområde inom raden genom att anropa en överlagrad version av AutoFitRow-metoden. 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.

AutoFitRow-metoden kontrollerar innehållet i alla kolumner i raden och autofitar sedan raden.

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"Book1.xlsx";

    // Path of output Excel file
    U16String outputFilePath = outDir + u"output.xlsx";

    // Open the Excel file
    Workbook workbook(inputFilePath);

    // Accessing the first worksheet in the Excel file
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Auto-fitting the 3rd row of the worksheet
    worksheet.AutoFitRow(1, 0, 5);

    // Save the modified Excel file
    workbook.Save(outputFilePath);

    std::cout << "Row auto-fitted and file saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Hur man Autofittar kolumn i ett område av celler

En kolumn består av många rader. Det är möjligt att automatiskt anpassa en kolumn baserat på innehållet i ett område av celler i kolumnen genom att anropa en överbelastad version av AutoFitColumn metoden 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.

AutoFitColumn-metoden kontrollerar innehållet i alla rader i kolumnen och autofitar sedan kolumnen.

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"Book1.xlsx";

    // Create workbook from the input file
    Workbook workbook(inputFilePath);

    // Access the first worksheet in the Excel file
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Auto-fit the 5th column (index 4) from row 4 to 6
    worksheet.AutoFitColumn(4, 4, 6);

    // Save the modified Excel file
    U16String outputFilePath = srcDir + u"output.xlsx";
    workbook.Save(outputFilePath);

    std::cout << "Column auto-fitted and file saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Hur man Autofittar rader för sammanfogade celler

Med Aspose.Cells är det möjligt att autofit-rader även för celler som har blivit sammanslagna med hjälp av AutoFitterOptions API. AutoFitterOptions klassen tillhandahåller egenskapen GetAutoFitMergedCellsType() som kan användas för att autofitta rader för sammanslagna celler. GetAutoFitMergedCellsType() accepterar AutoFitMergedCellsType enumeration, som har följande medlemmar:

  • Ingen: Ignorera sammanslagna celler.
  • FörstaLinjen: Utökar endast höjden på den första raden.
  • SistaLinjen: Utökar endast höjden på den sista raden.
  • VarjeRad: Utökar endast höjden på varje rad.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Output directory
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Instantiate a new Workbook
    Workbook wb;

    // Get the first (default) worksheet
    Worksheet worksheet = wb.GetWorksheets().Get(0);

    // Create a range A1:B1
    Range range = worksheet.GetCells().CreateRange(0, 0, 1, 2);

    // Merge the cells
    range.Merge();

    // Insert value to the merged cell A1
    worksheet.GetCells().Get(0, 0).SetValue(u"A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");

    // Create a style object
    Style style = worksheet.GetCells().Get(0, 0).GetStyle();

    // Set wrapping text on
    style.SetIsTextWrapped(true);

    // Apply the style to the cell
    worksheet.GetCells().Get(0, 0).SetStyle(style);

    // Create an object for AutoFitterOptions
    AutoFitterOptions options;

    // Set auto-fit for merged cells
    options.SetAutoFitMergedCellsType(AutoFitMergedCellsType::EachLine);

    // Autofit rows in the sheet (including the merged cells)
    worksheet.AutoFitRows(options);

    // Save the Excel file
    wb.Save(outDir + u"AutofitRowsforMergedCells.xlsx");

    std::cout << "Autofit rows for merged cells completed successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Viktigt att veta

Avancerade ämnen