我做了大量的研究,并且使用PHPExcel输出无望。当我使用PHPexcel列出结果时,我得到了codeigniter中的输出,但并不总是如此。我该如何纠正这个问题?我对Excel表格编码不是很精通。 这是我的疑问:
public function excel_customer_all($date, $fdate)
{
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && $_SESSION['role'] && $_SESSION['role_des'])
{
$this->load->library('excel');
$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '#000000'),
'size' => 10,
'name' => 'Verdana'
));
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('All Customer Details');
.....
SETTING CELL VALUES HERE
.....
//set cell A1 content with some text
................just ignoring these parts to shorten the code.........
//retrive contries table data
$rs=$this->Customer_model->list_all_customer_date($date, $fdate);
$row = 4; // 1-based index
$col = 0;
$no = 1;
foreach($rs as $key=>$value) {
echo $row . ", ". $col . "<br>";
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row,$no);
$col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_main_ph);
$col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_name);
$col++;$col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_add);
$col++; $col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_email);
$col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_main_location);
$col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->category);
$col++;
if($value->used_devices&&$value->used_device_count){
$z=array();
$a=explode(',',$value->used_devices);
$b=explode(',',$value->used_device_count);
foreach($a as $index=>$val){
if($b[$index]=!0)
{
array_push($z,$a[$index].'-'.$b[$index]);
}
}
$y='';
$y=implode(" , ",$z);
}
if($value->other_device&&$value->other_device_count){
$f=array();
$c=explode(',',$value->other_device);
$d=explode(',',$value->other_device_count);
foreach($c as $index=>$val){
if($d[$index]=!0&&$c[$index])
{
array_push($f,$c[$index].'-'.$d[$index]);
}
}
$g=implode(" , ",$f);
}
if($y || $g)
{
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $y.' , '.$g);
}
$col++;
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->follow_up_dt);
$col++;
$row++;$col++;
echo $row . ", ". $col . "<br>";
$col = 0;
$no++;
}
$this->excel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('B4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('C4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$filename=mt_rand(1,100000).'.xls'; //just some random filename
ob_clean();
ob_start();
header('Content-type: application/vnd.ms-excel; charset=UTF-8' );
//header('Content-Type: application/xlsx');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel,'Excel5');
$objWriter->save('php://output');
} else
{
redirect('Admin/index', 'refresh');
}
}
当我使用多个选定数据测试代码时,我发现问题出现在上述函数中的爆炸部分附近。有时,错误会显示&#34;未定义的变量$ y&#34; ,有时会显示&#34;已发送的标题&#34; 带有一些数字的警告消息例如:见下图:
无论数据库中存在的数据类型如何,都应该做什么才能获得输出。
答案 0 :(得分:2)
防止错误&#34; 未定义变量$ y &#34;在foreach循环的每次迭代开始时初始化$ y。
const styles = {
propContainer: {
width: '100%',
marginTop: theme.spacing.unit * 3,
overflowX: 'auto',
margin: '0 auto',
},
};
并检查它是否为空
foreach($rs as $key=>$value) {
$y = '';
....