Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.Cells provides a Workbook class that represents a Microsoft Excel file. The Workbook class contains a Workbook.getWorksheets() collection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods for managing a worksheet. This article looks at using the Worksheet class to autofit rows or columns.
The most straightforward approach to auto-sizing the width and height of a row is to call the Worksheet class autoFitRow method. The autoFitRow method takes a row index (of the row to be resized) as a parameter.
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);
A row is composed of many columns. Aspose.Cells allows developers to auto-fit a row based on the content in a range of cells within the row by calling an overloaded version of the autoFitRow method. It takes the following parameters:
The autoFitRow method checks the contents of all the columns in the row and then auto‑fits the row.
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"));
A column is composed of many rows. It is possible to auto‑fit a column based on the content in a range of cells in the column by calling an overloaded version of the autoFitColumn method that takes the following parameters:
The autoFitColumn method checks the contents of all rows in the column and then auto‑fits the column.
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"));
With Aspose.Cells, it is possible to autofit rows even for cells that have been merged using the AutoFitterOptions API. The AutoFitterOptions class provides the getAutoFitMergedCellsType() method that can be used to autofit rows for merged cells. getAutoFitMergedCellsType() accepts the AutoFitMergedCellsType enumeration, which has the following members.
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 a value into 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"));
You may also try to use the overloaded versions of autoFitRows & autoFitColumns methods accepting a range of rows/columns and an instance of AutoFitterOptions to auto‑fit the selected rows/columns with your desired AutoFitterOptions accordingly.
The signatures of the aforesaid methods are as follows:
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.