在CSV导出期间出现问题(通过简单的.html按钮)。 标头之前的第一行为空。这会导致在excel导入过程中出现一些问题(如果我不手动删除第一行空白,则不会导入该文件的...最后一行)。
所以我试图删除第一行空白,但是我真的不知道如何在导出过程中做到这一点。
请参见下面的php代码,将数据导出到.csv(正在运行):
<?php
// Query
$sql = "SELECT * FROM [cm_blueprint] WITH(NOLOCK) ORDER BY cm_blueprint_name";
$msdb = new DB();
$result = $msdb->runQuery($sql);
date_default_timezone_set("Europe/Zurich");
// Array
$data = array();
While($row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)) {
$data[] = array(
"cm_blueprint_id" => utf8_encode($row['cm_blueprint_id']),
"cm_blueprint_name" => utf8_encode($row['cm_blueprint_name']),
"cm_blueprint_type" => utf8_encode($row['cm_blueprint_type']),
"cm_blueprint_vradatecreated" => utf8_encode($row['cm_blueprint_vradatecreated']->format('Y-m-d G:i:s')),
"cm_blueprint_vralastupdated" => utf8_encode($row['cm_blueprint_vralastupdated']->format('Y-m-d G:i:s')),
"cm_blueprint_description" => utf8_encode($row['cm_blueprint_description']),
"cm_blueprint_service" => utf8_encode($row['cm_blueprint_service']),
"cm_blueprint_status" => utf8_encode($row['cm_blueprint_status']),
"cm_blueprint_version" => utf8_encode($row['cm_blueprint_version']),
"cm_blueprint_requestable" => utf8_encode($row['cm_blueprint_requestable']),
"cm_blueprint_tenant" => utf8_encode($row['cm_blueprint_tenant']),
"cm_blueprint_output" => utf8_encode($row['cm_blueprint_output']),
"cm_blueprint_quota" => utf8_encode($row['cm_blueprint_quota']),
"cm_blueprint_createdtime" => utf8_encode($row['cm_blueprint_createdtime']->format('Y-m-d G:i:s')),
"cm_blueprint_lastupdate " => utf8_encode($row['cm_blueprint_lastupdate']->format('Y-m-d G:i:s'))
);
}
// Export Part
$file = "cm_blueprint.csv";
// Headers
$headers = array();
foreach( sqlsrv_field_metadata($result) as $fieldMetadata){
$headers[] = $fieldMetadata['Name'];
}
// File to export
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Pragma: no-cache');
header('Expires: 0');
$fp = fopen('php://output', 'w');
fputcsv($fp, $headers);
// Array export
foreach ($data as $fields) {fputcsv($fp, $fields);}
break;
?>
.csv如下所示:
我已经检查了php代码中的空白行/空格,什么都没有。 如果您有任何建议...
感谢您的帮助。
编辑30.12.2019 var_dump($ headers);
array(15) { [0]=> string(21) "cm_blueprint_id" [1]=> string(23) "cm_blueprint_name" [2]=> string(23) "cm_blueprint_type" [3]=> string(33) "cm_blueprint_vradatecreated" [4]=> string(33) "cm_blueprint_vralastupdated" [5]=> string(30) "cm_blueprint_description" [6]=> string(26) "cm_blueprint_service" [7]=> string(25) "cm_blueprint_status" [8]=> string(26) "cm_blueprint_version" [9]=> string(30) "cm_blueprint_requestable" [10]=> string(25) "cm_blueprint_tenant" [11]=> string(25) "cm_blueprint_output" [12]=> string(24) "cm_blueprint_quota" [13]=> string(30) "cm_blueprint_createdtime" [14]=> string(29) "cm_blueprint_lastupdate" }