Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.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
Php 从数据库导出到.CSV_Php_Excel_Phpexcel - Fatal编程技术网

Php 从数据库导出到.CSV

Php 从数据库导出到.CSV,php,excel,phpexcel,Php,Excel,Phpexcel,在PHPExcel插件的帮助下,我创建了这个PHP脚本,将一些数据从数据库导出到.CSV文档 问题是.CSV文档上的数据在导出后都保留在同一列中,仅用逗号分隔 如果我导出到.XLS而不是.CSV,它将按预期工作 是否可以导出为.CSV并保留我想要的格式,而不是用逗号分隔 <?php session_start(); error_reporting(0); ini_set('display_err

在PHPExcel插件的帮助下,我创建了这个PHP脚本,将一些数据从数据库导出到.CSV文档

问题是.CSV文档上的数据在导出后都保留在同一列中,仅用逗号分隔

如果我导出到.XLS而不是.CSV,它将按预期工作

是否可以导出为.CSV并保留我想要的格式,而不是用逗号分隔

<?php
                session_start();
                error_reporting(0);
                ini_set('display_errors', TRUE);
                ini_set('display_startup_errors', TRUE);
                set_time_limit (120);

                $db=mysqli_connect('','','','testes') or die("Error " . mysqli_error($db));
                mysqli_set_charset($db, 'utf8');
                $time=$_SESSION['id_ficheiro'];

                $SQL1="select data from testes.lidl_promos where id_ficheiro='$time' group by data";
                $result1=mysqli_query($db, $SQL1);
                while($row1=mysqli_fetch_assoc($result1)){
                $data_promo=$row1['data'];

                $SQL="select loja, date_format(data_entrega,'%d%m%Y') as data_ent,artigo,caixas from testes.lidl_promos where data='$data_promo' and id_ficheiro='$time'";
                $result=mysqli_query($db, $SQL);

                ini_set('include_path', ini_get('include_path').';../plugin/phpexcel/');
                require_once 'PHPExcel.php';
                require_once 'PHPExcel/IOFactory.php';

                $objPHPExcel = new PHPExcel();
                $r=$objPHPExcel->getActiveSheet();

                $r->setCellValue('A1', 'Lojas');
                $rowcount=2;

                while($row=mysqli_fetch_assoc($result)){               

                        $r->setCellValue('A'.$rowcount, $row['loja']);
                        $r->setCellValue('C'.$rowcount, $row["data_ent"]);
                        $r->setCellValue('D1', $row['artigo']);
                        $r->setCellValue('D'.$rowcount, $row['caixas']);
                        ++$rowcount;
                }


                header("Content-type: text/csv");

                //header('Content-Disposition: attachment;filename="report.xls"');
                $date=date('dmY');
                $new_data_promo = date("d-m-Y", strtotime($data_promo));
                $name=$date.'_promo_'.$new_data_promo;
                //$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
                $objWriter->save(str_replace(__FILE__,'lidl_output/'.$name.'.csv',__FILE__));
                //$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                //$objWriter->save(str_replace(__FILE__,'lidl_output/'.$name.'.xls',__FILE__));

                }
                header('Content-Type: application/json');
                echo json_encode(array('status' => 'Exportado com sucesso'));

                die();
                exit;

?>

CSV代表逗号分隔的值。你的问题让人困惑,请澄清。php内置了csv函数,我不知道你为什么在这里使用PHPexcel。我不会为此使用PHPexcel,我只会使用php内置的fputcsv函数;但是在MS Excel中打开CSV文件时需要非常小心,因为分隔符是特定于MS Excel版本的区域设置的,错误的分隔符根本无法识别对不起,我不知道如何格式化注释中的代码$句柄=假;$rowcount=1;$date=日期'dmY';$new_data_promo=dated-m-Y,strotime$data_promo;$name=$date.promo'.$new\u data\u promo;$handle=fopenlidl_output/$name..csv,“a+”;而$row=mysqli_fetch_assoc$result{if$rowcount==1{if$handle{fwrite$handle,Lojas,,,$row['artigo'];}}其他{if$handle{fwrite$handle,row['loja'],$row['caixas'];}++$rowcount;还是不行。同样的问题。