PHP exports data to Excel to prevent digital character formats such as identity cards from becoming a scientific counter-------disadvantage, you must use the table TR TD

Source: Internet
Author: User
Tags closing tag

PHP exports data to Excel to prevent digital character formats such as identity cards from becoming scientific counting parties Time:2012-08-17 13:50 Source:Unknown Author:Silicon Peak network-red bean Click:219 times on the internet to find a lot of information about the solution is about two: one is to add an English single quotation mark in front of the ID field, and the other is to format excel in text format. I tried the first one really can show, but there is a ' number in there feel really not good, although heard not to affect, but need the user to click the cell that single quote to disappear, and in the

There's a lot of information on the Internet. There are about two solutions: one is to add an English single quotation mark in front of the ID field, and the other is to format excel in text format.

I tried the first one really can show, but there is a "'" sign there is really not very good, although heard does not affect, but need the user to click the cell that single quote to disappear, and in the upper left corner of the lattice to display a green triangle.

The customer asked me to continue to improve.

Then consider the second method, found that the information on the Internet is some, but it is about delphi,asp.net,asp and other formats.

And about PHP, but most of them are exported using the Phpexcel method or spreadsheet such as the export method, such as class or control, and I am in the maintenance of the system is a very simple method, such as the following, there is very little on the internet to tell how to set the Excel format to export data.

I exported the previous section of the main code for 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 the 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">
<meta http-equiv= "Expires" content= "Mon, 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]-->

}

The following is the output of the data in <table><tr><td></td></tr></table> format

After most of the day found a little useful information, now excerpt as follows:

"First, let's look at how Excel exports from Web pages." When we send this data to the client, we want to have the client program (the browser) read it in Excel format, so we set the MIME type to: application/ Vnd.ms-excel, when Excel reads a file, it renders the data in the format of each cell, and if the cell does not have a specified format, Excel renders the cell's data in the default format. This gives us the space to customize the data format, and of course we must use the format supported by Excel. Here's a list of some commonly used formats:
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.0%
These formats you can also customize, such as the year you can be defined as: yy-mm and so on. So what do you know about these formats and how do you add them to the cell? Very simply, we just need to add the style to the corresponding label pair (that is, the closing tag). For example, add a style to the label <td></td> <td></td>, as follows: <td style= "vnd.ms-excel.numberformat:@" > 410522198402161833</td>
Also, we can add styles to <div></div> and add styles to <tr></tr>,<table></table>. When we add a style to both the parent tag pair and the child label pair, which style does the data appear in? After testing, it is rendered in a style that is closest to the data.

So according to his method set the export data when the corresponding ID column of the <td> style:

echo "<td style= ' vnd.ms-excel.numberformat:@ ' >". $printable. " </td>\n ";

Sure enough, no wasted half-day information.

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.