将 PHP 创建的 html table 导出到 Excel
Exporting html table created by PHP into Excel
我有一个 html table 由 PHP 使用 mysql 创建,我使用 "table to excel" JQUERY 方法导出它但是我 运行 遇到的问题是从 Mysql 打印到 table 的变量没有导出到 excel 我的意思是当我将 table 导出到 Excel 我只能看到 headers 或列名以及所有 html 变量而不是 php 变量
JQUERY 脚本:
<script type="text/javascript">
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>
html table 创建者 php :
<?php
$tbl_name="form_0"; // Table name
$query = mysqli_query($con,"SELECT * FROM `{$tbl_name}` WHERE `uni_id`='{$_SESSION["uni_id"]}' ");
$count=mysqli_num_rows($query);
?>
<table class='styled-table' id="testTable" cellspacing='0' border='1' rules="groups">
<thead>
<tr>
<th scope='col' style='font-size:13px;'>sample 1</th>
<th scope='col' style='font-size:13px;'>sample 2</th>
<th scope='col' style='font-size:13px;'>sample 3</th>
<th scope='col' style='font-size:13px;'>sample 4</th>
<th scope='col' style='font-size:13px;'>sample 5 گواهی</th>
</tr>
</thead>
<?php while($row = mysqli_fetch_assoc($query)){ ?>
<tbody>
<tr>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='morabi' id='morabi' value= "<?php echo $row['morabi']; ?>" ></td>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='ostadyar' id='ostadyar' value= "<?php echo $row['ostadyar']; ?>" ></td>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='daneshyar' id='daneshyar' value= "<?php echo $row['daneshyar']; ?>" ></td>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='ostad' id='ostad' value= "<?php echo $row['ostad']; ?>" ></td>
<td align='center'><a class='styled-input' style=' padding: 7px; margin-right:2px;margin-left:2px;color:#008AB8;' href="<?php echo '../../../../uploads/form_0/'.$row['govahi']; ?>" target="_blank")">view</a></td>
</tr>
</tbody>
<?php } ?>
</table>
<div class='cleaner h20'></div>
<input class='styled-input_2' style="width:105px;" type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="export to excel" />
<div class='cleaner h20'></div>
尝试从您的 table 中删除输入标签并像下面那样直接回显您的 php 变量。
<td class='styled-input' style='width: 100px;'><?php echo $row['morabi']; ?></td>
我有一个 html table 由 PHP 使用 mysql 创建,我使用 "table to excel" JQUERY 方法导出它但是我 运行 遇到的问题是从 Mysql 打印到 table 的变量没有导出到 excel 我的意思是当我将 table 导出到 Excel 我只能看到 headers 或列名以及所有 html 变量而不是 php 变量
JQUERY 脚本:
<script type="text/javascript">
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>
html table 创建者 php :
<?php
$tbl_name="form_0"; // Table name
$query = mysqli_query($con,"SELECT * FROM `{$tbl_name}` WHERE `uni_id`='{$_SESSION["uni_id"]}' ");
$count=mysqli_num_rows($query);
?>
<table class='styled-table' id="testTable" cellspacing='0' border='1' rules="groups">
<thead>
<tr>
<th scope='col' style='font-size:13px;'>sample 1</th>
<th scope='col' style='font-size:13px;'>sample 2</th>
<th scope='col' style='font-size:13px;'>sample 3</th>
<th scope='col' style='font-size:13px;'>sample 4</th>
<th scope='col' style='font-size:13px;'>sample 5 گواهی</th>
</tr>
</thead>
<?php while($row = mysqli_fetch_assoc($query)){ ?>
<tbody>
<tr>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='morabi' id='morabi' value= "<?php echo $row['morabi']; ?>" ></td>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='ostadyar' id='ostadyar' value= "<?php echo $row['ostadyar']; ?>" ></td>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='daneshyar' id='daneshyar' value= "<?php echo $row['daneshyar']; ?>" ></td>
<td align='center'><input class='styled-input' style=' padding: 5px; width: 100px;margin-right:2px;margin-left:2px;' type='text' name='ostad' id='ostad' value= "<?php echo $row['ostad']; ?>" ></td>
<td align='center'><a class='styled-input' style=' padding: 7px; margin-right:2px;margin-left:2px;color:#008AB8;' href="<?php echo '../../../../uploads/form_0/'.$row['govahi']; ?>" target="_blank")">view</a></td>
</tr>
</tbody>
<?php } ?>
</table>
<div class='cleaner h20'></div>
<input class='styled-input_2' style="width:105px;" type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="export to excel" />
<div class='cleaner h20'></div>
尝试从您的 table 中删除输入标签并像下面那样直接回显您的 php 变量。
<td class='styled-input' style='width: 100px;'><?php echo $row['morabi']; ?></td>