[英]How can I save and load the content of HTML into the file using PHP
我有一個包含表格的模態,並且有一個保存設置按鈕和一個加載設置按鈕,當我單擊保存按鈕時,表格內容應該以任何格式保存在任何文件夾中的文件中,並且通過單擊加載按鈕它應該能夠加載HTML格式的文件。
那我該怎么做呢?
下面是模態的代碼
<div class="modal fade" id="envVarModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel" style="text-align:center">Environment Variable Settings</h4>
</div>
<div class="modal-body" id="envVarModalBodyID">
<label>Env Variable:</label>
<select id="envVarID" name="Select2" onchange="addEnvVar()">
<option value="nothingSelected">Select Env Variable</option>
<option value="Count">Count</option>
<option value="Schedule">Schedule</option>
</select>
<table><tr><td>A</td><td>B</td></tr><tr><td>One</td><td>Two</td></tr><tr><td>Three</td><td>Four</td></tr></table>"
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Save Setup</button>
<button type="button" class="btn btn-primary">Load Setup</button>
</div>
</div>
</div>
</div>
使用此javascript將表格保存為.xls格式。我已將click事件添加到您的按鈕中
btn btn-default類,您必須在其中將表包含在<div id="table1">
您的表結構</div>
<script>
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="tableToExcel('table1', 'W3C Example Table')" value="Export to Excel">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.