Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPEXCEL-数组到CSV文件-第一行中的所有标题_Php_Phpexcel - Fatal编程技术网

PHPEXCEL-数组到CSV文件-第一行中的所有标题

PHPEXCEL-数组到CSV文件-第一行中的所有标题,php,phpexcel,Php,Phpexcel,我正在尝试使用PHPEXCEL创建一个CSV文件。这是我的代码: <?php /** Include PHPExcel */ require_once($path.'PHPExcel/Classes/PHPExcel.php'); require_once($path.'PHPExcel/Classes/PHPExcel/IOFactory.php'); // Create new PHPExcel object $objPHPExcel = new PHPExcel(); $res

我正在尝试使用PHPEXCEL创建一个CSV文件。这是我的代码:

<?php

/** Include PHPExcel */
require_once($path.'PHPExcel/Classes/PHPExcel.php');
require_once($path.'PHPExcel/Classes/PHPExcel/IOFactory.php');

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

$res_headers = array(
    'A' => 'Title',
    'B' => 'Custom Label',
    'C' => 'CustomLabel',
    'D' => 'Description',
    'E' => 'PicURL',
    'F' => 'C:Herstellernummer',
    'G' => 'StartPrice'
);

$ff = array_flip($res_headers);
$dd = array_values($ff);

$objPHPExcel->setActiveSheetIndex(0);
foreach($res_headers AS $key => $value)
{
    $objPHPExcel->getActiveSheet()->setCellValue($key.'1', $value);
}

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="simple.csv"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->setDelimiter(','); 
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->save('php://output');

?>

问题是成排的。在生成的CSV文件中,标题、自定义标签、自定义标签等所有行都位于第一行(A1)。我想补充:

  • A1行中的“标题”
  • “B1”行中的自定义标签
  • “C1”行中的“CustomLabel”
  • 。。。 等等
但现在,所有内容都在第一行(A1):“标题,自定义标签,自定义标签,描述,PicURL,C:HerstellNummer,StartPrice”。为什么?你能帮我吗


谢谢。

好的,我找到了解决方案。只需更改此选项:

$objWriter->setDelimiter(','); 
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
为此:

$objWriter->setDelimiter(';'); 
$objWriter->setEnclosure('"');
$objWriter->setLineEnding("\r\n");

就这些。

好的,我找到了解决方案。只需更改以下内容:

$objWriter->setDelimiter(','); 
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
为此:

$objWriter->setDelimiter(';'); 
$objWriter->setEnclosure('"');
$objWriter->setLineEnding("\r\n");
就这些