Home  >  Article  >  Backend Development  >  Initial use of phpExcel How to make excel drop-down menu excel2013 official download excel2007

Initial use of phpExcel How to make excel drop-down menu excel2013 official download excel2007

WBOY
WBOYOriginal
2016-07-29 08:49:04843browse

It has been a while since I started using PHP. Let me summarize some of the techniques I have used recently so that I can use them directly in the future:

    header("Content-type: text/html; charset=utf-8");  
    require_once'phpexcel.php';  
    require_once'PHPExcel\IOFactory.php';  
    require_once'PHPExcel\Reader\Excel2007.php';  
    $uploadfile='data.xlsx';  

    $objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/$objPHPExcel = $objReader->load($uploadfile); //Excel 路径  $sheet = $objPHPExcel->getSheet(0);  
    $highestRow = $sheet->getHighestRow(); // 取得总行数  $highestColumn = $sheet->getHighestColumn(); // 取得总列数  /*方法一*/$strs=array();  
    for ($j=1;$j<=$highestRow;$j++){//从第一行开始读取数据  /*注销上一行读取数据*/unset($str);  
        unset($strs);  
        for($k='A';$k<=$highestColumn;$k++){//从A列读取数据  //实测在excel中,如果某单元格的值包含了||||||导入的数据会为空                       $str .=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'||||||';//读取单元格  
        }  
        //explode:函数把字符串分割为数组。              $strs = explode("||||||",$str);  
        $sql = "INSERT INTO te() VALUES ( '{$strs[0]}','{$strs[1]}', '{$strs[2]}','{$strs[3]}','{$strs[4]}')";  
        echo$sql.'
'
; } /*方法二【推荐】*/$objWorksheet = $objPHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); // 取得总行数 $highestColumn = $objWorksheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数 for ($row = 1;$row <= $highestRow;$row++) { $strs=array(); //注意highestColumnIndex的列数索引从0开始 for ($col = 0;$col < $highestColumnIndex;$col++) { $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); } print_r($strs); } ?>

Result: array(4) { [0]=> string(10) "Software Class 1 " [1]=> string(7) "Zhang San 1" [2]=> string(7) "Hello 1" [3]=> string(0) "" } array(4) { [ 0]=> string(10) "Software Class 2" [1]=> string(7) "Zhang San 2" [2]=> string(7) "Hello 2" [3]=> string(0) "" } array(4) { [0]=> string(10) "Software Class 3" [1]=> string(7) "Zhang San 3" [2]=> string( 7) "Hello 3" [3]=> string(0) "" } array(4) { [0]=> string(10) "Software Class 4" [1]=> string(7) "Zhang San 4" [2]=> string(7) "Hello 4" [3]=> string(0) "" } array(4) { [0]=> string(10) "Software Class 5" [1]=> string(7) "Zhang San 5" [2]=> string(7) "Hello 5" [3]=> string(0) "" } array(4) { [0]=> string(10) "Software Class 6" [1]=> string(7) "Zhang San 6" [2]=> string(7) "Hello 6" [3]= > string(0) "" } array(4) { [0]=> string(10) "Software Class 7" [1]=> string(7) "Zhang San 7" [2]=> string(7) "Hello 7" [3]=> string(0) "" } array(4) { [0]=> string(10) "Software Class 8" [1]=> string( 7) "Zhang San 8" [2]=> string(7) "Hello 8" [3]=> string(0) "" } array(4) { [0]=> string(10) "Software Class 9" [1]=> string(7) "Zhang San 9" [2]=> string(7) "Hello 9" [3]=> string(0) "" }
The above is my usage code for using phpExcel. I know there are many on the Internet, but after summarizing, I can still find it quickly in the future, and I don’t need Baidu anymore;


phpExcel package

').addClass('pre-numbering ').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the preliminary use of phpExcel, including excel content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn