Hướng dẫn exceljs nodejs example - ví dụ về exceljs nodejs

→ Nhiều lần khi chúng tôi có một số báo cáo hoặc ứng dụng bảng quản trị, chúng tôi cần tải xuống dữ liệu trong tệp Excel.

Nội dung chính ShowShow

  • Repository:
  • Bớt tư tưởng:
  • Làm cách nào để sử dụng Node JS làm cơ sở dữ liệu trong Excel?
  • Làm cách nào để xuất dữ liệu từ Node JS sang Excel?
  • Làm cách nào để lưu dữ liệu JSON trong Excel?
  • Làm cách nào để chuyển đổi nút thành CSV trong Excel?

Nội dung chính ShowShowShow

  • Repository:
  • Bớt tư tưởng:
  • Làm cách nào để sử dụng Node JS làm cơ sở dữ liệu trong Excel?
  • Làm cách nào để xuất dữ liệu từ Node JS sang Excel?
  • Làm cách nào để lưu dữ liệu JSON trong Excel?
  • Làm cách nào để chuyển đổi nút thành CSV trong Excel?

Nội dung chính ShowShowany JavaScript framework. For this article, we will use node with express.js.

Repository:

Bớt tư tưởng:

express <your_app_name> --view=ejs

→ Điều này có thể đạt được trong bất kỳ khung JavaScript nào. Đối với bài viết này, chúng tôi sẽ sử dụng nút với Express.js.any JavaScript framework. For this article, we will use node with express.js.any JavaScript framework. For this article, we will use node with express.js.

→ Chúng tôi sẽ sử dụng gói Exceljs cho cùng.start the node server using the below command

npm start

→ Chúng ta cần cài đặt thiết lập Express cơ bản bằng lệnh bên dướilocalhost:3000 in the browser. The default home route opens.

→ Mở ứng dụng trong trình chỉnh sửa mã yêu thích của bạn. Cấu trúc thư mục sẽ trông giống như dưới đây:exceljs package

npm install exceljs

→ Hãy để chúng tôi khởi động máy chủ nút bằng lệnh dưới đâystart the node server using the below commandstart the node server using the below commandSTEP1: We will create a route for which download will happen. Open route/index.js file.

→ Mở Localhost: 3000 trong trình duyệt. Tuyến nhà mặc định mở ra.localhost:3000 in the browser. The default home route opens.localhost:3000 in the browser. The default home route opens.exportUser is a controller which we will create in STEP3

router.get("/downloadExcel", exportUser);

→ Sau khi dự án được thiết lập, chúng tôi sẽ cài đặt gói Exceljsexceljs packageexceljs packageSTEP2: We will create a model. This page will hold the data which we want to export. In real scenarios, we will connect with either relational or non-relational databases and get data.

→ Bước 1: Chúng tôi sẽ tạo một tuyến đường mà tải xuống sẽ xảy ra. Mở tệp/index.js.STEP1: We will create a route for which download will happen. Open route/index.js file.STEP1: We will create a route for which download will happen. Open route/index.js file.

Ở đây Exportuser là một bộ điều khiển mà chúng tôi sẽ tạo trong bước 3exportUser is a controller which we will create in STEP3exportUser is a controller which we will create in STEP3Models/User.js

// GET DATA FROM DATABASE// Sample dataconst User = [{ 
fname: "Amir",
lname: "Mustafa",
email: "",
gender: "Male"
},
{
fname: "Ashwani",
lname: "Kumar",
email: "",
gender: "Male",
},
{
fname: "Nupur",
lname: "Shah",
email: "",
gender: "Female"
},
{
fname: "Himanshu",
lname: "Mewari",
email: "",
gender: "Male",
},
{
fname: "Vankayala",
lname: "Sirisha",
email: "",
gender: "Female",
},];module.exports = User;

→ Bước 2: Chúng tôi sẽ tạo một mô hình. Trang này sẽ giữ dữ liệu mà chúng tôi muốn xuất. Trong các kịch bản thực, chúng tôi sẽ kết nối với các cơ sở dữ liệu quan hệ hoặc không quan hệ và lấy dữ liệu.STEP2: We will create a model. This page will hold the data which we want to export. In real scenarios, we will connect with either relational or non-relational databases and get data.STEP2: We will create a model. This page will hold the data which we want to export. In real scenarios, we will connect with either relational or non-relational databases and get data.STEP3: We will create a controller which will link to our route

→ Ở đây, để đơn giản, chúng tôi đã viết chỉ đầu ra của dữ liệu từ bất kỳ cơ sở dữ liệu nào.Controller/User.js

const User = require("../Models/User");const excelJS = require("exceljs");const exportUser = async (req, res) => {   // WRITE DOWNLOAD EXCEL LOGIC};module.exports = exportUser;

Tạo mô hình/user.jsModels/User.jsModels/User.jsroute, we will import this controller and configure to our route

route/index.js

var express = require("express");const exportUser = require("../Controller/User"); // CONTROLLERvar router = express.Router();
router.get("/downloadExcel", exportUser); // DOWNLOAD ROUTE
module.exports = router;

→ Bước3: Chúng tôi sẽ tạo một bộ điều khiển sẽ liên kết với tuyến đường của chúng tôiSTEP3: We will create a controller which will link to our routeSTEP3: We will create a controller which will link to our routelogic for downloading excel in the User controller:

Controller/User.js

const User = require("../Models/User"); // This has data to be used
const excelJS = require("exceljs");
const exportUser = async (req, res) => { const workbook = new excelJS.Workbook(); // Create a new workbook const worksheet = workbook.addWorksheet("My Users"); // New Worksheet const path = "./files"; // Path to download excel // Column for data in excel. key must match data key
worksheet.columns = [
{ header: "S no.", key: "s_no", width: 10 },
{ header: "First Name", key: "fname", width: 10 },
{ header: "Last Name", key: "lname", width: 10 },
{ header: "Email Id", key: "email", width: 10 },
{ header: "Gender", key: "gender", width: 10 },
];// Looping through User data
let counter = 1;
User.forEach((user) => { user.s_no = counter; worksheet.addRow(user); // Add data in worksheet counter++;});// Making first line in excel bold
worksheet.getRow(1).eachCell((cell) => {
cell.font = { bold: true };});try { const data = await workbook.xlsx.writeFile(`${path}/users.xlsx`) .then(() => { res.send({ status: "success", message: "file successfully downloaded", path: `${path}/users.xlsx`, });
});
} catch (err) { res.send({ status: "error", message: "Something went wrong", }); }};module.exports = exportUser;

Tạo bộ điều khiển/user.jsController/User.jsController/User.jscreate a workbook. Inside the workbook, we create a worksheet.

Trong tuyến đường, chúng tôi sẽ nhập bộ điều khiển này và định cấu hình vào tuyến đường của chúng tôiroute, we will import this controller and configure to our routeroute, we will import this controller and configure to our route

→ Bước5: Tại đây, chúng tôi sẽ viết logic để tải xuống Excel trong bộ điều khiển người dùng:logic for downloading excel in the User controller:logic for downloading excel in the User controller:define the columns to print. The key of every entry should match the User data that is there in the User model.

→ Ở đây chúng tôi tạo một sổ làm việc. Bên trong sổ làm việc, chúng tôi tạo ra một bảng tính.create a workbook. Inside the workbook, we create a worksheet.create a workbook. Inside the workbook, we create a worksheet.

await workbook.xlsx.writeFile(`${path}/users.xlsx`)

→ Mỗi tờ giữ một bộ dữ liệu khác nhau. Đối với bài viết này, chúng tôi chỉ có một tệp để tải xuống, tức là bảng tính người dùng

localhost:3000/downloadExcel

→ Trong bước tiếp theo, chúng tôi xác định các cột để in. Khóa của mọi mục nhập sẽ phù hợp với dữ liệu người dùng có trong mô hình người dùng.define the columns to print. The key of every entry should match the User data that is there in the User model.define the columns to print. The key of every entry should match the User data that is there in the User model.

Video:

https://secure.vidyard.com/organizations/1904214/players/Qr56rBG9FMtqB1SrETx2ut?edit=true&npsRecordControl=1

Repository:

https://github.com/AmirMustafa/export-to-excel

Bớt tư tưởng:

Chúng ta đã thấy làm thế nào đơn giản là chúng ta có thể xuất dữ liệu trong một tệp excel. Hãy thử đọc tài liệu Exceljs. Ở đó chúng tôi có thể thực hiện nhiều tùy chọn định dạng và tải xuống trong các tệp Excel.

Cảm ơn bạn đã đến cuối cùng. Nếu bạn thích bài viết này hoặc học một cái gì đó mới, hãy hỗ trợ tôi bằng cách nhấp vào nút Chia sẻ bên dưới để tiếp cận nhiều người hơn và/hoặc cho tôi theo dõi trên Twitter để xem một số mẹo, bài viết khác và những điều tôi học và chia sẻ ở đó.

Làm cách nào để sử dụng Node JS làm cơ sở dữ liệu trong Excel?

Cách nhập bản ghi tệp Excel trong cơ sở dữ liệu MySQL thông qua Node JS bằng cách sử dụng Multer....

Bước 1: Tạo thư mục dự án ..

Bước 2: Thiết lập gói JSON ..

Bước 2: Cài đặt các gói NPM ..

Bước 3: Tạo bảng MySQL ..

Bước 4: Tạo biểu mẫu tải lên bootstrap ..

Bước 5: Tạo tệp máy chủ ..

Bước 6: Tải lên Excel lên MySQL ..

Làm cách nào để xuất dữ liệu từ Node JS sang Excel?

Ứng dụng JS .....

Bắt đầu với SpreadJS và Node.js ..

Sử dụng gói npm lpcorjs ..

Tải tệp Excel vào ứng dụng Node.js của bạn ..

Thu thập đầu vào của người dùng ..

Điền vào tệp Excel của bạn ..

Xuất Node.js ra Excel ..

Làm cách nào để lưu dữ liệu JSON trong Excel?

Trên tab Dữ liệu trên mạng, từ phần GET GET & Biến đổi dữ liệu, chọn Nhận dữ liệu> từ tệp> từ JSON.Bạn sẽ thấy cửa sổ Nhập tiêu chuẩn của máy tính.Ở đây, mở thư mục nơi đặt tệp JSON của bạn.Bấm đúp vào tệp để kết nối nó với Excel.. You will see your computer's standard “Import” window. Here, open the folder where your JSON file is located. Double-click the file to connect it to Excel.. You will see your computer's standard “Import” window. Here, open the folder where your JSON file is located. Double-click the file to connect it to Excel.. You will see your computer's standard “Import” window. Here, open the folder where your JSON file is located. Double-click the file to connect it to Excel.

Làm cách nào để chuyển đổi nút thành CSV trong Excel?

Chuyển đổi XLSX hoặc XLS thành CSV trong nút .....

Tạo một đối tượng của lớp sổ làm việc để tải tệp XLSX ..

Chuyển đổi tệp XLSX thành CSV bằng Workbook.Lưu (Chuỗi, SaveFormat. CSV) Phương thức ..