nodejs导出excel----exceljs (1)

导出excel的库

exceljs可导出excel,提供了可支持文件路径及文件流的方式去导出

使用方法

import * as Excel from 'exceljs'; ; // 如果是js则为 var Excel = require('exceljs');
import * as Stream from 'stream';
import { CellData } from '../models/base/zzj-cell.base';
const tinytinycolor  = require('tinytinycolor'); //颜色值的生成
实例化和引入excel
     this.workBook = new Excel.Workbook();
     if(Object.prototype.toString.call(excel) == '[object String]') {
         await this.workBook.xlsx.readFile(<string>excel);
     } else {
         await this.workBook.xlsx.read(excel);
     }

.read(excel)中excel为流,readFile(excel)为excel的文件路径,并且这个两个方法返回都是Promise,可以接着用then继续后续的操作

选取使用worksheet
this.workBook.getWorksheet(this.worksheetNum); //从1开始计数

这里写图片描述

worksheet中方法

这里写图片描述
这里写图片描述

合并了的单元格信息是单独保存了

获取合并单元格的信息
(<any>this.worksheet)._merges; // _开头的方法或变量都是约定的私有的,所以在index.d.ts中没声明,js中就没影响了

mergeCell的结构为
这里写图片描述

获取单元格的值

官方中提供了遍历每一行每一列的方法

worksheet.eachRow((row, rowNum) => {
    // 遍历每一行
    row.eachCell((cell,col)=>{
        //遍历这一行中的每一个单元格
    });
});

官方提供的这两个方法有个缺点,不管这个单元是否修改了样式和别的属性,只要是没有值都是不会被遍历的,所以

    public eachRow(callBack: EachCellCallback) {
        let row = (<any>this.worksheet)._rows;
        for(let i = 0; i < row.length; i++) {
            if(row[i] == undefined) continue;
            callBack(row[i], i); 
        }        
    }
单元格的数据结构
export interface Cell extends Style, Address {
    readonly worksheet: Worksheet;
    readonly workbook: Workbook;

    readonly effectiveType: ValueType;
    readonly isMerged: boolean;
    readonly master: Cell;
    readonly isHyperlink: boolean;
    readonly hyperlink: string; // todo
    readonly text: string;
    readonly fullAddress: {
        sheetName: string;
        address: Address;
        row: Row;
        col: Column;
    };
    model: CellModel;
    /**
     * Assign (or get) a name for a cell (will overwrite any other names that cell had)
     */
    name: string;

    /**
     * Assign (or get) an array of names for a cell (cells can have more than one name)
     */
    names: string[];

    /**
     * Cells can define what values are valid or not and provide
     * prompting to the user to help guide them.
     */
    dataValidation: DataValidation;

    /**
     * Value of the cell
     */
    value: CellValue;

    /**
     * convenience getter to access the formula
     */
    readonly formula: string;

    /**
     * convenience getter to access the formula result
     */
    readonly result: number | string | Date;

    /**
     * The type of the cell's value
     */
    readonly type: ValueType;

    /**
     * The type of the cell's formula
     */
    readonly formulaType: FormulaType;

    /**
     * The styles of the cell
     */
    style: Partial<Style>;

    addName(name: string): void;

    /**
     * Remove a name from a cell
     */
    removeName(name: string): void;
    removeAllNames(): void;

    destroy(): void;
    toCsvString(): string;
    release(): void;
    addMergeRef(): void;
    releaseMergeRef(): void;
    merge(master: Cell): void;
    unmerge(): void;
    isMergedTo(master: Cell): boolean;
    toString(): string;
}
获取单元格的值

既是单元格结构体中的value,但是单元格的value也是有中类型,类型有:

export type CellValue =
    | null | number | string | boolean | Date
    | CellErrorValue
    | CellRichTextValue | CellHyperlinkValue
    | CellFormulaValue | CellSharedFormulaValue;

所以获取值的方法

    public getValue(cell: Excel.Cell, CellObj: CellData) {
        try {
            let valueType = Object.prototype.toString.call(cell.value);
            let cellValue = <any>cell.value;
            if(valueType == '[object Object]') {
                if(cell.value.hasOwnProperty('richText')) {
                    let val = '';
                    for(let obj of cellValue.richText) {
                        val += obj.text;
                    }
                    CellObj.value = val;
                }
                if(cell.value.hasOwnProperty('error')) {
                    CellObj.value = cellValue.error
                }
                if(cell.value.hasOwnProperty('hyperlink')) {
                    CellObj.value = cellValue.text + cellValue.hyperlink;
                }
                if(cell.value.hasOwnProperty('sharedFormula')) {

                }
            } else if(valueType == '[object String]' || valueType == '[object Number]') {
               CellObj.value = cell.text; 
            } else if (valueType == '[object Null]') {

            } else if(valueType == '[object Boolean]') {

            } else if(valueType == '[object Date]') {

            }      
        } catch (error) {
            CellObj.value = '';
        }

    }

只是写了几中常用的
获取别,明天写……

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值