Angular如何使用格式单元格导出到Excel?

时间:2019-07-01 06:31:19

标签: angular export-to-excel

使用方法exportAsExcelFile的My Excel Service:

#include <stdio.h>
#include <stdlib.h>

int bintest();

int main()
{
        int* x = malloc(sizeof(int));
        printf("\n\n*****\nEnter value of x\n*****\n");
        scanf("\n%d", &x);
        bintest(x); 
}

int bintest(int* y)
{
        printf("\ny: %d", y);
}

我的组件,具有exportDataToExcel()方法:

    public exportAsExcelFile(json: any[], excelFileName: string): void {
    const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
    const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
    const excelBuffer: any = XLSX.write(workbook, { bookType: 'xls', type: 'array' });
    this.saveAsExcelFile(excelBuffer, excelFileName);
  }

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

如何在Excel中使用格式化“会计”单元格导出json total_price?

1 个答案:

答案 0 :(得分:0)

您使用的实用程序不支持开箱即用的单元格格式。您需要自己处理一下sheetjs。

您可以迭代单元格并相应地更改cell.z属性,因为它代表单元格格式。

/* new format */
var fmt = "0.00";
/* change cell format of range B2:D4 */
var range = { s: {r:1, c:1}, e: {r:2, c:3} };
for(var R = range.s.r; R <= range.e.r; ++R) {
  for(var C = range.s.c; C <= range.e.c; ++C) {
    var cell = ws[XLSX.utils.encode_cell({r:R,c:C})];
    if(!cell || cell.t != 'n') continue; // only format numeric cells
    cell.z = fmt;
  }
}

这是一个小提琴的例子: https://jsfiddle.net/1ny97xrb/1/

https://github.com/SheetJS/js-xlsx/issues/966