如何通过PHP将excel的数据导入MySQL中
p hp使用phpExcelReader导入excel的方法 phpExcelReader是专门用来读取文件的。返回一个数组,包含表的所有内容。 该 class 使用的方法可以参考网站下载回来的压缩档中的 example.php。 有几点要需要注意: 1、reader.php 中:将 require_once 'Spreadsheet/E
php使用phpExcelReader导入excel的方法
phpExcelReader是专门用来读取文件的。返回一个数组,包含表格的所有内容。
该 class 使用的方法可以参考网站下载回来的压缩档中的 example.php。
有几点要需要注意:
1、reader.php 中:将 require_once 'Spreadsheet/Excel/Reader/OLERead.php';改为 require_once 'oleread.inc';
2、example.php 中:修改 $data->setOutputEncoding('CP1251');为 $data->setOutputEncoding('CP936');或者是$data->setOutputEncoding('gbk');
3、修改 nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col]));为 $table_output[$sheet] .= nl2br(htmlspecialchars($data->sheets[$sheet]['cells'][$row][$col]));
不然中文会有问题。
繁体的话可以修改为CP950、日文是CP932,具体可参考codepage说明。
===========================================================
require_once 'excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('gbk'); //设置编码
$data->read('徐州.xls'); //指定文件
for ( $i = 1; $i sheets[0]['numRows']; $i++) {
for ($j = 1; $j sheets[0]['numCols']; $j++) {
echo $data->sheets[0]['cells'][$i][$j]." | ";
}
echo "
";
}
-------------------------------------------------------------------------------------------------------
如何通过PHP将excel的数据导入MySQL中
在开发PHP程序时,很多时候我们会有将数据批量导入数据库的需求,如学生信息批量添加到数据库中,而事先用excel编排好,对excel实行操作,便是我们比较常用的选择方式。
在对excel的操作中,phpExcelReade便是很多人的共同选择。在具体实现中,我们可以以文件上传方式将excel文件上传到服务器中的某个位置,通过以下操作将excel中的数据导入到数据库后,在将上传的文件删除即可。
代码如下:
$dir=dirname(__FILE__); //获取当前脚本的绝对路径
$dir=str_replace("\","/",$dir)."/";
$filename='uploadFile.xls'; //可以定义一个上传后的文件名称
$result=move_uploaded_file($_FILES['upload'] ['tmp_name'],$dir.$filename);//假如上传到当前目录下
if($result) //如果上传文件成功,就执行导入excel操作
{
require_once 'phpExcelReader/Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');//设置在页面中输出的编码方式,而不是utf8
// 该方法会自动判断上传的文件格式,不符合要求会显示错误提示信息(错误提示信息在该方法内部)。
$data->read("$filename"); //读取上传到当前目录下名叫$filename的文件
error_reporting(E_ALL ^ E_NOTICE);
//如果excel表带标题,则从$i=2开始,去掉excel表中的标题部分(要将$i
for ($i = 2; $i sheets[0]['numRows']; $i++)
{
$sql = "INSERT INTO user (stuid,class,name,sex,classNum,tel,addr,remark) VALUES('".
$data->sheets[0]['cells'][$i][1]."','". //学号
$data->sheets[0] ['cells'][$i][2]."','". //班级
$data->sheets[0]['cells'] [$i][3]."','". //姓名
$data->sheets[0]['cells'][$i] [4]."','". //性别
$data->sheets[0]['cells'][$i] [5]."','". //班内序号
$data->sheets[0]['cells'][$i] [6]."','". //联系电话
$data->sheets[0]['cells'][$i] [7]."','". //联系地址
$data->sheets[0]['cells'][$i][8]."')"; //附注
$db->query($sql);
$insert_info.= " $sql
n"; //可以用来显示数据插入的信息
}
$totalNums=$data->sheets[0]['numRows']-2;//求出导入的总数据条数(这里是减去2,才会得到去除标题后的总数据)
//echo "导入成功!";
unlink("$filename"); //删除上传的excel文件
}
else
{
$errmsg="上传失败";
}
=======================================================================
zendframework上传文件
此方法适用于1.6或更高版本。
1.先在view页面写html代码。
涓

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
