简体   繁体   中英

export excel sheet cell with specified date format

I am exporting empty excel sheet with some header values. In that excel sheet I want to set the date format like (mm/dd/yyyy) for date field. How to set the specified format for that cell.

Here I defined excel sheet cells:

    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','mm-dd-yyyy').value = 'F: Start Date';

How do I set the specified date format for the date column while generating the excel sheet.

Thanks in advance

You could use moment js for date formating.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM