How to set cell formatting in php: 1. Set the cell to text through "PHPExcel_Style_NumberFormat::FORMAT_TEXT"; 2. Set the specified data type through setCellValueExplicit, etc.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to set cell formatting in php?
php excel Set cell format to text format
Solve PHPExcel long number string is displayed as scientific notation
In excel, if you enter in a default cell Or copy a very long numeric string, which will be displayed as a scientific calculation, such as an ID number. The solution is to format the table as text or add a single quotation mark before inputting.
Using PHPExcel to generate excel will also encounter the same problem. There are three solutions:
1. Set the cell as text
$objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle('Simple'); //设置A3单元格为文本 $objPHPExcel->getActiveSheet()->getStyle('A3')->getNumberFormat() ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); //也可以设置整行或整列的style /* //E 列为文本 $objPHPExcel->getActiveSheet()->getStyle('E')->getNumberFormat() ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); //第三行为文本 $objPHPExcel->getActiveSheet()->getStyle('3')->getNumberFormat() ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); */
More formats are available Found in PHPExcel/Style/NumberFormat.php. Note: The above settings still display the results of scientific notation in text mode for long numeric strings. The reason may be that PHP uses scientific notation when processing large numbers.
2. The specified data type displayed when setting the value
$objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->getActiveSheet()->setCellValueExplicit('D1',123456789033,PHPExcel_Cell_DataType::TYPE_STRING);
3. Add a space before the numeric string to make it a string
$objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->getActiveSheet()->setCellValue('D1', ' ' . 123456789033);
It is recommended to use the first Two or three, the first one does not fundamentally solve the problem.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to set cell formatting in php. For more information, please follow other related articles on the PHP Chinese website!