如何使用网格将 HTML 表格导出到 Excel
Posted
技术标签:
【中文标题】如何使用网格将 HTML 表格导出到 Excel【英文标题】:How can I export a HTML table to Excel with a grid 【发布时间】:2017-07-17 06:45:08 【问题描述】:我在将表格/数据导出到 Excel 或 PDF 方面非常陌生。但是到目前为止,我已经编写了用于将 html 表导出到 Excel (xls) 的 jQuery。问题是它没有显示excel的网格。这是 Excel 中的输出示例。
这是我要导出的代码:
$(document).ready(function()
$("#btnExport").click(function(e)
e.preventDefault();
//getting data from our table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('table_id');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
var a = document.createElement('a');
a.href = data_type + ', ' + table_html;
a.download = 'exported_salmon_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
a.click();
);
);
这是我的 HTML
<table id="table_id" class="data-table">
<caption class="title">Sales Salmon Usage Per 1K</caption>
<thead>
<tr>
<th><center>NO</center></th>
<th><center>OUTLET</center></th>
<th><center>DATE</center></th>
<th><center>SALES</center></th>
<th><center>USAGE</center></th>
<th><center>USAGE PER 1K</center></th>
</tr>
</thead>
<tbody>
<?php
$storenum=$_POST['search'];
$store_name=$_POST['storename'];
$store_region=$_POST['region'];
$date1 = $_POST['date1'];
$date11 = strtotime($date1);
$date111 = date('Y-m',$date11);
$date112 = date('d',$date11);
//date2
$date2 = $_POST['date2'];
$date21 = strtotime($date2);
$date211 = date('Y-m',$date21);
$date2111= date('F Y',$date21);
$date212 = date('d',$date21);
$t = date('t',$date21);
$x=(($date212-$date112)+1)/$t;
//input date1 and date2
if ((!empty($date1)) && (!empty($date2)) && (empty($storenum)) &&
(empty($store_region)))
//my$query = "";
//input storenum, date1,date2
else if ((!empty($storenum)) && (!empty($date1)) && (!empty($date2)) &&
(empty($store_region)))
//my$query = "";
//input region, date1,date2
else if ((!empty($store_region)) && (!empty($date1)) && (!empty($date2)) &&
(empty($storenum)))
//my$query = "";
// Execute the query, or else return the error message.
$result = $dbhandle->query($query) or exit("Error code ($dbhandle->errno): $dbhandle->error");
$no = 1;
while ($row = mysqli_fetch_array($result))
//someofmycode...
echo "<tr class='y_n';>";
echo "<td><center>".$no."</center></td>";
echo "<td>".$store_name."</td>";
echo "<td>".$date2111."</td>";
echo "<td>".$netamt11."</td>";
echo "<td>".$usage11."</td>";
echo "<td>".$duh1."</td>";
echo "</tr>";
$no++;
?>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th><?=($total)?></th>
</tr>
<tr>
<th colspan="5">AVERAGE <?php echo $actionstatus2 ?></th>
<?php
//someofmycodecondition...
?>
<th><?php echo number_format($X,4)?></th></tr>
<tr>
<th colspan="6"><center><?php echo $actionstatus?></center></th>
</tr>
</tfoot>
</table>
</h4></center>
<div class="box">
<center><input id="btnExport" type="submit" class="button" value=" EXPORT "></center>
</div>
</body>
【问题讨论】:
【参考方案1】: $objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(6);
$objPHPExcel->getActiveSheet()->setTitle('Test')->setCellValue('A1', 'Chapter')
->setCellValue('B1', 'Topic')
->setCellValue('C1', 'Test')
->setCellValue('D1', 'Total Questions')
->setCellValue('E1', 'Time');
$o=2;
foreach($test as $testval)
$connection = Yii::$app->db;
$chapname2 = "SELECT name as 'chapter' from chapter Where id = '".$testval['chapter_id']."' ORDER BY name ASC";
$command = $connection->createCommand($chapname2);
$value = $command->queryOne();
$objPHPExcel->setActiveSheetIndex(6)
->setCellValue('A'.$o, $value['chapter'])
->setCellValue('B'.$o, $testval['name'])
->setCellValue('C'.$o, $testval['test'])
->setCellValue('D'.$o, $testval['total_questions'])
->setCellValue('E'.$o, $testval['test_time']);
$topicID[] = $testval['id'];
$o++;
【讨论】:
以上是关于如何使用网格将 HTML 表格导出到 Excel的主要内容,如果未能解决你的问题,请参考以下文章
如何将 HTML 表格导出到 Chrome 和 IE 支持的 Excel?
将 HTML 表格导出到 Excel - 使用 jQuery 或 Java