PHP import and export EXCELl, CSV

Source: Internet
Author: User
PHP import and export EXCELl, CSV
# PHP import and export Excel, CSV ## HTML "" ### PHP import> function importcsv "/*** [importcsv description] * @ return [type] [description] */public function importcsv () {if (IS_POST) {$ filename = $ _ FILES ['file'] ['tmp _ name']; if (empty ($ filename )) {$ this-> error ("csv import file, please do not leave it blank");} $ handle = fopen ($ filename, 'r'); function input_csv ($ handle) {$ out = array (); $ n = 0; while ($ data = fgetcsv ($ handle, 10000) {$ num = Count ($ data); for ($ I = 0; $ I <$ num; $ I ++) {$ out [$ n] [$ I] = $ data [$ I] ;}$ n ++;} return $ out ;}$ result = input_csv ($ handle ); // Parse csv // $ number = count ($ result, COUNT_RECURSIVE)-count ($ result); // $ len_result = count ($ result ); $ len_result = count ($ result)-1; if ($ len_result = 0) {$ this-> error ("csv imported data is empty ");} foreach ($ result as $ kr => $ vr) {// write your business logic here if ($ kr) {$ I = 0; // Chinese transcoding $ data ['esta Te_id '] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // real estate ID $ data ['num'] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // building $ data ['unit '] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // unit $ data ['float'] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // floor $ data ['Number'] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // house number $ data ['status'] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // status $ data ['type'] = iconv ('gb2312', 'utf-8', $ vr [$ I ++] ); // Associated apartment $ data ['market _ money'] = iconv ('gb2312', 'utf-8', $ vr [$ I ++]); // market price // create_time last_time NOW_TIME $ data ['create _ time'] = iconv ('gb2312 ', 'utf-8', NOW_TIME ); // creation time $ data ['last _ time'] = iconv ('gb2312', 'utf-8', NOW_TIME ); // modification time $ list = D ('properties')-> add ($ data) ;}} if ($ list) {$ unit_info = D ('unit ') -> where ('Id = '. I ('unit _ id')-> find (); $ map ['estate _ id'] = $ unit_info ['estate _ id']; $ m Ap ['build'] = $ unit_info ['build']; $ buildid = D ('Build')-> where ($ map)-> getField ('id '); $ success = D ('unit ')-> where ('Id = '. I ('unit _ id')-> data (array ('status' => 1)-> save (); if ($ success) {$ this-> success ("csv import successful! Import ". $ len_result. "Data entries", U ('admin/Unit/Index', array ('id' => $ buildid )));}} else {$ this-> error ("failed to import csv! ");} Fclose ($ handle); // close pointer} else {$ info = M ('unit ')-> where ('Id = '. I ('id')-> find (); $ buid = D ('properties')-> bu1__property ($ info); S ('buid '. time (). I ('id'), $ buid); $ protype = D ('estate')-> get_project_type ($ info ['estate _ id']); $ this-> assign ('unit ', $ buid); $ this-> assign ('unit _ id', I ('id ')); $ this-> assign ('type', $ protype); $ this-> assign ('estate _ id', 'buid '. time (). I ('id'); $ this-> dis Play () ;}}" ### PHP export> Here I want to export the Excel file and export it as a csv file first, for details, you can change> function exportcsv "/*** [exportcsv description] to export Excel * @ return [type] [description] */public function exportcsv () {// export Excel estate_id num unit $ id = I ('id'); if (isset ($ id )&&! Empty ($ id) {$ unit_info = D ('unit ')-> where ('Id = '. $ id)-> find (); if ($ unit_info) {$ map = array (); $ map ['estate _ id'] = $ unit_info ['estate _ id']; // Project id $ map ['num'] = $ unit_info ['build']; // building $ map ['unit '] = $ unit_info ['unit']; // unit $ result = M ('properties')-> where ($ map) -> select (); if ($ result) {$ title = array ('sequence', 'Project # project', 'loudd', 'unit ', 'floor ', 'house number ', 'status', 'associated apartment type', 'market price ', 'creation time', 'modification time'); foreach ($ Result as $ k => $ v) {$ data [$ k] [] = $ v ['id']; // sequence $ data [$ k] [] = idtoname ($ v ['estate _ id'], 'Estate', 'name', 'id '). '#'. $ v ['estate _ id']; // project # $ data [$ k] [] = $ v ['num']; // building $ data [$ k] [] = $ v ['unit ']; // unit $ data [$ k] [] = $ v ['float']; // floor $ data [$ k] [] = $ v ['float']. '0 '. $ v ['Number']; // house number $ data [$ k] [] = $ v ['status']; // status $ data [$ k] [] = idtoname ($ v ['type'], 'type', 'name', 'id '); // Associated apartment $ d Ata [$ k] [] = format_price ($ v ['market _ money']); // market price $ data [$ k] [] = time_format ($ v ['create _ time']); // creation time $ data [$ k] [] = time_format ($ v ['last _ time']); // modification time} export_csv_xls ($ title, $ data ); unset ($ title, $ data); exit ;}} else {$ this-> error ("no real estate unit information found! ") ;}} Else {$ this-> error (" csv import failed! ") ;}}"> Function export_csv_xls" function export_csv_xls ($ title, $ array = '', $ code = 'gb2312 ', $ filename =") {if (! $ Filename) {// $ filename = date ("Y-m-d "). ". xls "; $ filename = randStr (10 ). ". xls ";} header (" Content-Type: application/vnd. ms-execl "); header (" Content-Type: application/vnd. ms-excel; charset = ". $ code); header ("Content-Disposition: attachment; filename = $ filename"); header ("Pragma: no-cache"); header ("Expires: 0 "); if ($ code = "gb2312") {$ title = array_map ("utf8togb2312", $ title);} echo implevels E ("\ t", $ title ). "\ n"; if ($ array) {foreach ($ array as $ v) {if ($ code = "gb2312") {$ v = array_map ("utf8togb2312 ", $ v);} echo implode ("\ t", $ v ). "\ n" ;}}}"> function utf8togb2312 "function utf8togb2312 ($ str) {// code conversion $ str = format_xml_num ($ str ); $ bianma = mb_detect_encoding ($ str); if ($ bianma = "GB2312") {return $ str;} else {return iconv ($ bianma, 'gb2312 ', trim ($ str) ;}} "> function forma T_xml_num "function format_xml_num ($ str) {// export the formatted number if (strlen (float) $ str) >=12 & is_numeric (float) $ str) & (float) $ str &&! Strpos ($ str, '.') {$ str = "'". (string) $ str;} return $ str ;}"

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.