用C++自动调整行列宽

自动调整

Aspose.Cells提供了表示Microsoft Excel文件的Workbook类。Workbook类包含一个Worksheets集合,可以访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了管理工作表的多种方法。本文介绍了使用Worksheet类自动调整行或列的方法。

自动调整行 - 简单

最简单的自动调整行宽和列高的方法是调用Worksheet类的AutoFitRow方法。AutoFitRow方法以行索引作为参数,调整特定行。

#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;
}

如何在单元格范围内自动调整行

一行由多个列组成。Aspose.Cells允许开发者通过调用AutoFitRow方法的重载版本,根据该行中某个范围的单元格内容自动调整行高。参数如下:

  • 行索引,即要自动调整的行的索引。
  • 第一个列索引,即行的第一个列的索引。
  • 最后列索引,指行的最后一列的索引。

AutoFitRow方法会检查该行所有列的内容,然后自动调整行高。

#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();
}

如何在一系列单元格中自动调整列

一列由多个行组成。可以通过调用AutoFitColumn方法的重载版本,根据列中某个范围的单元格内容自动调整列宽。参数如下:

  • 列索引,要自动调整的列的索引。
  • 第一行索引,列的第一行的索引。
  • 最后行索引,列的最后一行的索引。

AutoFitColumn方法会检查该列所有行的内容,然后自动调整列宽。

#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();
}

如何为合并单元格自动调整行高

使用Aspose.Cells,即使对合并的单元格,也可以自动调整行高。AutoFitterOptions类提供了GetAutoFitMergedCellsType()属性,可用于自动调整合并单元格的行高。GetAutoFitMergedCellsType()接受包含以下成员的AutoFitMergedCellsType枚举:

  • None:忽略合并的单元格。
  • FirstLine:只扩展第一行的高度。
  • 最后一行:只扩展最后一行的高度。
  • 每行:只扩展每一行的高度。
#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();
}

重要知识

高级主题