简体   繁体   English

导出具有指定日期格式的excel表格单元格

[英]export excel sheet cell with specified date format

I am exporting empty excel sheet with some header values.我正在导出带有一些标题值的空 Excel 工作表。 In that excel sheet I want to set the date format like (mm/dd/yyyy) for date field.在那个 excel 表中,我想为日期字段设置日期格式,如 (mm/dd/yyyy)。 How to set the specified format for that cell.如何为该单元格设置指定的格式。

Here I defined excel sheet cells:在这里我定义了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
          };

I tried to set the date format in this way.我试图以这种方式设置日期格式。 But it is not working但它不起作用

InstructionSheet.getCell('A22').font = { name: 'Calibri', bold: true, dateformate:'mm-dd-yyyy' }; InstructionSheet.getCell('A22').font = { 名称:'Calibri',粗体:true,日期格式:'mm-dd-yyyy'}; InstructionSheet.getCell('A22','mm-dd-yyyy').value = 'F: Start Date'; InstructionSheet.getCell('A22','mm-dd-yyyy').value = 'F: 开始日期';

How do I set the specified date format for the date column while generating the excel sheet.如何在生成 Excel 表格时为日期列设置指定的日期格式。

Thanks in advance提前致谢

You could use moment js for date formating.您可以使用 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');

You can decide what format when defining the columns like this.您可以在定义这样的列时决定采用何种格式。

 ...
  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