Đọc file excel bằng javascript

In this article I will explain with an example, how to read and parse Excel file (XLS and XLSX) using JavaScript.

Once File is selected in FileUpload control, it is read as Binary data and then the Binary data is read using the xlsx Excel plugin.

The read data from Excel file is displayed in HTML Table using JavaScript.

 

 

HTML Markup

The HTML Markup consists of a FileUpload control (HTML File Input) and a HTML Button.

<input type="file" id="fileUpload" />

<input type="button" id="upload" value="Upload" onclick="Upload()" />

<hr />

<div id="dvExcel">div>

 

 

Reading (Parsing) Excel File (XLS and XLSX) and displaying data in HTML Table using JavaScript

When the Excel file is selected in FileUpload control (HTML File Input) and Upload button is clicked, the Upload JavaScript function is being called.

Inside the function, first a check is performed to verify whether the file is a valid Excel file i.e. file with extension XLS or XLSX. Then a check is performed to make sure whether the browser supports HTML5 FileReader API.

The Excel file is read as Binary data using HTML5 FileReader and then the Binary data is read using the xlsx Excel plugin which returns rows from Excel in JSON Array format.

Trong bài viết này, tôi sẽ giải thích với một ví dụ, cách tải xuống tệp Excel (XLS và XLSX) bằng JavaScript.

Nội dung chính Show

  • Làm cách nào để tải xuống một tệp xlsx?
  • Làm cách nào để tải xuống một tệp javascript?
  • JavaScript có thể tạo một tệp excel không?
  • Làm cách nào để tải xuống một tệp excel bằng React JS?

Dữ liệu bảng HTML sẽ được chuyển đổi và tải xuống thành tệp Excel với sự trợ giúp của plugin JQuery Table2Excel.

Đánh dấu HTML

Đánh dấu HTML sau đây bao gồm một bảng HTML và một nút.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

& nbsp; & nbsp; & nbsp;<title>title>

& nbsp; & nbsp; & nbsp; & nbsp;<style type="text/css">

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hìnhbody

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;font-family: Arial;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;font-size: 10pt;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; }

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; bàntable

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;border: 1px solid #ccc;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;border-collapse: collapse;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; }

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; bàntable th

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;background-color: #F7F7F7;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;color: #333;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; }font-weight: bold;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; }

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; bàntable th, table td

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;padding: 5px;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;border: 1px solid #ccc;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; }

& nbsp; & nbsp; & nbsp;style>

head>

<body>

& nbsp; & nbsp; & nbsp; & nbsp;<table id="tblCustomers" cellspacing="0" cellpadding="0">

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hình<tr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {<th>Customer Idth>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;<th>Nameth>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;<th>Countryth>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hìnhtr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hình<tr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {<td>1td>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;<td>John Hammondtd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;<td>United Statestd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hìnhtr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hình<tr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {<td>2td>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;<td>Mudassar Khantd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;<td>Indiatd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hìnhtr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hình<tr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {<td>3td>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;<td>Suzanne Mathewstd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;<td>Francetd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hìnhtr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hình<tr>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {<td>4td>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;<td>Robert Schidnertd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; kích thước phông chữ: 10pt;<td>Russiatd>

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hìnhtr>

& nbsp; & nbsp; & nbsp;table>

& nbsp; & nbsp; & nbsp;<br />

& nbsp; & nbsp; & nbsp;<input type="button" id="btnExport" value="Export" onclick="Export()" />

& nbsp; & nbsp; & nbsp;<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">script>

& nbsp; & nbsp; & nbsp;<script src="table2excel.js" type="text/javascript">script>

& nbsp; & nbsp; & nbsp;<script type="text/javascript">

& nbsp; & nbsp; & nbsp; & nbsp;function Export() {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; thân hình"#tblCustomers").table2excel({

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; {"Table.xls"

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-gia đình: Arial;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; }

& nbsp; & nbsp; & nbsp;script>

body>

html>

Explanation:

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; bàn

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Biên giới: 1pxsolid#CCC;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Thu hẹp biên giới: sụp đổ;: You will get a Warning message from Microsoft Excel application when you try to open the generated Excel file. This Warning is shown because the generated file is not a valid Excel format as the plugin simply exports the HTML content to an Excel file.

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; TABLETH

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; màu nền: #f7f7f7;

Đọc file excel bằng javascript

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Màu sắc: #333;

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Phông chữ-Trọng lượng: đậm;

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Tableth, Tabletd

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Đệm: 5px;

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

 

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

 

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

 

Hướng dẫn download excel file using javascript - tải xuống tệp excel bằng javascript

* Tất cả các logo trình duyệt được hiển thị ở trên là thuộc tính của chủ sở hữu tương ứng của họ.

Thử nghiệm

Tải xuống

Tải xuống API Word/PDF/Excel miễn phí

Bình luận

Không có ý kiến ​​đã được thêm vào bài viết này.

Làm cách nào để tải xuống một tệp xlsx?

Để thực hiện điều này: Chọn Tệp> Lưu dưới dạng> Tải xuống một bản sao. Nếu Excel yêu cầu mở hay lưu sổ làm việc, hãy chọn Lưu.Select File > Save As > Download a Copy. If Excel asks whether to open or save the workbook, select Save.

Làm cách nào để tải xuống một tệp javascript?

Vì vậy, các bước để tải xuống tệp sẽ là:..

Sử dụng API Fetch để tải xuống tệp tập lệnh ..

Chuyển đổi dữ liệu thành loại BLOB ..

Chuyển đổi đối tượng Blob thành một chuỗi bằng cách sử dụng URL.createdObjectUrl () ..

Tạo một phần tử để tải xuống chuỗi ..

JavaScript có thể tạo một tệp excel không?

SheetJS có thể được sử dụng trong trang web của bạn hoặc ứng dụng dựa trên JavaScript để tạo và điều khiển các sổ làm việc Excel.Thao tác các tờ Excel cũng có thể được thực hiện bằng các ngôn ngữ khác, nhưng nếu bạn có xu hướng JavaScript hoặc cố gắng thêm tính năng xuất vào trang web của bạn, SheetJS là cách để đi.Có 2 phiên bản của SheetJS.