2

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

N15
  • 305
  • 2
  • 4
  • 17

2 Answers2

0

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');
0

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 }
  ];
  ...
Randi Pratama
  • 102
  • 1
  • 8