Formatera rader och kolumner med Node.js via C++

Arbeta med rader

Hur man justerar radhöjden

Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Workbook-klassen innehåller en WorksheetCollection som tillåter åtkomst till varje arbetsblad i Excel-filen. Ett arbetsblad representeras av Worksheet-klassen. Worksheet-klassen tillhandahåller en Cells-samling som representerar alla celler i arbetsbladet.

Samlingen Cells erbjuder flera metoder för att hantera rader eller kolumner i ett arbetsblad. Några av dessa diskuteras nedan i mer detalj.

Hur man ställer in höjden på en rad

Det är möjligt att ange höjden för en enskild rad genom att anropa {}-samlingens {}-metod. {}-metoden tar följande parametrar:

  • Radindex, index för den rad vars höjd du ändrar.
  • Radhöjd, radhöjden som ska tillämpas på raden.
try {
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.createReadStream(filePath);

// Reading the file stream into a buffer
const chunks = [];
fstream.on('data', chunk => chunks.push(chunk));
fstream.on('end', () => {
const buffer = Buffer.concat(chunks);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(buffer);

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

// Setting the height of the second row to 13
worksheet.getCells().setRowHeight(1, 13);

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

// Closing the file stream to free all resources
fstream.close();

Hur man ställer in höjden på alla rader i ett kalkylblad

Om utvecklare behöver sätta samma radhöjd för alla rader i arbetsbladet kan de göra det genom att använda getStandardHeight()-egenskapen hos Cells-samlingen.

Exempel:

const fs = require("fs");
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, "book1.xls");

// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);

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

// Setting the height of all rows in the worksheet to 15
worksheet.getCells().setStandardHeight(15);

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

// Closing the file stream to free all resources
// (Note: Closing the file stream is unnecessary in this context as it's a 
// synchronous read performed using fs.readFileSync, which does not require
// explicit closure, but if using fs.createReadStream, you would handle it accordingly)

Arbeta med kolumner

Hur man ställer in bredden på en kolumn

Sätt kolumnbredden genom att anropa Cells-samlingens setColumnWidth(number, number)-metod. setColumnWidth(number, number)-metoden tar följande parametrar:

  • Kolumnindex, index för den kolumn vars bredd du ändrar.
  • Kolumnbredd, önskad kolumnbredd.
const fs = require("fs");
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, "book1.xls");

// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);

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

// Setting the width of the second column to 17.5
worksheet.getCells().setColumnWidth(1, 17.5);

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

// Closing the file stream to free all resources
fstream; // Note: No explicit close needed for fs.readFileSync

Hur man ställer in kolumnbredd i pixlar

Sätt kolumnbredden genom att anropa Cells-samlingens setColumnWidthPixel(number, number)-metod. setColumnWidthPixel(number, number)-metoden tar följande parametrar:

  • Kolumnindex, index för den kolumn vars bredd du ändrar.
  • Kolumnbredd, önskad kolumnbredd i pixlar.
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");
const outDir = path.join(__dirname, "output");

// Load source Excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Book1.xlsx"));

// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);

// Set the width of the column in pixels
worksheet.getCells().setColumnWidthPixel(7, 200);

workbook.save(path.join(outDir, "SetColumnWidthInPixels_Out.xlsx"));

Hur man ställer in bredden på alla kolumner i en arbetsbok

För att sätta samma kolumnbredd för alla kolumner i arbetsbladet använd Cells-samlingens getStandardWidth()-egenskap.

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

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

// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "book1.xls");

// Instantiating a Workbook object
// Opening the Excel file
const workbook = new AsposeCells.Workbook(filePath);

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

// Setting the width of all columns in the worksheet to 20.5
worksheet.getCells().setStandardWidth(20.5);

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

// Closing the file stream to free all resources
// No explicit close needed in Node.js

Fortsatta ämnen