php里的文字需要用mb_convert_encoding($string, ‘utf-8‘, ‘gbk‘)转换一下,要不然会出现乱码的情况
$objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setTitle("定制车专题用户意向"); //设置当前的sheet $objPHPExcel->setActiveSheetIndex(0); //设置sheet的name $objPHPExcel->getActiveSheet()->setTitle(mb_convert_encoding(‘用户意向‘, ‘utf-8‘, ‘gbk‘)); $titleArr = array( ‘A1‘ => ‘购车人姓名‘, ‘B1‘ => ‘购车人电话‘, ‘C1‘ => ‘车型‘, ‘D1‘ => ‘颜色‘, ‘E1‘ => ‘所选配件‘, ‘F1‘ => ‘购车省份‘, ‘G1‘ => ‘购车城市‘, ‘H1‘ => ‘预计提车时间‘, ‘I1‘ => ‘创建时间‘, ); foreach ($titleArr as $k => $v) { $objPHPExcel->getActiveSheet()->setCellValue($k, mb_convert_encoding($v, ‘utf-8‘, ‘gbk‘)); } $i = 2; foreach ($res as $item) { //设置单元格的值 $objPHPExcel->getActiveSheet()->setCellValue(‘A‘ . $i, $item[‘name‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘B‘ . $i, $item[‘phone‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘C‘ . $i, $item[‘carModuel‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘D‘ . $i, $item[‘color‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘E‘ . $i, $item[‘parts‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘F‘ . $i, $item[‘province‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘G‘ . $i, $item[‘city‘]); $objPHPExcel->getActiveSheet()->setCellValue(‘H‘ . $i, mb_convert_encoding($item[‘takeoutTime‘] == 0 ? "一个周内" : $item[‘takeoutTime‘] == 1 ? "一个月内" : "三个月内", ‘utf-8‘, ‘gbk‘)); $objPHPExcel->getActiveSheet()->setCellValue(‘I‘ . $i, empty($item[‘insertTime‘]) ? "--" : date(‘Y-m-d‘, $item[‘insertTime‘])); $i++; } $fileName = sprintf("定制车专题用户意愿%s.xls", date(‘Y-m-dHi‘)); // $objWriter->save($fileName); // 直接输出到浏览器 // $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); header("Pragma: public"); header("Expires: 0"); header("Cache - Control:must - revalidate, post - check = 0, pre - check = 0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header(sprintf(‘Content-Disposition:attachment;filename="%s"‘, $fileName)); header("Content-Transfer-Encoding:binary"); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘); $objWriter->save(‘php://output‘);
时间: 2024-11-14 11:16:47