Hướng dẫn excel4node read-excel file - excel4node đọc tệp excel
Useful link Show
https://ciphertrick.com/read-excel-files-convert-json-node-js/ 1.8.0 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản 4 tháng trướcPublic • Published 4 months ago
Excel4NodeMột thư viện tạo tệp XLSX đầy đủ tính năng cho phép tạo các tệp Excel nâng cao. Excel4Node phù hợp với đặc tả ECMA-376 OOXML Phiên bản thứ 2 TÀI LIỆU THAM KHẢO OpenXML Giấy trắng ECMA-376 Trang web OpenOffice excel Định dạng tệp tham chiếu OpenOffice Anatomy of OoxML Giải thích MS-XSLX Spec (PDF) Mã tài liệu tham khảo Thông số kỹ thuật Phần từ ECMA-376 Phiên bản thứ 2 DOC ECMA-376, Phiên bản thứ hai, Phần 1-Nguyên tắc cơ bản và tham chiếu ngôn ngữ đánh dấu.pdf được tìm thấy trong ECMA-376 Phiên bản thứ 2 Phần 1 Tải xuống tại http://www.ecma-International.org /publications/standards/ecma-376.htm Cách sử dụng cơ bản// Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx'); Excel4NodeExcel4Node đi kèm với một số chức năng và loại chung xl.getexcelrowcol (cellref) chấp nhận tham chiếu ô (tức là 'a1') và trả về đối tượng với hàng và cột tương ứng xl.getExcelRowCol('B5'); // returns { row: 5, col: 2} xl.getexcelalpha (cột) chấp nhận cột là số nguyên và trả về tham chiếu cột tương ứng là alpha xl.getExcelAlpha(10); // returns 'J' xl.getexcelcellref (hàng, cột) chấp nhận hàng và cột làm số nguyên và trả về tham chiếu ô Excel xl.getExcelCellRef(5, 3); // returns 'C5' xl.getexcelts (ngày) chấp nhận đối tượng ngày và trả về dấu thời gian excel var newDate = new Date('2015-01-01T00:00:00.0000Z'); xl.getExcelTS(newDate); // Returns 42004.791666666664 xl.PAPER_SIZE Sách bài tậpMột thể hiện của lớp sổ làm việc chứa tất cả dữ liệu và tham số cho sổ làm việc Excel. Người xây dựngTrình xây dựng sổ làm việc chấp nhận một đối tượng cấu hình tùy chọn. var xl = require('excel4node'); var wb = new xl.Workbook({ jszip: { compression: 'DEFLATE', }, defaultFont: { size: 12, name: 'Calibri', color: 'FFFFFFFF', }, dateFormat: 'm/d/yy hh:mm:ss', workbookView: { activeTab: 1, // Specifies an unsignedInt that contains the index to the active sheet in this book view. autoFilterDateGrouping: true, // Specifies a boolean value that indicates whether to group dates when presenting the user with filtering options in the user interface. firstSheet: 1, // Specifies the index to the first sheet in this book view. minimized: false, // Specifies a boolean value that indicates whether the workbook window is minimized. showHorizontalScroll: true, // Specifies a boolean value that indicates whether to display the horizontal scroll bar in the user interface. showSheetTabs: true, // Specifies a boolean value that indicates whether to display the sheet tabs in the user interface. showVerticalScroll: true, // Specifies a boolean value that indicates whether to display the vertical scroll bar. tabRatio: 600, // Specifies ratio between the workbook tabs bar and the horizontal scroll bar. visibility: 'visible', // Specifies visible state of the workbook window. ('hidden', 'veryHidden', 'visible') (§18.18.89) windowHeight: 17620, // Specifies the height of the workbook window. The unit of measurement for this value is twips. windowWidth: 28800, // Specifies the width of the workbook window. The unit of measurement for this value is twips.. xWindow: 0, // Specifies the X coordinate for the upper left corner of the workbook window. The unit of measurement for this value is twips. yWindow: 440, // Specifies the Y coordinate for the upper left corner of the workbook window. The unit of measurement for this value is twips. }, logLevel: 0, // 0 - 5. 0 suppresses all logs, 1 shows errors only, 5 is for debugging author: 'Microsoft Office User', // Name for use in features such as comments }); Phương phápwb.addworksheet (tên, tùy chọn); Thêm một bảng tính mới vào sổ làm việc chấp nhận tên của bảng tính mới và đối tượng tùy chọn (xem phần bảng tính) Trả về một phiên bản bảng tính wb.setselectedtab (id); Các bộ tab nào sẽ được chọn khi sổ làm việc được mở ADE WB.CreateStyle (OPTS); Tạo một thể hiện kiểu mới chấp nhận đối tượng cấu hình kiểu (xem phần kiểu) Trả về một thể hiện kiểu mới wb.write (); Phương thức write () có thể chấp nhận một tên tệp, tên tệp có hàm gọi lại hoặc đối tượng phản hồi HTTP. var xl = require('excel4node'); var wb = new xl.Workbook(); wb.write('ExcelFile.xlsx'); // Writes the file ExcelFile.xlsx to the process.cwd(); wb.write('ExcelFile.xlsx', function(err, stats) { if (err) { console.error(err); } else { console.log(stats); // Prints out an instance of a node.js fs.Stats object } }); // sends Excel file to web client requesting the / route // server will respond with 500 error if excel workbook cannot be generated var express = require('express'); var app = express(); app.get('/', function(req, res) { wb.write('ExcelFile.xlsx', res); }); app.listen(3000, function() { console.log('Example app listening on port 3000!'); }); wb.writetobuffer (); Phương thức WritetObuffer () không có tham số và trả về một lời hứa giải quyết với NodeBuffer được tạo bởi thư viện JSZIP. Bộ đệm này sau đó có thể được gửi đến các luồng khác. // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');0 Bảng tínhMột thể hiện của lớp bảng tính chứa tất cả thông tin cụ thể cho bảng tính đó Người xây dựngTrình xây dựng sổ làm việc chấp nhận một đối tượng cấu hình tùy chọn. // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');1 Phương pháp // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');2 wb.addworksheet (tên, tùy chọn); Thêm một bảng tính mới vào sổ làm việc chấp nhận tên của bảng tính mới và đối tượng tùy chọn (xem phần bảng tính) Trả về một phiên bản bảng tính headerFooter strings accept Dynamic Formatting Strings. i.e. '&L&A&C&BCompany, Inc. Confidential&B&RPage &P of &N' Phương phápwb.addworksheet (tên, tùy chọn); Thêm một bảng tính mới vào sổ làm việc chấp nhận tên của bảng tính mới và đối tượng tùy chọn (xem phần bảng tính) Trả về một phiên bản bảng tínhwb.setselectedtab (id); Các bộ tab nào sẽ được chọn khi sổ làm việc được mở ADE WB.CreateStyle (OPTS); Tạo một thể hiện kiểu mới chấp nhận đối tượng cấu hình kiểu (xem phần kiểu) Trả về một thể hiện kiểu mới // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');4 wb.write (); Phương thức write () có thể chấp nhận một tên tệp, tên tệp có hàm gọi lại hoặc đối tượng phản hồi HTTP.wb.writetobuffer (); Phương thức WritetObuffer () không có tham số và trả về một lời hứa giải quyết với NodeBuffer được tạo bởi thư viện JSZIP. Bộ đệm này sau đó có thể được gửi đến các luồng khác. // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');5 Bảng tính Một thể hiện của lớp bảng tính chứa tất cả thông tin cụ thể cho bảng tính đóTrình xây dựng bảng tính được gọi qua lớp sổ làm việc và chấp nhận đối tượng tên và cấu hình Tùy chọn bảng tính đầy đủ. Tất cả các tùy chọn là tùy chọn. // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');6 Lưu ý: Chuỗi tiêu đề Chấp nhận chuỗi định dạng động. tức là '& l & a & c & bcompany, Inc.Xác nhận dữ liệu bảng tính ws.adddatavalidation (); Chấp nhận một đối tượng Tùy chọn xác thực với các tùy chọn có sẵn này. Tất cả các tùy chọn là tùy chọn ngoại trừ SQREF. // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');7 // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');3Bảng tính định dạng có điều kiện // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');8 Định dạng có điều kiện thêm các định dạng tùy chỉnh để đáp ứng với trạng thái tham chiếu ô. Một tập hợp con của các tính năng định dạng có điều kiện hiện được hỗ trợ bởi Excel4Node. Quy tắc định dạng áp dụng ở cấp bảng tính. Ví dụ sau đây sẽ làm nổi bật tất cả các ô giữa A1 và A10 có chứa chuỗi "OK" với văn bản màu xanh lá cây, màu xanh lá cây: // Require library var xl = require('excel4node'); // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Worksheets to the workbook var ws = wb.addWorksheet('Sheet 1'); var ws2 = wb.addWorksheet('Sheet 2'); // Create a reusable style var style = wb.createStyle({ font: { color: '#FF0800', size: 12, }, numberFormat: '$#,##0.00; ($#,##0.00); -', }); // Set value of cell A1 to 100 as a number type styled with paramaters of style ws.cell(1, 1) .number(100) .style(style); // Set value of cell B1 to 200 as a number type styled with paramaters of style ws.cell(1, 2) .number(200) .style(style); // Set value of cell C1 to a formula styled with paramaters of style ws.cell(1, 3) .formula('A1 + B1') .style(style); // Set value of cell A2 to 'string' styled with paramaters of style ws.cell(2, 1) .string('string') .style(style); // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. ws.cell(3, 1) .bool(true) .style(style) .style({font: {size: 14}}); wb.write('Excel.xlsx');9 Loại định dạng có điều kiện duy nhất hiện được hỗ trợ là biểu thức. Khi công thức trả về 0, định dạng có điều kiện không được hiển thị. Khi công thức trả về một giá trị khác, định dạng có điều kiện được hiển thị. xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}0 Trang web bị phá vỡ trang xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}1 Có thể thêm các lần ngắt trang trang tính ở hàng và cột xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}2 xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}9 trong đó loại là xl.getExcelAlpha(10); // returns 'J'0 hoặc xl.getExcelAlpha(10); // returns 'J'1 và vị trí là hàng/cột cuối cùng trước khi ngắt trang. Khu vực in bảng tínhCác khu vực in bảng tính có thể được đặt xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}3 Phong cáchCác đối tượng kiểu có thể được áp dụng cho các ô bất kỳ kết hợp các giá trị kiểu nào cũng có thể được đặt tạo ra kiểu đặt trước hiệu quả hơn nhiều so với việc áp dụng các kiểu xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}4 Bình luậnNhận xét có thể được thêm vào các ô với một số tùy chọn xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}5 Hình ảnhThêm và hình ảnh vào bảng tính.Hiện tại loại 'Hình ảnh' được hỗ trợ định vị có 3 loại, 'Tuyệt đối'Góc bên trái của bảng tính OneCellanchor và Twocellanchor sẽ lấy các đối tượng vị trí: xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}6 Loại vị trí của OneCellanchor sẽ lấy một "vị trí" vị trí của loại twocellanchor sẽ lấy một vị trí "từ" và "đến" chỉ định một twocellanchor sẽ tự động điều chỉnh hình ảnh để phù hợp với giới hạn của hai mỏ neo. xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}7 Bạn cũng có thể chuyển bộ đệm với hình ảnh được tải vào bộ nhớ: xl.getExcelRowCol('B5'); // returns { row: 5, col: 2}8 |