簡體   English   中英

導出具有指定日期格式的excel表格單元格

[英]export excel sheet cell with specified date format

我正在導出帶有一些標題值的空 Excel 工作表。 在那個 excel 表中,我想為日期字段設置日期格式,如 (mm/dd/yyyy)。 如何為該單元格設置指定的格式。

在這里我定義了excel表格單元格:

    var InstructionSheet = workbook.addWorksheet('Instruction');
   InstructionSheet.getCell('A22').value = 'F: Start Date';
  InstructionSheet.getCell('A22').font = {
        name: 'Calibri',
        bold: true

    };
  worksheet.columns = [
        { header: 'ProjectName', key: 'id', width: 10 },
        { header: 'UniqueID', key: 'name', width: 40 },
        { header: 'Name', key: 'name', width: 40 },
        { header: 'ResourceNames', key: 'name', width: 32 },
        { header: 'Type', key: 'name', width: 32 },
        { header: 'IsBillable', key: 'name', width: 12 },
        { header: 'IsCR', key: 'name', width: 12 },
        { header: 'Duration', key: 'name', width: 25 },
        { header: 'StartDate', key: 'string', width: 25 },
        { header: 'EndDate', key: 'string', width: 25 },
        { header: 'Predecessors', key: 'string', width: 25 },
        { header: 'Phase', key: 'string', width: 25 },

    ];
  worksheet.getCell('I1').font = {  // Start Date column
        name: 'Calibri',

        bold: true
          };

我試圖以這種方式設置日期格式。 但它不起作用

InstructionSheet.getCell('A22').font = { 名稱:'Calibri',粗體:true,日期格式:'mm-dd-yyyy'}; InstructionSheet.getCell('A22','mm-dd-yyyy').value = 'F: 開始日期';

如何在生成 Excel 表格時為日期列設置指定的日期格式。

提前致謝

您可以使用 moment js 進行日期格式化。

const moment = require('moment');

const dateFormat = async function (date, format) {
  return moment(date).format(format);
}

worksheet.getColumn('A').value = await dateFormat(new Date(), 'YYYY-MM-DD');

您可以在定義這樣的列時決定采用何種格式。

 ...
  worksheet.columns = [
        { header: 'ProjectName', key: 'id', width: 10 },
        { header: 'UniqueID', key: 'name', width: 40 },
        { header: 'Name', key: 'name', width: 40 },
        { header: 'ResourceNames', key: 'name', width: 32 },
        { header: 'Type', key: 'name', width: 32 },
        { header: 'IsBillable', key: 'name', width: 12 },
        { header: 'IsCR', key: 'name', width: 12 },
        { header: 'Duration', key: 'name', width: 25 },
        { header: 'StartDate', key: 'string', width: 25, style: { numFmt: 'mm-dd-yyyy' } },
        { header: 'EndDate', key: 'string', width: 25, style: { numFmt: 'mm-dd-yyyy' } },
        { header: 'Predecessors', key: 'string', width: 25 },
        { header: 'Phase', key: 'string', width: 25 }
  ];
  ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM