PHP 如何利用phpexcel导入数据库
2015-01-24信息快讯网
以下是对PHP中利用phpexcel导入数据库的实现代码进行了介绍,需要的朋友可以过来参考下
废话不多说,直接上代码吧
<?php
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); //设置时间
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.jb51.net/../Classes/');//设置环境变量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //这个是读 xls的
$inputFileType = 'Excel2007';//这个是计xlsx的
//$inputFileName = './sampleData/example2.xls';
$inputFileName = './sampleData/book.xlsx';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
/*
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得总行数
$highestColumn = $sheet->getHighestColumn(); //取得总列
*/
$objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
$highestRow = $objWorksheet->getHighestRow();//取得总列数
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br />";
$headtitle=array();
for ($row = 1;$row <= $highestRow;$row++)
{
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++)
{
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
$info = array(
'word1'=>"$strs[0]",
'word2'=>"$strs[1]",
'word3'=>"$strs[2]",
'word4'=>"$strs[3]",
);
//在这儿,你可以连接,你的数据库,写入数据库了
print_r($info);
echo '<br />';
}
?>
php强制文件下载而非在浏览器打开的自定义函数分享
PHP获取指定函数定义在哪个文件中以及其所在的行号实例
Windows和Linux中php代码调试工具Xdebug的安装与配置详解
PHP中把stdClass Object转array的几个方法
PHP批量检测并去除文件BOM头代码实例
PHP异常Parse error: syntax error, unexpected T_VAR错误解决方法
php加速器eAccelerator的配置参数、API详解
PHP中使用FFMPEG获取视频缩略图和视频总时长实例
php数组查找函数in_array()、array_search()、array_key_exists()使用实例
PHP函数addslashes和mysql_real_escape_string的区别
通过dbi使用perl连接mysql数据库的方法
PHP扩展模块Pecl、Pear以及Perl的区别
php导出excel格式数据问题
php导出word文档与excel电子表格的简单示例代码
php不使用插件导出excel的简单方法
Drupal读取Excel并导入数据库实例
PHP中判断变量为空的几种方法分享
php实现利用phpexcel导出数据
提高PHP性能的编码技巧以及性能优化详细解析
PHP实现读取一个1G的文件大小
一致性哈希算法以及其PHP实现详细解析
PHP如何利用P3P实现跨域
php var_export与var_dump 输出的不同
浅析虚拟主机服务器php fsockopen函数被禁用的解决办法
基于php中使用excel的简单介绍
通过table标签,PHP输出EXCEL的实现方法
php打开文件fopen函数的使用说明
浅析PHP安装扩展mcrypt以及相关依赖项(PHP安装PECL扩展的方法)
深入解析fsockopen与pfsockopen的区别
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍