Set column type for text using exceljs

Asked

Viewed 11 times

0

Hello!

I am using exceljs to export excel file

I would like to define before exporting the column type to String

Currently I’m doing so:

const EXCEL_TYPE =  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
const EXCEL_EXTENSION =  '.xlsx'
const baixar = () =>{
  console.log('baixar')
}
const FileSaver = (t,f,m) => {
   try {
       var b = new Blob([t],{type:m});
       saveAs(b, f);
     } catch (e) {
        window.open("data:"+m+"," + encodeURIComponent(t), '_blank','');
     }
  }
 const exportFile = async () => {
  const workbook = new Excel.Workbook()
    const worksheet = workbook.addWorksheet('DADOS')            
        
    const header = [
         ['Start Date','Final Date']
    ]
  const rows = [
         ['10/01/2021','15/01/2021'],
         ['11/01/2021','21/01/2021']
   ]

  worksheet.addRows( header )
    
    worksheet.addRows( rows )
  worksheet.getColumn(1).dataValidation = {
      type: 'text'
  }
        
    let buffer = await workbook.xlsx.writeBuffer()
    saveAsExcelFile( buffer, 'action_type' )

            
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.2.0/exceljs.min.js" integrity="sha512-3ZnqnLCRnyAXG2Hbb0v+DNzl5QYyRocJeG7Uwy5qBausA0jB5cLSZ4gHWHkbINJIyWU3vApjCwH1rNO4XEn9SQ==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js" integrity="sha512-csNcFYJniKjJxRWRV1R7fvnXrycHP6qDR21mgz1ZP55xY5d+aHLfo9/FcGDQLfn2IfngbAHd8LdfsagcCqgTcQ==" crossorigin="anonymous"></script>

<button onclick="exportFile()">baixar</button>

I’m trying like this:

worksheet.getColumn(1).dataValidation = {
                type: 'text'
            }

When I type a date, excel turns into number.

Then every time I care, instead of seeing the date comes a number

For example:

The date of:

05/01/2021 vem 44201
10/01/2021 vem 44206

Every time before it matters I have to go on screen Formatar célula and switch to text in order to date and recognize the system as text

inserir a descrição da imagem aqui

Is there or at least there is some way to convert this number to the correct date?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.