Php reads excel files, and php packages are zip files. _ PHP Tutorial

Source: Internet
Author: User
Tags php excel ziparchive
Php reads the excel file, and the php package folder is a zip file ,. Php is used as a zip file, and php is used as a zip file. 1. download the file to the folder and put it in the apacheenvironment 2.d.xlsx is the server name and player list of a game. this program is only suitable for php to read excel files and pack php folders as zip files,
1. download the file to your local computer and place it in the Apache environment.
2.d.xlsx is the server name and player list of a game. this program is only applicable to the xlsx file structure. for other structures, modify the index. php source code.
3.unzip zip.php is used to pack the generated files into files.zip.
4. access index. php to generate the files folder, where 0. js --- n. js stores the names of each server and server_name_list.js stores the server list.
5. Classes stores the functional module for php to read excel. the specific task logic is in index. php.

A. PHP excel reading support excel2007

Demo logic code: the (arrayRecursive, JSON method is the json data processing function, compatible with Chinese characters)
Mainly with: PHPExcel plug-in, the attachment has Classes folder, official website: http://www.codeplex.com/PHPExcel
Index. php
 
 Title    
 1000) {die ('possible deep recursion attack');} foreach ($ array as $ key => $ value) {if (is_array ($ value )) {arrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also);} else {$ array [$ key] = $ function ($ value );} if ($ apply_to_keys_also & is_string ($ key) {$ new_key = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter --;} /*************************************** * ************************ convert an array to a JSON string (compatible with Chinese characters) * @ param array $ array the array to be converted * @ return string the converted json string * @ access public ***************** **************************************** * ***/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = jso N_encode ($ array); return urldecode ($ json);} require_once 'classes \ PHPExcel. php '; require_once' Classes \ PHPExcel \ IOFactory. php '; require_once 'classes \ PHPExcel \ Reader \ excel2007.php'; $uploadfile+'d.xlsx'; $ objReader = PHPExcel_IOFactory: createReader ('excel2007 '); /* Excel5 for 2003 excel2007 for 2007 */$ objPHPExcel = PHPExcel_IOFactory: load ($ uploadfile); $ sheet = $ objPHPExcel-> getSheet (0); $ highes TRow = $ sheet-> getHighestRow (); // get the total number of rows $ highestColumn = $ sheet-> getHighestColumn (); // obtain the total number of columns/* method [recommended] */$ objWorksheet = $ objPHPExcel-> getActiveSheet (); $ highestRow = $ objWorksheet-> getHighestRow (); // get the total number of rows $ highestColumn = $ objWorksheet-> getHighestColumn (); $ highestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn); // The total number of columns $ list = array (); for ($ row = 1; $ row <= $ highestRow; $ row ++) {$ Strs = array (); // note that the index of the highestColumnIndex column starts from 0 for ($ col = 0; $ col <$ highestColumnIndex; $ col ++) {$ strs [$ col] = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row)-> getValue ();} array_push ($ list, $ strs );} // read $ list // process the data and generate a new structure $ n = 0; $ ser = array (); for ($ I = 0; $ I <count ($ list); $ I ++) {$ ser [$ n] [0] = $ list [$ I] [0]; if (! Is_array (@ $ ser [$ n] [1]) {$ ser [$ n] [1] = array ();} array_push ($ ser [$ n] [1], $ list [$ I] [1]); if ($ I! = Count ($ list)-1) {if ($ list [$ I] [0]! = $ List [$ I + 1] [0]) {$ n ++ ;}}/ * output file */$ sname = array (); $ f = 'Files/'; // store the directory if (! File_exists ($ f) {mkdir ($ f) ;}for ($ j = 0; $ j <count ($ ser); $ j ++) {$ file = $ f. $ j. '. js'; echo $ file."
"; $ Fp = fopen (" $ file "," w + "); // open the file pointer and create the file if (! Is_writable ($ file) {die ("file:". $ file. "cannot be written. Please check it! ");} If (is_writable ($ file) = false) {die ('I am a chicken feather, I can't ');} $ data = $ ser [$ j] [1]; array_push ($ sname, $ ser [$ j] [0]); file_put_contents ($ file, JSON ($ data); fclose ($ fp); // close pointer} $ file = $ f. 'server _ name_list.js '; echo $ file."
"; $ Fp = fopen (" $ file "," w + "); // open the file pointer and create the file if (! Is_writable ($ file) {die ("file:". $ file. "cannot be written. Please check it! ");} If (is_writable ($ file) = false) {die ('I am a chicken feather, I cannot');} file_put_contents ($ file, JSON ($ sname); echo "generated! "; Echo 'package and generate file'?>

B. The PHP package folder is a zip file.

Zip. php

 
 Title
 AddFile ($ path. "/". $ filename) ;}}@ closedir ($ path) ;}$ zip = new ZipArchive (); if ($ zip-> open('files.zip ', ZipArchive: OVERWRITE) === TRUE) {addFileToZip ('files ', $ zip); // call the method to operate the root directory to be packaged, and pass the ZipArchive object to the method $ zip-> close (); // close the processed zip file} echo. the package is complete! '."
"; Echo" download files.zip "?>

Download Code: php-read-excel

Http://files.cnblogs.com/zhidong123/php-read-excel.zip




Downloads, 1. download the file to the local computer and put it in the Apache environment 2.d.xlsx is the server name and player list of a game. this program is only suitable...

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.