لینک کوتاه : https://en.magicfile.ir/?p=2587
Download the script to extract HTML table data to Excel [.xls, .csv.] using JavaScript
In this post, you will learn how to extract HTML table data to Excel spreadsheets and CSV files using JavaScript. The purpose of this file is to provide IT/CS students and new programmers with a reference for learning how to extract data into Excel using pure JavaScript.
In some applications or web applications, management often needs to extract data from the system into spreadsheets or Excel CSV files. Mainly the purpose of this feature is to extract data that allows system administration to use it for other purposes. Extract to Excel features are usually implemented in conjunction with the program's reporting feature.
چگونه با استفاده از JS جدول HTML را به فایل اکسل صادر کنیم؟
There are many ways to export table data to a CSV or Excel spreadsheet. Some developers prefer to use plugins or free libraries to make this possible. By using JavaScript built-in methods and script short lines, we can achieve the mentioned feature for our web application as well. JavaScript comes with a Blob object, which is very useful for our purpose here. We can simply create a raw data like file from the HTML table and create a URL object to put in an anchor tag that will allow us to download the file.
JS Blob چیست؟
A bubble is an immutable, file-like object with raw data. It can be read as text or binary data or converted to a ReadableStream so that its methods can be used to process the data. The Blob object represents JavaScript bubbles.
مراحل استخراج داده های جدول به اکسل [.xls، .csv]؟
استخراج به عنوان فایل صفحه گسترده اکسل
- Get the outer HTML of the table in JS and put it in a new HTML document string
- Convert an HTML document string to a Blob object using the application/vnd.openxmlformats-officedocument.spreadsheetml.sheet content type.
- یک عنصر Anchor ایجاد کنید
- Set a download attribute to the anchor with filename as exportedData.xls value
- یک URL Object از شی Blob ایجاد کنید و آن را در ویژگی href انکر قرار دهید
- تگ Anchor را به بدنه سند صفحه اضافه کنید
- ماشه روی برچسب Anchor کلیک کنید تا فایل صادر شده را دانلود کنید
استخراج به عنوان فایل CSV
- Get the outer HTML of the table in JS and put it in a new HTML document string
- رشته سند HTML را با استفاده از نوع محتوای text/csv به یک شی Blob تبدیل کنید.
- یک عنصر Anchor ایجاد کنید
- Set a download property to the anchor with the filename as the value ie exportedData.csv
- یک URL Object از شی Blob ایجاد کنید و آن را در ویژگی href انکر قرار دهید
- تگ Anchor را به بدنه سند صفحه اضافه کنید
- ماشه روی برچسب Anchor کلیک کنید تا فایل صادر شده را دانلود کنید
برای شما کاربر عزیز پیشنهاد دانلود داده می شود.