add cell backgroup color use exceljs
- User
add cell backgroup color use exceljs
- Assistant
In ExcelJS, you can set the background color of a cell using the
fillproperty. Here's an example of how you can add cell background color using ExcelJS in JavaScript:First, install the ExcelJS library by running the following command in your terminal or command prompt:
npm install exceljsThen, you can use the following code to create an Excel file with a cell that has a background color:
const ExcelJS = require('exceljs'); async function createExcelFile() { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Sheet 1'); const cell = worksheet.getCell('A1'); cell.value = 'Hello, World!'; // Set the background color of the cell cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFFF0000' } // red color }; await workbook.xlsx.writeFile('output.xlsx'); } createExcelFile().then(() => { console.log('Excel file created successfully!'); }).catch((error) => { console.error('Error creating Excel file:', error); });In the above code, we first import the ExcelJS library. Then, we create a new workbook and add a worksheet called "Sheet 1". We define a cell object using the
getCellmethod and set its value to "Hello, World!".To set the background color of the cell, we use the
fillproperty of the cell object. Thefillproperty is an object that consists of the type of fill (in this case, a solid pattern fill), the pattern type, and the foreground color. In this example, we set the foreground color to red.Finally, we save the workbook to a file called "output.xlsx" using the
writeFilemethod.