php导入导出excel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zch3210/article/details/86078626

首先到https://github.com/PHPOffice/PHPExcel下载PHPExcel

在根目录下创建read.php和write.php导出和导入excel

read.php

<?php

require('Classes/PHPExcel.php');

$inputFileName = 'test.xlsx';

try {
    //判断上传文件类型
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    //去除表格格式
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($inputFileName, $encode = 'utf-8');
    $array = $objPHPExcel->getSheet(0)->toArray();
    var_dump($array);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

导出test.excel表格

write.php

<?php
require('Classes/PHPExcel.php');

// 创建Excel文件对象
$objPHPExcel = new PHPExcel();
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
//清除缓冲区
ob_end_clean();
//根据excel坐标,添加数据
$objPHPExcel->getActiveSheet()->setCellValue("A1","姓名")->setCellValue("B1","分数")->setCellValue("C1","班级");

//文件类型
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:attachment;filename='.date('Y-m-d H:i:s',time()).'.xlsx');
//编码
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$objWriter->save('php://output');

 生成excel文件

//写入word https://www.helloweba.net/php/568.html

猜你喜欢

转载自blog.csdn.net/zch3210/article/details/86078626