Phpgeneration and reading excelfile (.csv) instance

Source: Internet
Author: User

Excelfile (.csv)

The code is as follows: Copy code

<? Php
Function getCSVdata ($ filename)

$ Row = 1; // start with the first line
If ($ handle = fopen ($ filename, "r "))! = False)
    { 
While ($ dataSrc = fgetcsv ($ handle ))! = False)
        { 
$ Num = count ($ dataSrc );
For ($ c = 0; $ c <$ num; $ c ++) // column
            { 
If ($ row = 1) // The first row is used as the field
                { 
$ DataName [] = $ dataSrc [$ c]; // field name
                } 
Else
                { 
Foreach ($ dataName as $ k => $ v)
                    { 
If ($ k = $ c) // corresponding field
                        { 
$ Data [$ v] = $ dataSrc [$ c];
                        } 
                    } 
                } 
            } 
If (! Empty ($ data ))
            { 
$ DataRtn [] = $ data;
Unset ($ data );
            } 
$ Row ++;
        } 
Fclose ($ handle );
Return $ dataRtn;
    } 

 
$ AData = getCSVdata('all_www.111cn.net.csv ');
 
Foreach ($ aData as $ k =>$ v ){
Echo "http: //". $ v ['A']. "<br> ";
}
?>


Generate an excel file (csv)

The code is as follows: Copy code

<? Php
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=test_data.xls ");
// The output content is as follows:
Echo "name". "t ";
Echo "age". "t ";
Echo "education". "t ";
Echo "n ";
Echo "James". "t ";
Echo "25". "t ";
Echo "undergraduate". "t ";
?>

The above is just some simple excel file operations. If you want to edit, modify, or delete a row table, you can use the phpexcel plug-in to operate excel files.

Recommended: use phpexcel to read the excel code

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.