Primeiro, você precisa **entender que o xlsx / SheetJS community edition padrão não suporta estilo de célula**a partir de agosto de 2023.
Portanto, você precisa usar o fork xlsx-js-style que você pode instalar usando
install_xlsx_js_style.sh
npm i --save xlsx-js-styleAgora, baseado em nosso exemplo anterior, use estas importações
bold_cell.ts
import * as XLSX from 'xlsx-js-style';
import { saveAs } from 'file-saver';e este código para gerar um arquivo XLSX com uma célula em negrito:
bold_cell.ts
// Create an empty workbook
const wb = XLSX.utils.book_new();
// Create an empty worksheet
// If this weren't a minimal example, your data would go here
const ws = XLSX.utils.aoa_to_sheet([
["Test"]
]);
// Make cell bold
ws["A1"].s = {
font: {
bold: true,
}
};
// Add the worksheet to the workbook
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
// Export the workbook to XLSX format
const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'buffer' });
// Convert the binary data to a Blob
const blob = new Blob([wbout], { type: 'application/vnd.ms-excel' });
// Download of the file using file-saver
saveAs(blob, 'example.xlsx');
Check out similar posts by category:
JavaScript Typescript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow