通过 Node.js 使用 C++ 自动调整行和列的宽度

自动调整

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

自动调整行 - 简单

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

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");

// Reading the Excel file into a buffer
const fs = require("fs");
const fileBuffer = fs.readFileSync(inputPath);

// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileBuffer);

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1);

// Saving the modified Excel file
const outputPath = path.join(dataDir, "output.xlsx");
workbook.save(outputPath);

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

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

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

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

const AsposeCells = require("aspose.cells.node");
const path = require("path");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");

// Reading the Excel file into a buffer
const fs = require("fs");
const fileData = fs.readFileSync(inputPath);

// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileData);

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

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

// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));

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

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

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

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

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");

// Creating a file stream containing the Excel file to be opened
const fs = require("fs");
const workbook = new AsposeCells.Workbook(fs.readFileSync(inputPath));

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

// Auto-fitting the Column of the worksheet
worksheet.autoFitColumn(4);

// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));

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

使用 Aspose.Cells,即使单元格已合并,也可以自动调整行的宽度,使用 AutoFitterOptions API。AutoFitterOptions 类提供 AutoFitterOptions.getAutoFitMergedCellsType() 属性,可用于自动调整合并单元格的行宽。AutoFitterOptions.getAutoFitMergedCellsType() 接受 AutoFitMergedCellsType 枚举,其成员如下。

  • None:忽略合并的单元格。
  • FirstLine:只扩展第一行的高度。
  • 最后一行:只扩展最后一行的高度。
  • 每行:只扩展每一行的高度。
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const outputDir = path.join(dataDir, "output");

// Instantiate a new Workbook
const wb = new AsposeCells.Workbook();

// Get the first (default) worksheet
const worksheet = wb.getWorksheets().get(0);

// Create a range A1:B1
const 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("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");

// Create a style object
const 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
const options = new AsposeCells.AutoFitterOptions();

// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AsposeCells.AutoFitMergedCellsType.EachLine);

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

// Save the Excel file
wb.save(path.join(outputDir, "AutofitRowsforMergedCells.xlsx"));

重要知识

高级主题