Import data using xlsx package

Asked

Viewed 69 times

1

Hello

Sometimes there is a need to import excel file to the database in order to take a shortcut to avoid Inserts one by one

So, I made a function in Vue.js that imports an excel spreadsheet

import xlsx from 'xlsx'
importExcel(e) {
            this.$emit('buscando', true)
            const files = e.target.files;            
            if (!files.length) {
                return ;
            } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
                return alert("The upload format is incorrect. Please upload xls or xlsx format");
            }
            const fileReader = new FileReader();
            fileReader.onload = async ev => {
                try {
                    const data = ev.target.result;
                    const XLSX = xlsx;
                    const workbook = XLSX.read(data, {
                        type: "binary"
                    });
                    const wsname = workbook.SheetNames[0]; // Take the first sheet,wb.SheetNames[0] :Take the name of the first sheet in the sheets
                    const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]); // Generate JSON table content,wb.Sheets[Sheet名]    Get the data of the first sheet

                    
                    for (var i = 0; i < ws.length; i++) {        
                         const campoData = ws[i]['DATA INÍCIO']
                     }   // fim for

                } catch (e) {
                    console.log('erro',e);
                    return alert(`Houve um problema na importação. Verifique as colunas e tente novamente`);;
                }
            };
            fileReader.readAsBinaryString(files[0]);
            var input = document.getElementById("upload");
            input.value = "";

How difficult?

It is in the date field:

For example: 01/01/2021

When I read the excel file it this sample date comes in an entire format: '44197' for example

How do I read exactly what has in the cell and not this transformation that excel itself does?

The library I’m using is the xlsx

No answers

Browser other questions tagged

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