300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > php怎么读取excel里的数据类型 php读取excel表格数据-对PHPExcel一些简单的理解 及

php怎么读取excel里的数据类型 php读取excel表格数据-对PHPExcel一些简单的理解 及

时间:2019-04-03 02:01:08

相关推荐

php怎么读取excel里的数据类型 php读取excel表格数据-对PHPExcel一些简单的理解 及

php读取excel,excel下多个个工作表,该怎么读取

php读取excel,excel下多个个工作表的方法:

1、利用PHPExcelReader来完成多个excel的读取。

2、PHPExcel比较强大,能够将内存中的数据输出成Excel文件,同时还能够对Excel做各种操作,下面主要介绍下如何使用PHPExcel进行Excel 格式(.xlsx)文件的读取。

3、下载PHPExcel后保存到自己的类文件目录中,然后使用以下代码可以打开Excel (xlsx)格式的文件:

require_once '/libs/PHPExcel-1.8.0/Classes/PHPExcel.php'; //修改为自己的目录

echo '

TEST PHPExcel 1.8.0: read xlsx file

';

$objReader = PHPExcel_IOFactory::createReaderForFile($filename);

$objPHPExcel = $objReader->load($filename);

$objPHPExcel->setActiveSheetIndex(1);

$date = $objPHPExcel->getActiveSheet()->getCell('A16')->getValue();

输出$date变量就能够看到文件中的内容了。

PHP 用PHPExcel往数据库导入大量数据

PHPExcel

PHPExcel是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel (BIFF) .xls, Excel (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函数

functionreadExcel($path){

//引入PHPExcel类库

include'Classes/PHPExcel.php';

include'Classes/PHPExcel/IOFactory.php';

$type='Excel5';//设置为Excel5代表支持或以下版本,

Excel代表版

$xlsReader=\PHPExcel_IOFactory::createReader($type);

$xlsReader->setReadDataOnly(true);

$xlsReader->setLoadSheetsOnly(true);

$Sheets=$xlsReader->load($path);

//开始读取上传到服务器中的Excel文件,返回一个

二维数组

$dataArray=$Sheets->getSheet(0)->

toArray();

return$dataArray;

}

PHPexcel 读取excel中的值是空的

php有个PHPExcel扩展,是可以实现你的要求的。

我这里有个可以读取多个工作薄的自定义excel类,试试看:

/**

*excel.class.php

*/

class Excel

{

/**

* 从excel文件中取得所有数据。并转换成指定编码格式。

* $toCode 表示需要转换成的编码格式,目前扩充了utf8,gbk2312,html三种格式。

* @return 返回二维数组。

*/

static function getDataFromExl($filePath,$toCode = "utf8")

{

$fh = @fopen($filePath,'rb');

if( !$fh || filesize($filePath)==0 )

{

return -1; //文件不可读或者为空

}

$fc = fread( $fh, filesize($filePath) );

@fclose($fh);

if( strlen($fc) < filesize($filePath) )

{

return -2; //读取错误

}

$exc = new ExcelFileParser();

$res = $exc->ParseFromString($fc);

$ws_number = count($exc->worksheet['name']);//取得工作薄数量

if( $ws_number < 1 )

{

return -3;

}

for ($ws_n = 0; $ws_n < $ws_number; $ws_n )

{

$ws = $exc -> worksheet['data'][$ws_n];

$data = $ws['cell'];

foreach($data as $k=>$v) //一行数据

{

$row = null;

foreach($v as $a=>$d) //一行数据的一个字节

{

$value = null;

if(count($d) == 1)

{

continue;

}

if($d['type'] == 0) //如果是字符类型则转换成为指定编码格式

{

$ind = $d['data'];

if( $exc->sst['unicode'][$ind] ) //返回数据编码格式

{

switch($toCode)

{

case "utf8":

$s = Strings::uc2utf8($exc->sst['data'][$ind]);

break;

case "gbk":

$s = Strings::uc2gbk($exc->sst['data'][$ind]);

break;

case "html":

$s = Strings::uc2html($exc->sst['data'][$ind]);

break;

default:

$s = Strings::uc2utf8($exc->sst['data'][$ind]);

break;

}

}

else

{

$s = $exc->sst['data'][$ind];

}

if( strlen(trim($s))==0 || $s === null )

{

$value = '';

}

else

{

$value = $s;

}

}

elseif($d['type'] == 3)

{

$time_list = explode('.', $d['data']);

$time_format = $time_list[2].'-'.$time_list[0].'-'.$time_list[1];

$timestamp = strtotime($time_format);

$value = date("Y-m-d H:i:s", $timestamp);

}

else

{

$value = $d['data'];

}

$row[$a] = $value;

}

$recordList[] = $row;

}

}

return $recordList;

}

}

require_once('./excel.class.php');

$emailData = Excel::getDataFromExl($_FILES['file_name']['tmp_name']);

对PHPExcel一些简单的理解 及怎么读取单元格数据

php有个PHPExcel扩展,是可以实现你的要求的。

我这里有个可以读取多个工作薄的自定义excel类,试试看:

/**

*excel.class.php

*/

class Excel

{

/**

* 从excel文件中取得所有数据。并转换成指定编码格式。

* $toCode 表示需要转换成的编码格式,目前扩充了utf8,gbk2312,html三种格式。

* @return 返回二维数组。

*/

static function getDataFromExl($filePath,$toCode = "utf8")

{

$fh = @fopen($filePath,'rb');

if( !$fh || filesize($filePath)==0 )

{

return -1; //文件不可读或者为空

}

$fc = fread( $fh, filesize($filePath) );

@fclose($fh);

if( strlen($fc) < filesize($filePath) )

{

return -2; //读取错误

}

$exc = new ExcelFileParser();

$res = $exc->ParseFromString($fc);

$ws_number = count($exc->worksheet['name']);//取得工作薄数量

if( $ws_number < 1 )

{

return -3;

}

for ($ws_n = 0; $ws_n < $ws_number; $ws_n )

{

$ws = $exc -> worksheet['data'][$ws_n];

$data = $ws['cell'];

foreach($data as $k=>$v) //一行数据

{

$row = null;

foreach($v as $a=>$d) //一行数据的一个字节

{

$value = null;

if(count($d) == 1)

{

continue;

}

if($d['type'] == 0) //如果是字符类型则转换成为指定编码格式

{

$ind = $d['data'];

if( $exc->sst['unicode'][$ind] ) //返回数据编码格式

{

switch($toCode)

{

case "utf8":

$s = Strings::uc2utf8($exc->sst['data'][$ind]);

break;

case "gbk":

$s = Strings::uc2gbk($exc->sst['data'][$ind]);

break;

case "html":

$s = Strings::uc2html($exc->sst['data'][$ind]);

break;

default:

$s = Strings::uc2utf8($exc->sst['data'][$ind]);

break;

}

}

else

{

$s = $exc->sst['data'][$ind];

}

if( strlen(trim($s))==0 || $s === null )

{

$value = '';

}

else

{

$value = $s;

}

}

elseif($d['type'] == 3)

{

$time_list = explode('.', $d['data']);

$time_format = $time_list[2].'-'.$time_list[0].'-'.$time_list[1];

$timestamp = strtotime($time_format);

$value = date("Y-m-d H:i:s", $timestamp);

}

else

{

$value = $d['data'];

}

$row[$a] = $value;

}

$recordList[] = $row;

}

}

return $recordList;

}

}

require_once('./excel.class.php');

$emailData = Excel::getDataFromExl($_FILES['file_name']['tmp_name']);

php读取excel,excel下多个个工作表,该怎么读取

php有个PHPExcel扩展,是可以实现你的要求的。

我这里有个可以读取多个工作薄的自定义excel类,试试看:

/**

*excel.class.php

*/

class Excel

{

/**

* 从excel文件中取得所有数据。并转换成指定编码格式。

* $toCode 表示需要转换成的编码格式,目前扩充了utf8,gbk2312,html三种格式。

* @return 返回二维数组。

*/

static function getDataFromExl($filePath,$toCode = "utf8")

{

$fh = @fopen($filePath,'rb');

if( !$fh || filesize($filePath)==0 )

{

return -1; //文件不可读或者为空

}

$fc = fread( $fh, filesize($filePath) );

@fclose($fh);

if( strlen($fc) < filesize($filePath) )

{

return -2; //读取错误

}

$exc = new ExcelFileParser();

$res = $exc->ParseFromString($fc);

$ws_number = count($exc->worksheet['name']);//取得工作薄数量

if( $ws_number < 1 )

{

return -3;

}

for ($ws_n = 0; $ws_n < $ws_number; $ws_n )

{

$ws = $exc -> worksheet['data'][$ws_n];

$data = $ws['cell'];

foreach($data as $k=>$v) //一行数据

{

$row = null;

foreach($v as $a=>$d) //一行数据的一个字节

{

$value = null;

if(count($d) == 1)

{

continue;

}

if($d['type'] == 0) //如果是字符类型则转换成为指定编码格式

{

$ind = $d['data'];

if( $exc->sst['unicode'][$ind] ) //返回数据编码格式

{

switch($toCode)

{

case "utf8":

$s = Strings::uc2utf8($exc->sst['data'][$ind]);

break;

case "gbk":

$s = Strings::uc2gbk($exc->sst['data'][$ind]);

break;

case "html":

$s = Strings::uc2html($exc->sst['data'][$ind]);

break;

default:

$s = Strings::uc2utf8($exc->sst['data'][$ind]);

break;

}

}

else

{

$s = $exc->sst['data'][$ind];

}

if( strlen(trim($s))==0 || $s === null )

{

$value = '';

}

else

{

$value = $s;

}

}

elseif($d['type'] == 3)

{

$time_list = explode('.', $d['data']);

$time_format = $time_list[2].'-'.$time_list[0].'-'.$time_list[1];

$timestamp = strtotime($time_format);

$value = date("Y-m-d H:i:s", $timestamp);

}

else

{

$value = $d['data'];

}

$row[$a] = $value;

}

$recordList[] = $row;

}

}

return $recordList;

}

}

require_once('./excel.class.php');

$emailData = Excel::getDataFromExl($_FILES['file_name']['tmp_name']);

php怎么读取excel里的数据类型 php读取excel表格数据-对PHPExcel一些简单的理解 及怎么读取单元格数据...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。