我有一个创建"工作票"的Web应用程序。我的客户已请求报告页面使他们能够将报告下载为csv文件。我有这个功能。基本上,它是在创建报告表时动态创建CSV。如果表中没有大量数据,则可以下载csv。我遇到的是,当表格太大时,我达到了可以在网址中传递的字符数限制。我怎么能绕过这个?这让我发疯了。下面是我的php和html代码的示例。
用于创建CSV下载的PHP代码
<?php
$out = '';
$filename_prefix = 'UnInvoiced';
if (isset($_REQUEST['csv_hdr'])) {
$out .= $_REQUEST['csv_hdr'];
$out .= "\n";
}
if (isset($_REQUEST['csv_output'])) {
$out .= $_REQUEST['csv_output'];
}
$filename = $filename_prefix."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-Encoding: UTF-8");
header("Content-type: text/csv; charset=UTF-8");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $out;
exit;
?>
PHP代码在HTML页面上创建初始表。
echo "<div class='panel panel-primary'>
<div class='panel-heading'>
<h3 class='panel-title'>Un-Invoiced Tickets</h3>
</div>
<table class='table'>
<thead>
<tr class='filters'>
<th>Date</th>
<th>Ticket #</th>
<th>Client</th>
<th>Lease</th>
<th>Ticket Total</th>
<th>Ordered By</th>
</tr>
</thead>
<tbody>";
$conn = sqlsrv_connect($DBSERV, $connection);
if (!$conn) {
die('Could not connect: ' . sqlsrv_error($conn));
}
if ($client === 'All'){
$sql="SELECT REPLACE(Client,',',' ')
AS Client,WorkTicketNum,Lease,Date,OrderedBy,WTTotal FROM WorkTicket WHERE Date BETWEEN '".$begin."' AND '".$end."' AND Invoiced IS NULL ORDER BY Client ";
}else{
$sql="SELECT REPLACE(Client,',',' ') AS Client,WorkTicketNum,Lease,Date,OrderedBy,WTTotal FROM WorkTicket WHERE Date BETWEEN '".$begin."' AND '".$end."' AND Invoiced IS NULL AND Client = '".$client."' ORDER BY Client ";
}
$result =sqlsrv_query($conn, $sql);
while($row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)) { ?>
<tr>
<td> <?php echo $row['Date'];
$csv_output .= $row['Date'].", "; ?> </td>
<td> <?php echo $row['WorkTicketNum'];
$csv_output .= $row['WorkTicketNum'].", "; ?> </td>
<td> <?php echo $row['Client'];
$csv_output .= $row['Client'].", "; ?> </td>
<td> <?php echo $row['Lease'];
$csv_output .= $row['Lease'].", "; ?> </td>
<td> <?php echo $row['WTTotal'];
$csv_output .= $row['WTTotal'].", "; ?> </td>
<td> <?= $row['OrderedBy'];
$csv_output .= $row['OrderedBy']."\n "; ?> </td>
</tr>
<?php
}
echo " </tbody>
</table>
</div>
<br />
<a class='btn btn-default' href='/processes/uninvoicedexport.php? csv_hdr=".urlencode($csv_hdr)."&csv_output=".urlencode($csv_output)."'>Export to CSV</a>";
/*<form name='export' action='/processes/unsignedexport.php' method='post' target='_BLANK'>
<input type='submit' value='Export table to CSV'>
<input type='hidden' value=".$csv_hdr." name='csv_hdr'>
<input type='hidden' value=".$csv_output." name='csv_output'>
</form>
<div id='ticketHint'><b></b></div>
</form>";*/
sqlsrv_close($conn);
?>
有人建议我将csv写入SQL数据库,然后在按下下载按钮时检索,但我不能为我的生活找出如何做到这一点。
答案 0 :(得分:4)
你可以(也可能)使用mysql输出:
SELECT * FROM tableName INTO OUTFILE 'filename.csv'
这将为您生成文件,而无需通过您的网址传递数据。当然,您可以选择要导出的任何字段。
答案 1 :(得分:-1)
如果您的mysql用户没有权限选择outfile并且喜欢上述评论,请不要将所有数据传递给网址。
尝试这样的事情(没有经过测试,但应该给你一个想法):
<?php
if ($_REQUEST["myaction"]=="getcsvstuff") {
// handle the conditions you passed into $_POST or $_REQUEST
// set your headers, do your query and output the csv results
} else {
// print your form to pass what you would like
// you may have visual results of your query here in your table etc
// just like your example
?>
<form action="<?php $_SERVER["PHP_SELF"];?>" method="POST">
<input type="hidden" name="myaction" value="getcsvstuff" />
<input type="submit" value="go to csv results" />
... whatever else you want to pass to the above condition...
</form>
<?php
}
?>