Export and write Excel files using PHP

Source: Internet
Author: User

<? Php // PHPExcel need to download Official Website: http://www.codeplex.com/PHPExcelheader ("Content-Type: text/html; charset = UTF-8"); require_once '. /PHPExcel. php '; require_once '. /PHPExcel/IOFactory. php '; require_once '. /PHPExcel/Reader/Excel5.php ';/*** read the Excel table * @ param $ filePath Excel file path * @ param $ field the field to be saved in array ('id ', 'username', 'Password') * @ param $ column reads the array ('A', 'B', 'C') of the Excel column * @ examlpe $ filePath = '1.xls '; $ field = array ('I D', 'username', 'Password'); $ column = array ('A', 'B', 'C'); readExcel ($ filePath, $ field, $ column); */function readExcel ($ filePath, $ field, $ column) {$ objReader = PHPExcel_IOFactory: createReader ('excel5 '); // use excel2007 for 2007 format $ objPHPExcel = $ objReader-> load ($ filePath); // $ filename can be an uploaded file, or the specified file $ sheet = $ objPHPExcel-> getSheet (0); $ highestRow = $ sheet-> getHighestRow (); // get the total number of rows $ highestColumn = $ Sheet-> getHighestColumn (); // obtain the total number of columns for ($ j = 1; $ j <= $ highestRow; $ j ++) {$ colData = array (); $ count = count ($ column); for ($ I = 0; $ I <$ count; $ I ++) {$ colData [$ field [$ I] = $ objPHPExcel-> getActiveSheet ()-> getCell ($ column [$ I]. $ j)-> getValue (); // obtain the value of column A} $ excelData [] = $ colData;} return $ excelData ;} /*** export data into an excel table * @ param $ data: a two-dimensional array. The structure is like the array found from the database * @ param $ title: The first row title of excel, an array, if it is null, no title * @ param $ filename indicates the downloaded file name *@ Examlpe exportexcel ($ arr, array ('id', 'account', 'Password', 'nickname '), 'file name! '); */Function exportexcel ($ data = array (), $ title = array (), $ filename = 'report') {header ("Content-type: application/octet-stream "); header (" Accept-Ranges: bytes "); header (" Content-type: application/vnd. ms-excel "); header (" Content-Disposition: attachment; filename = ". $ filename. ". xls "); header (" Pragma: no-cache "); header (" Expires: 0 "); // export xls to start if (! Empty ($ title) {foreach ($ title as $ k => $ v) {$ title [$ k] = iconv ("UTF-8", "GB2312 ", $ v) ;}$ title = implode ("\ t", $ title); echo "$ title \ n" ;}if (! Empty ($ data) {foreach ($ data as $ key => $ val) {foreach ($ val as $ ck => $ cv) {$ data [$ key] [$ ck] = iconv ("UTF-8", "GB2312", $ cv );} $ data [$ key] = implode ("\ t", $ data [$ key]);} echo implode ("\ n", $ data) ;}}?>

 

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.