Applet to read excel spreadsheet data, and stored in the cloud database

Has recently been busy, they agreed to resolve excel applet has not been written, and finally sneak Today, organic write this article will give you all.

The old rules look at the renderings

Applet to read excel spreadsheet data, and stored in the cloud database
The effect is actually very simple, is to excel in the parsed data, then save to the cloud database. That is simple. But still a lot of time to really do it, and found something to use. Do not believe. . . . That look at the flow chart

flow chart

Applet to read excel spreadsheet data, and stored in the cloud database
Through flow charts, I look to us to use cloud functions, cloud storage, cloud database.
Flowchart mainly implements the following steps

  • 1, wx.chooseMessageFile choose to parse excel spreadsheet
  • 2, by wx.cloud.uploadFile excel upload files to the cloud storage
  • 3, cloud storage is returned to us a fileid
  • 4, the definition of a function cloud excel
  • 5, the transfer step 3 returns to excel cloud function fileid
  • 6, parsing excel in excel function in the cloud, and add data to the cloud database.

You can see the most mysterious, the most important thing we excel cloud function.

So we put the first five steps to achieve, and behind our focus on the next cloud excel function.

First, select and upload files to the cloud storage excel spreadsheet

Here we use the cloud to develop, use cloud development must first register a small program, and give yourself a small program launched cloud development capabilities. The knowledge I have said many times, do not know how to open and use cloud development of students, I look through under the previous articles, I look or recorded video to explain the "five hours Getting applet cloud development."

  • 1, first define our pages
    page is very simple, it is a button in the following figure, calling chooseExcel method when you click the button, choose Excel
    Applet to read excel spreadsheet data, and stored in the cloud database
    wxml code corresponds to the following
    Applet to read excel spreadsheet data, and stored in the cloud database
  • 2, the preparation method of file selection and upload files
    Applet to read excel spreadsheet data, and stored in the cloud database
    on a map chooseExcel is our excel file selection method.
    uploadExcel is our file upload method, after a successful upload will return a fildID. We pass fildID to our jiexi method, jiexi follows
  • 3 fildID transfer function to the cloud
    Applet to read excel spreadsheet data, and stored in the cloud database

Second, the solution down is the definition of the function of our cloud.

  • 1, we need to create a new cloud function
    Applet to read excel spreadsheet data, and stored in the cloud database
    if you do not know how to create a cloud function, you can look at the article I wrote before, we can see a video I recorded "five hours Getting applet cloud development"
    as shown in excel our function is to create cloud
    Applet to read excel spreadsheet data, and stored in the cloud database
  • 2, node-xlsx installation dependencies
    Applet to read excel spreadsheet data, and stored in the cloud database
    shown above, right-excel, and click Open in the terminal. After opening the terminal,
    the input npm install node-xlsx installation dependent. You can see the installation progress bar in the figure below
    Applet to read excel spreadsheet data, and stored in the cloud database
    this step you need to install on your computer through node.js and configure npm command.
  • 3, node-xlsx dependent libraries installation complete
    Applet to read excel spreadsheet data, and stored in the cloud database

Third, write cloud function

I posted the complete code for everyone

const cloud = require('wx-server-sdk')
cloud.init()
var xlsx = require('node-xlsx');
const db = cloud.database()

exports.main = async(event, context) => {
  let {
    fileID
  } = event
  //1,通过fileID下载云存储里的excel文件
  const res = await cloud.downloadFile({
    fileID: fileID,
  })
  const buffer = res.fileContent

  const tasks = [] //用来存储所有的添加数据操作
  //2,解析excel文件里的数据
  var sheets = xlsx.parse(buffer); //获取到所有sheets
  sheets.forEach(function(sheet) {
    console.log(sheet['name']);
    for (var rowId in sheet['data']) {
      console.log(rowId);
      var row = sheet['data'][rowId]; //第几行数据
      if (rowId > 0 && row) { //第一行是表格标题,所有我们要从第2行开始读
        //3,把解析到的数据存到excelList数据表里
        const promise = db.collection('users')
          .add({
            data: {
              name: row[0], //姓名
              age: row[1], //年龄
              address: row[2], //地址
              wechat: row[3] //wechat
            }
          })
        tasks.push(promise)
      }
    }
  });

  // 等待所有数据添加完成
  let result = await Promise.all(tasks).then(res => {
    return res
  }).catch(function(err) {
    return err
  })
  return result
}

The above code annotation is very clear, and I am here not long-winded in Hello.
There are several points to note to tell you that the next

  • 1, first create a data table
    image.png

  • 2, sometimes if the resolution fails always, may be some computer needs to have a cloud development environment in the cloud initialization function in
    小程序读取excel表格数据,并存储到云数据库

Fourth, parsed and uploaded successfully

As my form, there are the following three data
小程序读取excel表格数据,并存储到云数据库
click the Upload button and select our spreadsheet file
小程序读取excel表格数据,并存储到云数据库
upload successful return the following, you can see we've added three data to the database
小程序读取excel表格数据,并存储到云数据库

FIG effect added successfully as
小程序读取excel表格数据,并存储到云数据库

Here we have to complete implementation of small programs excel upload data to the database function.

Look again with a flowchart

小程序读取excel表格数据,并存储到云数据库

If you are experiencing problems, you can leave a message at the bottom, I saw will promptly answer. I'll write more later applet cloud development combat the article came out. This section will also record video out, so stay tuned.

Guess you like

Origin blog.51cto.com/14368928/2454039