在angular7中导入excel并使用node js保存到数据库中

Import excel in angular 7 and save into database using node js

我有一个页面要上传excel

    <div class="form-group">
    <label>Select file to upload.</label>
    <input type="file" class="form-control" (change)="onFileChange($event);">
    </div>
    <button type="button" (click)="Upload()" class="btn btn-success pull-right"><i class="fa fa-save fa-fw"></i> Upload File</button>

下面是我的 upload() 函数和 component.ts

中的 onFileChange()
 onFileChange(event) {
   if (event.target.files.length > 0) {
    this.file = event.target.files[0];
   }
 }

 Upload() {
      let fileReader = new FileReader();
    fileReader.onload = (e) => {
        this.arrayBuffer = fileReader.result;
        var data = new Uint8Array(this.arrayBuffer);
        var arr = new Array();
        for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
        var bstr = arr.join("");
        var workbook = XLSX.read(bstr, {type:"binary"});
        var first_sheet_name = workbook.SheetNames[0];
        var worksheet = workbook.Sheets[first_sheet_name];
        this.exceljsondata = XLSX.utils.sheet_to_json(worksheet,{raw:true, defval:""});

       console.log(this.exceljsondata);
        this.providerservice.importexcel(this.exceljsondata).subscribe(data=>{
        })
    }
    fileReader.readAsArrayBuffer(this.file);
}

上面console.log(this.exceljsondata)的输出如下

  0: {Your First Name: "", Your Last Name: "", Agency/Practice Name: "2-1-1 Big Bend", Agency's Location - Address: "", Agency's Location - Address (House Number): "", …}
  1: {Your First Name: "Melanie", Your Last Name: "Rosemberg", Agency/Practice Name: "360º Therapy", Agency's Location - Address: "1380 NE Miami Gardens Dr Suite 242", Agency's Location - Address (House Number): "1380", …}

这是我的提供商服务,我将 excel json 数据发布到节点 js

   public importexcel(providrdata):Observable<any> {
        return this.http.post(this.baseUrl+"provider/importexcel", JSON.stringify(providrdata), httpOptions).pipe(map((res)=> res));
    }

这是我在 node js 中的函数。我正在尝试将此 json 数据插入数据库

    router.post('/importexcel',(req, res) => {
       console.log(req.body)
    })

这里在nodejs中console.log的输出如下

[ { 'Your First Name': '',
    'Your Last Name': '',
    'Agency/Practice Name': '2-1-1 Big Bend',
    'Agency\'s Location - Address': '',
    'Agency\'s Location - Address (House Number)': '',
    'Agency\'s Location - Address (Street)': '',
    'Agency\'s Location - City': '',
    'Agency\'s Location - State': '',
    'Agency\'s Location - Postal Code': '',
    'Practice/Agency Location: County': '',
    'Phone Number': '211 and (850) 617-6333',
    Email: '',
    Website: 'http://www.211bigbend.org',
    'Which category best describes your private practice or agency?* - Selected Choice': 'Hotline',
    'Which category best describes your private practice or agency?* - Other - Text': '',
    'Please select any additional descriptors which fit your private practice or agency. - Selected Choice': '',
    'Please select any additional descriptors which fit your private practice or agency. - Other - Text': '',
    Specialty:
     'Crisis Counseling,Maternal Mental Health,Referrals,Resource,Suicide Prevention',
    'Insurance - Selected Choice': 'No charge for services',
    'Insurance - Other - Text': '',
    Serves:
     'Individual,Adolescents,Caregivers,Children,Couples,Family,Geriatrics,Groups,Women',
    'Areas Served':
     'Franklin,Gadsden,Jefferson,Leon,Liberty,Madison,Taylor,Wakulla',
    'Days/Hours of Operation - Selected Choice': 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday',
    'Days/Hours of Operation - Monday - Text': '',
    'Days/Hours of Operation - Tuesday - Text': '',
    'Days/Hours of Operation - Wednesday - Text': '',
    'Days/Hours of Operation - Thursday - Text': '',
    'Days/Hours of Operation - Friday - Text': '',
    'Days/Hours of Operation - Saturday - Text': '',
    'Days/Hours of Operation - Sunday - Text': '',
    'Do you provide telehealth services? - Selected Choice': '',
    'Do you provide telehealth services? - Yes (other) - Text': '',
    'Accepting New Clients?': 'Yes',
    'Additional Information/Description': '' },
  { 'Your First Name': 'Melanie',
    'Your Last Name': 'Rosemberg',
    'Agency/Practice Name': '360º Therapy',
    'Agency\'s Location - Address': '1380 NE Miami Gardens Dr Suite 242',
    'Agency\'s Location - Address (House Number)': '1380',
    'Agency\'s Location - Address (Street)': 'NE Miami Gardens Dr Suite 242',
    'Agency\'s Location - City': 'North Miami Beach',
    'Agency\'s Location - State': 'Florida',
    'Agency\'s Location - Postal Code': 33179,
    'Practice/Agency Location: County': 'Miami-Dade',
    'Phone Number': 3053966009,
    Email: 'melanierosemberg@gmail.com',
    Website: 'www.melanierosemberg.com',
    'Which category best describes your private practice or agency?* - Selected Choice': 'Licensed Mental Health Counselor',
    'Which category best describes your private practice or agency?* - Other - Text': '',
    'Please select any additional descriptors which fit your private practice or agency. - Selected Choice': 'Support Group',
    'Please select any additional descriptors which fit your private practice or agency. - Other - Text': '',
    Specialty:
     'Anxiety,Counseling,Depression,Maternal Mental Health,Psychotherapy',
    'Insurance - Selected Choice': 'Self pay by check or cash,Sliding scale fees available',
    'Insurance - Other - Text': '',
    Serves: 'Individual,Adolescents,Caregivers',
    'Areas Served': 'Broward,Miami-Dade',
    'Days/Hours of Operation - Selected Choice': 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
    'Days/Hours of Operation - Monday - Text': 43717,
    'Days/Hours of Operation - Tuesday - Text': 43717,
    'Days/Hours of Operation - Wednesday - Text': 43717,
    'Days/Hours of Operation - Thursday - Text': 43717,
    'Days/Hours of Operation - Friday - Text': 43712,
    'Days/Hours of Operation - Saturday - Text': 43680,
    'Days/Hours of Operation - Sunday - Text': '',
    'Do you provide telehealth services? - Selected Choice': 'Yes (via video),Yes (via phone)',
    'Do you provide telehealth services? - Yes (other) - Text': '',
    'Accepting New Clients?': 'Yes',
    'Additional Information/Description': '' } ]

如何在 'Your First Name'、'Your Last Name'、'Agency/Practice Name' 等中获取值,因为它们之间有 space。我不能像下面这样称呼它

 router.post('/importexcel',(req, res) => {
   for(lnt of req.body){
       var firstname = lnt.'Your First Name';
       var lastname = lnt.'Your Last Name';
   }
})

如何获取这些变量。因为我需要将它们插入数据库。请帮忙

您可以使用 JavaScript bracket notation 访问这些对象的所需属性。

例如:

let obj = {};

// Set properties using bracket notation
obj['First Name'] = 'Jim';
obj['Last Name'] = 'Smith';

// We can still use .dot notation if we wish
obj.Age = 42;

// Access properties using bracket notation
console.log('Accessing object properties using [bracket] notation:');
console.log('First name: ' + obj['First Name']);
console.log('Last name: ' + obj['Last Name']);
console.log('Age: ' + obj['Age']);


// Enumerate using Object.entries:
console.log('\nEnumerating object entries:');
for (let [key, value] of Object.entries(obj)) {
    console.log(`${key}: ${value}`);
}