search
HomeBackend DevelopmentPHP TutorialPHPExcel reads excel and imports mysql database code_PHP tutorial
PHPExcel reads excel and imports mysql database code_PHP tutorialJul 13, 2016 pm 05:06 PM
excelmysqlphpexcelintroducecodeimportdatabasearticleread

This article will introduce to you a PHPExcel code implementation for reading excel and importing it into the database. Friends who need to know more can refer to it. What we introduce here is to create a mysql connection after reading the table, and then save it to the mysql database.

PHPExcel is a very powerful MS Office Excel document generation class library. When you need to output data in a more complex format, PHPExcel is a good choice. However, its usage is relatively cumbersome

The code is as follows
 代码如下 复制代码

    set_time_limit(20000);
    ini_set('memory_limit','-1');
    require_once './PHPExcel.php';
    require_once './PHPExcel/IOFactory.php';
    require_once './PHPExcel/Reader/Excel5.php';
    
    //使用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);
    
    $objReader = new PHPExcel_Reader_Excel5(); //use excel2007
    $objPHPExcel = $objReader->load('bks.xls'); //指定的文件
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数
    
    for($j=1;$j     {
    
    $student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列学号
    $name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名
    $gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid
    }
    //将获取的excel内容插入到数据库
    $stmt->execute();
?>

Copy code
set_time_limit(20000);
ini_set('memory_limit','-1');
​ require_once './PHPExcel.php';
​ require_once './PHPExcel/IOFactory.php';
​ require_once './PHPExcel/Reader/Excel5.php';
         
//Use pdo to connect to the database
$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 "Connection failed".$e->getMessage();
}
//pdo binding parameter operation
$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);
         
$objReader = new PHPExcel_Reader_Excel5(); //use excel2007
$objPHPExcel = $objReader->load('bks.xls'); //Specified file
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // Get the total number of rows
$highestColumn = $sheet->getHighestColumn(); // Get the total number of columns
         
for($j=1;$j {
         
$student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//The first column of student number
$name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//Second column name
$gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//The third column gid
}
//Insert the obtained excel content into the database
$stmt->execute();
?>

http://www.bkjia.com/PHPjc/630718.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/630718.html
TechArticle
This article will introduce to you a PHPExcel code implementation for reading excel and importing it into the database. Friends who need to know more can For reference, what we introduce here is to create a mysql connection after reading the table...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何使用pandas正确读取txt文件如何使用pandas正确读取txt文件Jan 19, 2024 am 08:39 AM

如何使用pandas正确读取txt文件,需要具体代码示例Pandas是一个广泛使用的Python数据分析库,它可以用于处理各种各样的数据类型,包括CSV文件、Excel文件、SQL数据库等。同时,它也可以用于读取文本文件,例如txt文件。但是,在读取txt文件时,我们有时会遇到一些问题,例如编码问题、分隔符问题等。本文将介绍如何使用pandas正确读取txt

使用pandas读取txt文件的实用技巧使用pandas读取txt文件的实用技巧Jan 19, 2024 am 09:49 AM

使用pandas读取txt文件的实用技巧,需要具体代码示例在数据分析和数据处理中,txt文件是一种常见的数据格式。使用pandas读取txt文件可以快速、方便地进行数据处理。本文将介绍几种实用的技巧,以帮助你更好的使用pandas读取txt文件,并配以具体的代码示例。读取带有分隔符的txt文件使用pandas读取带有分隔符的txt文件时,可以使用read_c

Java 中使用 OpenCSV 读取和写入 CSV 文件的示例Java 中使用 OpenCSV 读取和写入 CSV 文件的示例Dec 20, 2023 pm 01:39 PM

Java中使用OpenCSV读取和写入CSV文件的示例CSV(Comma-SeparatedValues)指的是以逗号分隔的数值,是一种常见的数据存储格式。在Java中,OpenCSV是一个常用的工具库,用于读取和写入CSV文件。本文将介绍如何使用OpenCSV来实现读取和写入CSV文件的示例。引入OpenCSV库首先,需要引入OpenCSV库到

Pandas读取网页数据的实用方法Pandas读取网页数据的实用方法Jan 04, 2024 am 11:35 AM

Pandas读取网页数据的实用方法,需要具体代码示例在数据分析和处理过程中,我们经常需要从网页中获取数据。而Pandas作为一种强大的数据处理工具,提供了方便的方法来读取和处理网页数据。本文将介绍几种常用的Pandas读取网页数据的实用方法,并附上具体的代码示例。方法一:使用read_html()函数Pandas的read_html()函数可以直接从网页中读

Pandas使用教程:读取JSON文件的快速入门Pandas使用教程:读取JSON文件的快速入门Jan 13, 2024 am 10:15 AM

快速入门:Pandas读取JSON文件的方法,需要具体代码示例引言:在数据分析和数据科学领域,Pandas是一个重要的Python库之一。它提供了丰富的功能和灵活的数据结构,能够方便地对各种数据进行处理和分析。在实际应用中,我们经常会遇到需要读取JSON文件的情况。本文将介绍如何使用Pandas来读取JSON文件,并附上具体的代码示例。一、Pandas的安装

PHP读取Excel文件方法及常见问题解答PHP读取Excel文件方法及常见问题解答Jun 09, 2023 am 11:41 AM

PHP读取Excel文件方法及常见问题解答Excel是一种非常普遍的电子表格文件格式,很多业务和数据都存放在Excel文件中。在开发过程中,如果需要将Excel文件中的数据导入系统中,就需要使用PHP读取Excel文件。本文将介绍PHP读取Excel文件的方法及常见问题解答。一、PHP读取Excel文件方法1.使用PHPExcel类库PHPExcel是一个P

完全指南:如何使用php扩展PHPExcel处理Excel文件完全指南:如何使用php扩展PHPExcel处理Excel文件Jul 28, 2023 pm 10:01 PM

完全指南:如何使用PHP扩展PHPExcel处理Excel文件引言:在处理大量数据和统计分析时,Excel文件经常被用作数据存储和交换的一种常见格式。使用PHP扩展PHPExcel,我们可以轻松地读取、写入和修改Excel文件,从而有效地处理Excel数据。本文将介绍如何使用PHP扩展PHPExcel来处理Excel文件,并提供代码示例。一、安装PHPExc

PHP开发:使用 PHPExcel 处理 Excel 文件PHP开发:使用 PHPExcel 处理 Excel 文件Jun 15, 2023 pm 03:45 PM

随着数字化时代的到来,数据已经成为了我们日常生活和工作中最重要的一部分,而Excel文件则成为数据处理的重要工具之一。相信很多PHP开发者也会在工作中经常遇到使用Excel文件进行数据处理和操作的情况。本文将为大家介绍使用PHPExcel库来处理Excel文件的方法和注意事项。什么是PHPExcel?PHPExcel是一个PHP类

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.