以角度为单元格xlsx上色

coloring cell xlsx in angular(以角度为单元格xlsx上色)

本文介绍了以角度为单元格xlsx上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想根据一个值将红色或绿色放在我的Exel行以进行下载: 如何在工作表中设置指定单元格的颜色。 下面是处理Exel格式的ts类:

最高:

import { Injectable } from '@angular/core';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
import { formatDate } from '@angular/common';
import { ExcelColMapper } from 'src/app/shared/services/excelexport/ExcelColMapper';

const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';
const EXCEL_DATE_FORMAT = 'yyyyMMdd';
@Injectable({
  providedIn: 'root'
})

export class ExcelService {

  constructor() { }
  public exportAsExcelFile(json: any[], excelFileName: string): void {
    // liste des width des colonnes
    const wscols = [];
    Object.values(ExcelColMapper.keys).forEach(element => {
      wscols.push({ wch: element.length });
    });
    const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json, { dateNF: 'YYYYMMDD HH:mm:ss'});
    const range = XLSX.utils.decode_range(worksheet['!ref']);
    for (let C = range.s.r, index = 0; C <= range.e.r; ++C, index++) {
      const col = XLSX.utils.encode_col(C) + '1';
      if (!worksheet[col]) { continue; }
      worksheet[col].v = Object.values(ExcelColMapper.keys)[index];
    }
    worksheet['!cols'] = wscols;
    worksheet.write_row("A1:C1", ['Ray','Ray2','Ray3']);
    const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
    const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
    this.saveAsExcelFile(excelBuffer, excelFileName);
  }

  saveAsExcelFile(buffer: any, fileName: string): void {
    const data: Blob = new Blob([buffer], { type: EXCEL_TYPE});
    FileSaver.saveAs(data, fileName + formatDate(new Date(), EXCEL_DATE_FORMAT, 'en') + EXCEL_EXTENSION);
  }


}

谢谢

推荐答案

cell.s = {
    fill: {
        bgColor: {rgb: "${color_code}"}
    },
    font: {
        color: {rgb: "${hashed_color_code}"}
    },
}

更多详情,请参阅库官方文档: https://www.npmjs.com/package/xlsx-with-styles

这篇关于以角度为单元格xlsx上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以角度为单元格xlsx上色

基础教程推荐