【问题标题】:export excel sheet cell with specified date format导出具有指定日期格式的 Excel 工作表单元格
【发布时间】:2018-04-03 20:38:34
【问题描述】:

我正在导出带有一些标题值的空 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', 粗体:真实, 日期格式:'mm-dd-yyyy' }; InstructionSheet.getCell('A22','mm-dd-yyyy').value = 'F: 开始日期';

生成excel表格时如何设置日期列的指定日期格式。

提前致谢

【问题讨论】:

    标签: angularjs node.js date exceljs npmjs


    【解决方案1】:

    您可以使用 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');
    

    【讨论】:

      【解决方案2】:

      您可以在定义这样的列时决定采用什么格式。

       ...
        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 }
        ];
        ...
      

      【讨论】:

        猜你喜欢
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 2014-07-26
        相关资源
        最近更新 更多