我想將大資料從mysql資料庫匯出到PHP中的Excel檔案,可以直接下載并在客戶端下載進度中生成。所以http回應不需要等待完全查詢進度并在服務器上生成excel,因為如果時間太長會超時。
我希望在下載程序 100% 完成之前,在下載進度客戶端上生成所有進度。有沒有可能?我該怎么做這個概念?
uj5u.com熱心網友回復:
您可以將資料呼叫到瀏覽器中,然后使用一點 JavaScript 將其動態地放入 CSV 檔案中。
const data = [
["rahul", "delhi", "accounts dept"],
["rajeev", "UP", "sales dept"]
];
let csvContent = "data:text/csv;charset=utf-8,";
data.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent = row "\r\n";
});
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
所以你會拿你的 PHP Mysql 結果陣列和
json_encode($result);
然后,一旦您使用 XHR 將其發布到 Javascript - 獲取 AJAX
json.parse(result)
然后使用上面的代碼...
uj5u.com熱心網友回復:
您可以使用 tableToExcel.js 將表格匯出到 Excel 檔案中。這通過以下方式作業:
1)。將此 CDN 包含在您的專案/檔案中
<script src="https://cdn.jsdelivr.net/gh/linways/[email protected]/dist/tableToExcel.js"></script>
2)。要么使用 JavaScript:
<button id="btnExport" onclick="exportReportToExcel(this)">EXPORT REPORT</button>
function exportReportToExcel() {
let table = document.getElementsByTagName("table"); // you can use document.getElementById('tableId') as well by providing id to the table tag
TableToExcel.convert(table[0], { // html code may contain multiple tables so here we are refering to 1st table tag
name: `export.xlsx`, // fileName you could use any name
sheet: {
name: 'Sheet 1' // sheetName
}
});
}
這樣就可以直接下載excel格式了,程序很快
uj5u.com熱心網友回復:
//replace with your own function ...
var i = 0;
function move() {
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 1;
var id = setInterval(frame, 10);
function frame() {
if (width >= 100) {
clearInterval(id);
i = 0;
} else {
width ;
elem.style.width = width "%";
}
}
}
}
#myProgress {
width: 100%;
background-color: #ddd;
}
#myBar {
width: 1%;
height: 30px;
background-color: #04AA6D;
}
<h1>JavaScript Progress Bar</h1>
<div id="myProgress">
<div id="myBar"></div>
</div>
<br>
<button onclick="move()">Click Me</button>
這是您執行同步請求的方式
function produceCSV(data)
{
let csvContent = "data:text/csv;charset=utf-8,";
//unstring the data
data = JSON.parse(data);
data.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent = row "\r\n";
});
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
}
//simple Async fetch request
fetch('http://time.jsontest.com/mydatafile.php')
.then(res => res.json())
.then((data) => {
console.log(data);
produceCSV(data);
}).catch(err => console.error(err));
只需確保您的 php 檔案回傳 json_encode($data);
...而且您可能希望在該 php 檔案中放置一些標頭...例如
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: FETCH');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
header('X-Requested-With: XMLHttpRequest');
header('Accept: application/json');
include_once 'connection.php';
include_once 'models.php';
//MYSQL CALL
$result = mysqlQuery ....
// convert the array to json string
$result = json_encode($result);
//Echoing data to the screen sends it to the clients browser in a post
request ...
echo $result;
如果您需要查找有關 JavaScript 變數的任何資訊
console.log(你的JS變數)
is your friend
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/448541.html
上一篇:Can'tphpartisanserve:failedtoopenstream:Nosuchfileordirectoryin/home/online21
下一篇:描述php物件
