使用phpexcel上传下载excel文件

时间:2022-07-26 23:04:35

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 needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('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

<?php

header("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>";
          
        }
    }

?> 

效果图

使用phpexcel上传下载excel文件

下载地址

关注公众号 方倍工作室,回复 1267