首页 > 解决方案 > 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'

标签: angular

解决方案


推荐阅读