欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

exls导入数据库 php_PHP读取excel文件并导入数据库

发布时间:2025/3/12 php 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 exls导入数据库 php_PHP读取excel文件并导入数据库 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

PHPExcel是一个PHP类库,用来帮助我们简单、高效实现从Excel读取Excel的数据和导出数据到Excel。下面是PHPExcel读取的使用教程:

1.首先下载PHPExcel

2.下载好文件,解压可以得到如下文件:

为了使用方便,我们可以在根目录创建一个文件夹来存放我们所要读取的excel和读取excel的PHP文件,里面写的信息如下:

Read_Excel

header("content-type:text/html;charset=utf8");

error_reporting(E_ALL);

date_default_timezone_set('Asia/ShangHai');

include_once('../Classes/PHPExcel/IOFactory.php');//包含类文件

$filename = "./xx.xlsx";//要读取的excel文件

if (!file_exists($filename)) {

exit("not found.\n");

}

$reader = PHPExcel_IOFactory::createReader('Excel2007'); //设置以Excel5格式(Excel97-2003工作簿)

$PHPExcel = $reader->load($filename); // 载入excel文件

$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表

$highestRow = $sheet->getHighestRow(); // 取得总行数

$highestColumm = $sheet->getHighestColumn(); // 取得总列数

/** 循环读取每个单元格的数据 */

$conn=@mysql_connect("localhost","root","数据库密码") or die('连接失败');//连接服务器

mysql_select_db("数据库",$conn) or die('连接数据库失败');//选择数据库

mysql_query("SET NAMES 'utf8'");//设置字符集

/*for ($row = 1; $row <= $highestRow; $row++)

{//行数是以第1行开始

for ($column = 'A'; $column <= $highestColumm; $column++)

{//列数是以A列开始

echo $sheet->getCell($column.$row)->getValue().' ';

}

echo '';

}*/

for ($row = 2; $row <= $highestRow; $row++)

{//行数是以第2行开始

$A=$sheet->getCell('A'.$row)->getValue();

$B=$sheet->getCell('B'.$row)->getValue();

$C=$sheet->getCell('C'.$row)->getValue();//需要读取的哪些列

$sql=" ";//sql语句 建议使用update插入语句

//echo $A,$B.'';

echo $row.$A.'';//只是方便看那里插入有问题

mysql_query($sql) or die('no ');

}

echo $row;//出错的位置,方便查错

?>

总结

以上是生活随笔为你收集整理的exls导入数据库 php_PHP读取excel文件并导入数据库的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。