Tables and Ranges with Node.js via C++
Introduction
Sometimes you create a table in Microsoft Excel and do not want to keep working with the table functionality that it comes with. Instead, you want something that looks like a table. To keep data in a table without losing formatting, convert the table to a regular range of data.
Aspose.Cells does support this feature of Microsoft Excel for tables and list objects.
Using Microsoft Excel
Use the Convert to Range feature to quickly convert a table to a range without losing formatting. In Microsoft Excel 2007/2010:
- Click anywhere in the table to make sure that the active cell is in a table column.
- On the Design tab, in the Tools group, click Convert to Range.
Using Aspose.Cells
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "table_ranges.xlsx");
// Open an existing file that contains a table/list object in it
const wb = new AsposeCells.Workbook(filePath);
// Convert the first table/list object (from the first worksheet) to normal range
wb.getWorksheets().get(0).getListObjects().get(0).convertToRange();
// Save the file
wb.save(path.join(dataDir, "output.xlsx"));
Convert Table to Range with Options
Aspose.Cells provides additional options while converting Table to Range through the TableToRangeOptions class. The TableToRangeOptions class provides getLastRow() property which allows you to set the last index of the table row. The table formatting will be retained up to the specified row index and the rest of the formatting will be removed.
The sample code given below demonstrates the use of TableToRangeOptions class.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "table_ranges.xlsx");
// Open an existing file that contains a table/list object in it
const workbook = new AsposeCells.Workbook(filePath);
const options = new AsposeCells.TableToRangeOptions();
options.setLastRow(5);
// Convert the first table/list object (from the first worksheet) to normal range
workbook.getWorksheets().get(0).getListObjects().get(0).convertToRange(options);
// Save the file
workbook.save(path.join(dataDir, "output.xlsx"));