我有这种形式的数据:
526,8696,Peach,Del Monte,Breakout Damage,TT Damage,Agricom,Mango,Damage to contents of cartons,4,2,P,2014-12-08,748,Atlanta 404,2014-12-08,Amir
以下是创建CSV的代码:
$receiveQuery = $objadminViewFunctions->exportFile();
$fp = fopen('data.csv', 'w');
$column = 'Serial No.,Report Number,Pallet Id,Type,Consignee,Damage,Damage Description,Label,Vessel,Location,Suryeyor';
fputcsv($fp, split(',',$column));
while($rows = $receiveQuery->fetch_assoc())
{
$data = $rows['report_number'].','.$rows['pallet_ID'].','.$rows['type'].','.$rows['consignee'].','.$rows['damage'].','.$rows['damage_desc'].','.$rows['label'].','.$rows['variety'].','.$rows['category_code'].','.$rows['pieces'].','.$rows['hold'].','.$rows['deck'].','.$rows['dDate'].','.$rows['vessel'].','.$rows['location'].','.$rows['dDate'].','.$rows['suryeyor'];
fputcsv($fp, split(',',$data));
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data.csv");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
问题是它正在创建CSV文件但是使用我的页面的HTML而不是我想从该文件导出的数据。
以下是我按下导出按钮时在CSV文件中的内容。
<!DOCTYPE html>
<html class="no-js">
<head>
<title>Reports</title>
<link href="../assets/styles/admin/vendors/bootstrap-wysihtml5/src/bootstrap-wysihtml5.css" rel="stylesheet" media="screen">
<link href="../assets/styles/admin/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="../assets/styles/admin/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<link href="../assets/styles/admin/vendors/easypiechart/jquery.easy-pie-chart.css" rel="stylesheet" media="screen">
<link href="../assets/styles/admin/vendors/datepicker.css" rel="stylesheet" media="screen">
<link href="../assets/styles/admin/assets/styles.css" rel="stylesheet" media="screen">
<link rel="stylesheet" href="../assets/styles/admin/assets/validationEngine.jquery.css" type="text/css">
<!-- HTML5 shim for IE6-8 support of HTML5 elements -->
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
<!-- HTML5 shim for IE6-8 support of HTML5 elements -->
答案 0 :(得分:0)
这是我工作的CSV导出功能的一部分:
public function exportAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=products.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('Product', 'Name', 'Price', 'Description'));
// fetch the data
$product_info = $this->product->getAll();
foreach ($product_info as $key => $product) {
$row['product'] = $product->product_id;
$row['name'] = $product->name;
$row['price'] = $product->price;
$row['description'] = $product->description;
fputcsv($output, $row);
}
}
请务必先输出标题,以免加载HTML。
答案 1 :(得分:0)
您没有让用户实际下载您制作的csv文件,只是将当前输出的内容类型更改为Content-type: text/csv
。您创建了data.csv
,但未将其发送给用户。
这可能有用:
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=data.csv");
header("Pragma: no-cache");
header('Expires: 0');
header('Content-Length: ' . filesize('data.csv'));
readfile('data.csv'); // This actually out puts your csv file
exit; // This is importent that stops the current script and prevents the out put of your html part