如何将xslx文件插入mysql数据库? [英] How do insert an xslx file into mysql database?

查看:124
本文介绍了如何将xslx文件插入mysql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了PHPExcel,我输入了一个基本代码,当我运行它时,它只给我一个空白页面。有什么建议吗?



我尝试过:



I've downloaded PHPExcel and I typed a basic code which gives me nothing but blank page when I run it. Any advices?

What I have tried:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
	<?php
     $conn = MySQLi( 'localhost', 'omri', '1234', 'omri' );
        if( $conn === false ) {
            echo "Could not connect.\n";
            die( print_r( sqlsrv_errors(), true));
        }
        include "Classes/PHPExcel.php";
        include "Classes/PHPExcel/IOFactory.php";
        //load excel file using PHPExcel's IOfactory
        $excel = PHPExcel_IOFactory::load('omri.xlsx');
        //set active sheet to first sheet
        $excel->setActiveSheetIndex(0);
        echo "<table>";
        echo "
                <tr>
                    <th>Name</th>
                    <th>ID</th>
                    <th>Citizenship</th>
                    <th>Expires</th>
                </tr>
                ";
        //first row of data series
        $i = 1 ;
        //loop until the end of data series(cell contains empty string)
        while($excel->getActiveSheet()->getcell('A'.$i)->getValue() != ""){
            //get cells value
            $name     = $excel->getactiveSheet()->getCell('A'.$i)->getValue();
            $id  = $excel->getactiveSheet()->getCell('B'.$i)->getValue();
            $citizenship = $excel->getactiveSheet()->getCell('C'.$i)->getValue();
            $expires    = $excel->getactiveSheet()->getCell('D'.$i)->getValue();
                $q = "INSERT INTO omri (name,id,citizenship,expires) VALUES ('$name','$id','$citizenship','$expires')";
                $d = sqlsrv_query($conn,$q);
            //echo
            echo "
                <tr>
                    <td>".$name."</td>
                    <td>".$id."</td>
                    <td>".$citizenship."</td>
                    <td>".$expires."</td>
                </tr>
            ";
            //and DON'T FORGET to increment the row pointer ($i)
            $i++;
        }
        echo "</table>";
        //echo error
        if( $d === false ) {
                if( ($errors = sqlsrv_errors() ) != null) {
                    foreach( $errors as $error ) {
                        echo "</br>SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
                        echo "code: ".$error[ 'code']."<br />";
                        echo "message: ".$error[ 'message']."<br />";
                    }
                }
            }
	?>
</body>
</html>

推荐答案

conn = MySQLi('localhost','omri','1234',' omri');
if(
conn = MySQLi( 'localhost', 'omri', '1234', 'omri' ); if(


conn === false){
echo无法连接。\ n;
die(print_r(sqlsrv_errors(),true));
}
包括Classes / PHPExcel.php;
包括Classes / PHPExcel / IOFactory.php;
//使用PHPExcel的IOfactory加载excel文件
conn === false ) { echo "Could not connect.\n"; die( print_r( sqlsrv_errors(), true)); } include "Classes/PHPExcel.php"; include "Classes/PHPExcel/IOFactory.php"; //load excel file using PHPExcel's IOfactory


excel = PHPExcel_IOFactory :: load('omri.xlsx');
//将活动工作表设置为第一张工作表
excel = PHPExcel_IOFactory::load('omri.xlsx'); //set active sheet to first sheet


这篇关于如何将xslx文件插入mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆