How to set cell formatting in php

藏色散人
Release: 2023-03-12 22:04:01
Original
2990 people have browsed it

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);
*/
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template