我正在使用PHPExcel将数据库数据转换为Excel Sheet。
我的代码:
$objPHPExcel = new PHPExcel();
$serialnumber=0;
$tmparray =array("Sl. No.","Ref. No.","Status");
$sheet =array($tmparray);
$tmparray2 =array("Mandatory","Mandatory","");
array_push($sheet,$tmparray2);
foreach ($customerHo as $key => $c) {
$tmparray =array();
$serialnumber = $serialnumber + 1;
$ref_no = $c['Lead']['ref_no'];
$status = $c['LeadFeedback']['feedback_status'];
array_push($tmparray,$serialnumber);
array_push($tmparray,$ref_no);
array_push($tmparray,$status);
array_push($sheet,$tmparray);
}
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="Feedback.xlsx"');
$worksheet = $objPHPExcel->getActiveSheet();
foreach($sheet as $row => $columns) {
foreach($columns as $column => $data) {
$worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
}
}
$objPHPExcel->getActiveSheet()->getStyle("A1:C1")->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()
->getStyle('A1:C1')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('66a3ff');
$objPHPExcel->getActiveSheet()
->getStyle('A2:C2')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('b8b894');
$objPHPExcel->getActiveSheet()->getStyle('A2:C2')->applyFromArray(array('font' => array( 'size' => 8,'color' => array('rgb' => 'cc0000'))));
$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->applyFromArray(array('font' => array( 'size' => 10)));
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:C2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator();
$cellIterator->setIterateOnlyExistingCells( true );
foreach( $cellIterator as $cell ) {
$sheet->getColumnDimension( $cell->getColumn() )->setAutoSize( true );
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
我想在状态列中创建一个包含(Won,NR,U,W,I,NA)值的下拉列表,并且如果数据库中存在某个值,则将在下拉列表中选择相同的值默认情况下。感谢进阶。