When the data is exported to excel, the number format is incorrect. Generally, it is divided into the following two situations.
1. The length of the excel cell setting is not enough
Solution:
//在excel.php文件中 $objActSheet = $objPHPExcel->getActiveSheet(); // 设置 栏目名称 $objActSheet->setCellValue("b1", "卡号"); // 设置列的宽度 $objActSheet->getColumnDimension('b')->setWidth(20);//改变此处设置的长度数值
2. The characters are interpreted as numbers by excel. Generally, you set the field to text or find a way to add some spaces.
Solution:
//添加数据处,主要是把要显示数据以chunk_split()函数处理以下,此函数的具体用法可以自己查看 $objActSheet->setCellValue ( "b$i", chunk_split("123456789 ",4," ") ); //当然,如果不想让用户看到数字间有空格,那就把要分割的字段值设大一些,如例子中的4设为大于等于9的即可。
The front part of the main code I exported to EXcel:
<? if(count($data)>40000){ $filename_type='csv'; }else{ $filename_type='xls'; } header("Content-Type: application/vnd.ms-excel"); Header("Accept-Ranges:bytes"); Header("Content-Disposition:attachment;filename=".$filename.".".$filename_type); //$filename导出的文件名 header("Pragma: no-cache"); header("Expires: 0"); if($filename_type=='xls'){ echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT"> <meta http-equiv=Content-Type content="text/html; charset=gb2312"> <!--[if gte mso 9]><xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name></x:Name> <x:WorksheetOptions> <x:DisplayGridlines/> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> </x:ExcelWorkbook> </xml><![endif]--> </head>'; }
The following is followed by
, add a style to the label pair | as follows: | 410522198402161833 td> Similarly, we can also add styles to , or we can add styles to |
The above is the solution to convert numbers into scientific notation when exporting excel with PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!