angular - XSLXWriter 和 Angular 5,无法更改 Excel 文件中的标题样式
问题描述
谁能帮我把 excel 标题加粗?我正在使用 file-saver 、 xlsx 和 xlsx 样式库在 Angular 5 中导出 excel 文件,但无法更改标题样式。如果没有 xlsx-style ,它可以工作,但它不会应用该样式,而使用 xlsx-style ,它会在发球期间引发错误。
Dependencies are like :
"@angular/core": "^5.2.0",
"file-saver": "^1.3.8",
"xlsx": "^0.13.2",
"xlsx-style": "^0.8.13"
这是 file-export.service.ts :
import { Injectable } from '@angular/core';
import * as FileSaver from 'file-saver';
import { utils, WorkBook, WorkSheet, CellObject } from 'xlsx';
import { write } from 'xlsx-style';
const EXCEL_TYPE = 'application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';
@Injectable()
export class FileExportService {
constructor() { }
public exportAsExcelFile(json: any[], excelFileName: string): void {
const worksheet: WorkSheet = utils.json_to_sheet(json);
this.wrapAndCenterCell(worksheet.A1);
const workbook: WorkBook= { Sheets: { 'data': worksheet }, SheetNames:
['data'] };
const excelBuffer: any = write(workbook, {bookType: 'xlsx', type:
'array'});
this.saveAsExcelFile(excelBuffer, excelFileName);
}
private wrapAndCenterCell(cell: CellObject) {
const wrapAndCenterCellStyle = { font: {bold:true} };
this.setCellStyle(cell, wrapAndCenterCellStyle);
}
private setCellStyle(cell, style: {}) {
cell.s = style;
}
private saveAsExcelFile(buffer: any, fileName: string): void {
const data: Blob = new Blob([buffer], { type: EXCEL_TYPE });
FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() +
EXCEL_EXTENSION);
}
}
在服务时,它会引发此错误:
./node_modules/xlsx-style/xlsx.js 中的警告未找到模块:错误:无法解析“D:\SAP\code\test\myApp\node_modules\xlsx-style”中的“crypto”
./node_modules/xlsx-style/xlsx.js 中的错误 找不到模块:错误:无法解析 'D:\SAP\code\test\myApp\node_modu les\xlsx-style' 中的 'fs' ./ node_modules/xlsx-style/ods.js 未找到模块:错误:无法解析 'D:\SAP\code\test\myApp\node_modu les\xlsx-style' 中的 'fs'
解决方案
推荐阅读
- java - IntelliJ:如果我调用带有 @VisibleForTesting 注释的方法,则会收到警告
- ansible - 如何以适当的方式定义多个目录来维护特定于目录的属性?
- c# - 如何使用 .ThenInclude()c 从 .Include() 中获取价值
- python-3.x - 访问python字典中的元素
- javascript - 在单选按钮上执行 onChange 函数
- php - php函数没有返回值
- image - Flutter :: Amazon S3 图像上传
- appium - “退出应用”也会清除应用数据
- haskell - 我的函数返回最后一个列表有错误
- javascript - Amsul DatePicker - 如何禁用日历上的日期?