使用phpexcel上傳下載excel檔案

來源:互聯網
上載者:User

標籤:open   new   公眾   col   nic   created   BMI   file   __file__   

1. 下載

<?php/** * Created by lonm.shi. * Date: 2012-02-09 * Time: 下午4:54 * To change this template use File | Settings | File Templates. */require("Excel/PHPExcel.php");// Create new PHPExcel object$objPHPExcel = new PHPExcel();// Set document properties$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")                             ->setLastModifiedBy("Maarten Balliauw")                             ->setTitle("Office 2007 XLSX Test Document")                             ->setSubject("Office 2007 XLSX Test Document")                             ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")                             ->setKeywords("office 2007 openxml php")                             ->setCategory("Test result file");// Add some data$objPHPExcel->setActiveSheetIndex(0);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A1‘, ‘ID‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘B1‘, ‘OpenID‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘C1‘, ‘暱稱‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘D1‘, ‘性別‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘E1‘, ‘國家‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘F1‘, ‘省份‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘G1‘, ‘城市‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘H1‘, ‘情境‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘I1‘, ‘標籤‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘J1‘, ‘頭像‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘K1‘, ‘關注時間‘);$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A1‘, 1111);// for($i = 0; $i < count($result); $i++){    // $j = $i + 2;    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A‘.$j, $result[$i][‘id‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘B‘.$j, $result[$i][‘openid‘]);      // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘C‘.$j, $result[$i][‘nickname‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘D‘.$j, $result[$i][‘sex‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘E‘.$j, $result[$i][‘country‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘F‘.$j, $result[$i][‘province‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘G‘.$j, $result[$i][‘city‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘H‘.$j, $result[$i][‘scene‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘I‘.$j, $result[$i][‘tagid‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘J‘.$j, $result[$i][‘headimgurl‘]);    // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘K‘.$j, date("Y-m-d H:i:s", $result[$i][‘subscribe‘]));    // // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘L‘.$j, $result[$i][‘so2‘]);    // // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘M‘.$j, $result[$i][‘pm10‘]);    // // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘N‘.$j, $result[$i][‘pm2_5‘]);    // // $objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘N‘.$j, $result[$i][‘quality‘]);// }// Rename worksheet$objPHPExcel->getActiveSheet()->setTitle(‘userlist‘);// Set active sheet index to the first sheet, so Excel opens this as the first sheet$objPHPExcel->setActiveSheetIndex(0);// Redirect output to a client’s web browser (Excel2007)$filename = date("YmdHis",time());header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);header(‘Content-Disposition: attachment;filename="‘.$filename.‘.xls"‘);header(‘Cache-Control: max-age=0‘);// If you‘re serving to IE 9, then the following may be neededheader(‘Cache-Control: max-age=1‘);// If you‘re serving to IE over SSL, then the following may be neededheader (‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘); // Date in the pastheader (‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s‘).‘ GMT‘); // always modifiedheader (‘Cache-Control: cache, must-revalidate‘); // HTTP/1.1header (‘Pragma: public‘); // HTTP/1.0// $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);$objWriter->save(‘php://output‘);

 

 

2. 上傳

2.1 upload.html

<html lang="zh-cn"><head><meta charset="utf-8"/></head><body>    <form action="upload.php" enctype="multipart/form-data" method="post">    上傳檔案:<input type="file" name="upfile" /><br>     <input type="submit" value="上傳" />    </form> </body>

2.2 upload.php

<?phpheader("Content-type: text/html; charset=utf-8");  require("Excel/PHPExcel.php");// var_dump($_FILES);   if(@is_uploaded_file($_FILES[‘upfile‘][‘tmp_name‘])){        $upfile=$_FILES["upfile"];  //擷取數組裡面的值         $name=$upfile["name"];//上傳檔案的檔案名稱         $type=$upfile["type"];//上傳檔案的類型         $size=$upfile["size"];//上傳檔案的大小         $tmp_name=$upfile["tmp_name"];//上傳檔案的臨時存放路徑           move_uploaded_file($tmp_name,dirname(__FILE__).$name);//將上傳到伺服器臨時檔案夾的檔案重新移動到新位置        $file_name=dirname(__FILE__).$name;        $error=$upfile["error"];//上傳後系統返回的值         if($error==0){             echo "檔案上傳成功啦!<br>";        }else{            echo "上傳失敗";        }        // print($file_name);        $objReader = \PHPExcel_IOFactory::createReader(‘Excel5‘);//建立讀取執行個體        /*         * log()//方法參數         * $file_name excal檔案的儲存路徑         */        $objPHPExcel = $objReader->load($file_name,$encode=‘utf-8‘);//負載檔案        $sheet = $objPHPExcel->getSheet(0);//取得sheet(0)表        $highestRow = $sheet->getHighestRow(); // 取得總行數        $highestColumn = $sheet->getHighestColumn(); // 取得總列數        for($i=1;$i<=10;$i++)        {            $username = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();            $password = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();            echo "帳號:".$username." 密碼:".$password."<br>";                  }    }?> 

關注公眾號 方倍工作室,回複 1267

 

使用phpexcel上傳下載excel檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.