Home  >  Article  >  Backend Development  >  How to set cell formatting in php

How to set cell formatting in php

藏色散人
藏色散人Original
2021-09-25 09:10:282846browse

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.

How to set cell formatting in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn