在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;
?>