繁体   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