很多站长朋友们都不太清楚php-excel的读取,今天小编就来给大家整理php-excel的读取,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php怎么读取excel 文件数据并输出 2、 php读取excel,excel下多个个工作表,该怎么读取 3、 php 读取excel 4、 PHP远程读取excel文件,怎么读取 5、 如何用PHPExcel读取超大excel文件 php怎么读取excel 文件数据并输出PHPExcel
PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等。
PHP读取示例代码
//获取上传的excel临时文件
$path = $_FILES["file"]["tmp_name"];
//将临时文件移动当前目录,可自定义存储位置
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
//将获取在服务器中的Excel文件,此处为上传文件名
$path = $_FILES["file"]["name"];
//调用readExcel函数返回一个
二维数组
$exceArray = readExcel($path);
//创建一个读取
excel函数
function readExcel($path){
//引入PHPExcel类库
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';
$type = 'Excel5';//设置为Excel5代表支持2003或以下版本,
Excel2007代表2007版
$xlsReader = \PHPExcel_IOFactory::createReader($type);
$xlsReader->setReadDataOnly(true);
$xlsReader->setLoadSheetsOnly(true);
$Sheets = $xlsReader->load($path);
//开始读取上传到服务器中的Excel文件,返回一个
二维数组
$dataArray = $Sheets->getSheet(0)->
toArray();
return $dataArray;
}
php读取excel,excel下多个个工作表,该怎么读取php读取excel,excel下多个个工作表的方法:
1、利用PHPExcelReader来完成多个excel的读取。
2、PHPExcel比较强大,能够将内存中的数据输出成Excel文件,同时还能够对Excel做各种操作,下面主要介绍下如何使用PHPExcel进行Excel 2007格式(.xlsx)文件的读取。
3、下载PHPExcel后保存到自己的类文件目录中,然后使用以下代码可以打开Excel 2007(xlsx)格式的文件:
require_once '/libs/PHPExcel-1.8.0/Classes/PHPExcel.php'; //修改为自己的目录
echo '<p>TEST PHPExcel 1.8.0: read xlsx file</p>';
$objReader = PHPExcel_IOFactory::createReaderForFile($filename);
$objPHPExcel = $objReader->load($filename);
$objPHPExcel->setActiveSheetIndex(1);
$date = $objPHPExcel->getActiveSheet()->getCell('A16')->getValue();
输出$date变量就能够看到文件中的内容了。
php 读取excel有一个简便的方法 将数据输出到网页的table标签里面 然后改变header头为excel
header ( "Content-type:application/vnd.ms-excel" );
header ( "Content-Disposition:attachment;filename=文件名称.xls" );
正式点的
前端使用bootstrapTable插件导出excel
后端PHP使用PHPEXCEL插件
PHP远程读取excel文件,怎么读取PHPExcel 通过 PHPExcel_Shared_OLERead 类的 read 方法读取文件
但 read 方法里使用了 is_readable 函数来确认文件是否存在,而 is_readable 不能作用于 url
所以不可直接远程读取
但若绕过 is_readable 函数的话,就是可以的
public function read($sFileName)
{
// Check if file exists and is readable
if(!is_readable($sFileName)) {
throw new Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
}
// Get the file data
$this->data = file_get_contents($sFileName);
如何用PHPExcel读取超大excel文件上一篇文章介绍了php-excel-reader读取excel文件的方法,因为需要,将excel这样的数据:新建数据库表如下:-- 数据库: `alumni`-- 表的结构 `alumni`CREATE TABLE IF NOT EXISTS `alumni` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`gid` varchar(20) DEFAULT NULL COMMENT '档案编号',`student_no` varchar(20) DEFAULT NULL COMMENT '学号',`name` varchar(32) DEFAULT NULL,PRIMARY KEY (`id`),KEY `gid` (`gid`),KEY `name` (`name`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;导入后数据库结果如下:php源码如下: 复制代码 代码如下: <?php header("Content-Type:text/html;charset=utf-8"); require_once 'excel_reader2.php'; set_time_limit(20000); ini_set("memory_limit","2000M"); //使用pdo连接数据库 $dsn = "mysql:host=localhost;dbname=alumni;"; $user = "root"; $password = ""; try{ $dbh = new PDO($dsn,$user,$password); $dbh->query('set names utf8;'); }catch(PDOException $e){ echo "连接失败".$e->getMessage(); } //pdo绑定参数操作 $stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) "); $stmt->bindParam(":gid", $gid,PDO::PARAM_STR); $stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR); $stmt->bindParam(":name", $name,PDO::PARAM_STR); //使用php-excel-reader读取excel内容 $data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('UTF-8'); $data->read("stu.xls"); for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { for ($j = 1; $j <= 3; $j++) { $student_no = $data->sheets[0]['cells'][$i][1]; $name = $data->sheets[0]['cells'][$i][2]; $gid = $data->sheets[0]['cells'][$i][3]; } //将获取的excel内容插入到数据库 $stmt->execute(); } echo "执行成功"; echo "最后插入的ID:".$dbh->lastInsertId(); ?> 考虑到excel的量比较大,使用了PDO的绑定操作!
关于php-excel的读取的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php-excel的读取 php 读取excel的详细内容...