How to Convert numbers into scientific counts when exporting data to excel in php _ PHP Tutorial

Source: Internet
Author: User
When exporting data to excel in php, the number becomes scientific count. When the data is exported to excel, the numeric format is incorrect, which is generally divided into the following two situations. 1. solve the problem of insufficient length of an excel cell: Copy the code as follows: when the data is exported to an excel file in an excel. php file, the numeric format is incorrect, which is generally divided into the following two situations.

1. insufficient length for excel cells

Solution:

The code is as follows:


// In the excel. php file
$ ObjActSheet = $ objPHPExcel-> getActiveSheet ();
// Set the topic name
$ ObjActSheet-> setCellValue ("b1", "Card number ");
// Set the column width
$ ObjActSheet-> getColumnDimension ('B')-> setWidth (20); // change the length value set here.



2. the character is understood as a number in excel. generally, it is set to text or try to add spaces.
Solution:

The code is as follows:


// Add data, mainly to process the data to be displayed using the chunk_split () function. you can view the specific usage of this function on your own.
$ ObjActSheet-> setCellValue ("B $ I", chunk_split ("123456789", 4, ""); // of course, if you do not want to see a space between digits, set the value of the field to be split to a greater value. for example, set 4 to a value greater than or equal to 9.



The first part of the main code for exporting EXcel is as follows:

The code is as follows:


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 exported file name
Header ("Pragma: no-cache ");
Header ("Expires: 0 ");
If ($ filename_type = 'XLS '){
Echo'Xmlns: x = "urn: schemas-microsoft-com: office: excel"
Xmlns = "http://www.w3.org/TR/REC-html40">





';
}


The following is
Format output data

I finally found some useful information for a long time. the excerpt is as follows:

"First, let's take a look at how excel exports from a web page. When we send the data to the client, we want the client program (browser) to read it in excel format, so we set the mime type to application/vnd. ms-excel: When an excel file is read, data is displayed in the format of each cell. if the cell does not have a specified format, excel displays the data of the cell in the default format. This provides us with a space for custom data formats. of course, we must use the formats supported by excel. Common formats are listed below:
1) Text: vnd. ms-excel.numberformat :@
2) Date: vnd. ms-excel.numberformat: yyyy/mm/dd
3) Number: vnd. ms-excel.numberformat: #,## 0.00
4) currency: vnd. ms-excel.numberformat: ¥ #, #0.00
5) percentage: vnd. ms-excel.numberformat: # 0.00%
You can also customize these formats. for example, you can define the year and month as yy-mm. So how can I add these formats to cells when I know these formats? It is very simple. we only need to add the style to the corresponding tag pair (that is, to close the tag. For example, To the tag pairAdd a style as follows:410522198402161833
Similarly, we can also

To add a style, you can also,
Add a style. when we add a style to both the parent and child tags, which style will the data be presented in? After testing, it is displayed in the style closest to the data.

Bytes. 1. solve the problem of insufficient length of excel cells: the code is as follows: // in the excel. php file...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.