我在php代码中有一个HTML表格,我需要通过同一页面上的按钮(超链接)将该表格的内容导出到.csv文件中。
请支持我。
<html>
<body>
<?php
$dt1 = $_POST["bday1"];
$dt2 = $_POST["bday2"];
// Create connection to Oracle
$conn = oci_connect("cse", "mahesh123", "XE");
$query = "select
trdt,SYMBOL,BID_PRICE,ASK_PRICE,OPEN_PRICE,
HIGH_PRICE,LOW_PRICE,LAST_TRADED_PRICE,CLOSE_PRICE,
to_char(TRADE_VOLUME,'999,999,999,999')TR_Volume,
to_char(SHARE_VOLUME,'999,999,999,999')Share_Vol,
to_char(TURNOVER,'999,999,999,999') Turnover,
to_char(ISSUED_QUANTITY ,'999,999,999,999') Issued_Qty
from daily_trades where trdt between TO_DATE('$dt1','YYYY-MM-
DD')and TO_DATE('$dt2','YYYY-MM-DD')
and symbol='PABC-N-0000' order by trdt";
//Date Symbol Short_Name Bid_Price Ask_Price
Open_Price High_Price Low_Price Last_Traded_Price
Close_Price Trade_Volume Share_Volume Turnover
Issued_Qty
$stid = oci_parse($conn, $query);
$r = oci_execute($stid);
// Fetch each row in an associative array
print '<table style="font-size:13px;">';
echo "<tr><th >Date</th><th>Symbol</th><th>Bid_Price</th>
<th>Ask_Price</th><th>Open_Price</th><th>High_Price</th>
<th>Low_Price</th><th>Last_Traded</th>
<th>Close_Price</th><th>Trade_Volume</th><th>Share_Volume</th>
<th>Turnover</th><th>Issued_Qty</th></tr>";
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
print '<tr>';
foreach ($row as $item) {
print '<td>'.($item !== null ? htmlentities($item, ENT_QUOTES) :
' ').'</td>';
}
print '</tr>';
}
print '</table>';
?>
</body>
</html>
我需要将数据导出到csv文件中。
答案 0 :(得分:-1)
<div class="form-group">
<button onclick="Export()" class="btn btn-primary">Export to CSV File</button>
</div>
<!-- /Content -->
<script>
function Export()
{
var conf = confirm("Export users to CSV?");
if(conf == true)
{
window.open("export.php", '_blank');
}
}
</script>
</div>
//create a file name export.php
$query = "select
trdt,SYMBOL,BID_PRICE,ASK_PRICE,OPEN_PRICE,
HIGH_PRICE,LOW_PRICE,LAST_TRADED_PRICE,CLOSE_PRICE,
to_char(TRADE_VOLUME,'999,999,999,999')TR_Volume,
to_char(SHARE_VOLUME,'999,999,999,999')Share_Vol,
to_char(TURNOVER,'999,999,999,999') Turnover,
to_char(ISSUED_QUANTITY ,'999,999,999,999') Issued_Qty
from daily_trades where trdt between TO_DATE('$dt1','YYYY-MM-
DD')and TO_DATE('$dt2','YYYY-MM-DD')
and symbol='PABC-N-0000' order by trdt";
//Date Symbol Short_Name Bid_Price Ask_Price
Open_Price High_Price Low_Price Last_Traded_Price
Close_Price Trade_Volume Share_Volume Turnover
Issued_Qty
$stid = oci_parse($conn, $query);
$r = oci_execute($stid);
$users = array();
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
$users[] = $row;
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Users.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('No', 'First Name', 'Last Name', 'Email'));
if (count($users) > 0) {
foreach ($users as $row) {
fputcsv($output, $row);
}
}