致命错误:未捕获错误:在字符串

时间:2017-07-07 03:28:26

标签: php mysql excel

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hello";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
} 

echo "connected";

use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

// Include Spout library 
include ('spout-2.4.3\src\Spout\Autoloader\autoload.php');

// check file name is not empty
if (!empty($_FILES['file']['name'])) {

    // Get File extension eg. 'xlsx' to check file is excel sheet
    $pathinfo = pathinfo($_FILES["file"]["name"]);

    // check file has extension xlsx, xls and also check 
    // file is not empty
   if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls') 
           && $_FILES['file']['size'] > 0 ) {

        // Temporary file name
        $inputFileName = $_FILES['file']['tmp_name']; 

        // Read excel file by using ReadFactory object.
        $reader = ReaderFactory::create(Type::XLSX);

        // Open file
        $reader->open($inputFileName);
        $count = 1;
        $rows = array(); 

        // Number of sheet in excel file
        foreach ($reader->getSheetIterator() as $sheet) {

            // Number of Rows in Excel sheet
            foreach ($sheet->getRowIterator() as $row) {

                // It reads data after header. In the my excel sheet, 
                // header is in the first row. 
                if ($count> 1){

                $newDate = $row[4]-> format('d/m/Y');   

                $result=mysqli_query($conn,"INSERT INTO stud VALUES                         ('NULL','$row[1]','$row[2]','$row[3]','$newDate','$row[5]','$row[6]','$row[7]')");

                }
                $count++;
            }


        }

        // Close excel file
        $reader->close();

    } else {

        echo "Please Select Valid Excel File";
    }

} else {

    echo "Please Select Excel File";

}
?>

这是用于将excel文件导入mysql数据库的php脚本,我在将日期从excel插入mysql表时出错。

$newDate = $row[4]-> format('d/m/Y');   
$result=mysqli_query($conn,"INSERT INTO stud VALUES           ('NULL','$row[1]','$row[2]','$row[3]','$newDate','$row[5]','$row[6]','$row[7]')");

我尝试格式化行[4],但它显示&#34;未捕获错误:调用字符串&#34;上的成员函数格式() error.Any解决方案?

Excel文件: click to view excel which am working

3 个答案:

答案 0 :(得分:0)

请更改

$newDate = $row[4]-> format('d/m/Y');
`

if(!empty($row[4])){
    $_newDate = new DateTime($row[4]);
}else{
    $_newDate = new DateTime();
}
$newDate = $_newDate->format('d/m/Y');

`

因为$ row [4]是字符串而不是对象日期时间。

答案 1 :(得分:0)

更改行:

spark

$newDate = $row[4]-> format('d/m/Y');

你在字符串上调用了$newDate = new DateTime($row[4])->format('d/m/Y'); 方法。

答案 2 :(得分:0)

新代码

<!doctype>
    <html>
    <head>
    </head>
    <body>
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //disply db table in php page
    mysqli_select_db($conn,'demo_data');


    require_once "Classes/PHPExcel.php";

            $tmpfname = "test.xlsx";
            $excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
            $excelObj = $excelReader->load($tmpfname);
            $worksheet = $excelObj->getSheet(0);
            $lastRow = $worksheet->getHighestRow();

            //echo "<table>";
            for ($row = 2; $row <= $lastRow; $row++) {
                // echo "<tr><td>";
                 $a= $worksheet->getCell('A'.$row)->getValue();
                // echo "</td><td>";
                 $b= $worksheet->getCell('B'.$row)->getValue();
                 $c= $worksheet->getCell('C'.$row)->getFormattedValue();
                 echo $a." ".$b." ".$c." ";
                $timestamp = strtotime($c);
    $e = date('Y-m-d',$timestamp );
    //echo $e;
                // echo "</td><tr>";
                $query="INSERT INTO excel_data VALUES('NULL','$a','$b','$e') ";
                $check=mysqli_query($conn,$query);
                if($check)
                {
                    echo "ok";
                    }
                    else {
                        "no data insertion";
                    }

            }
            //echo "</table>";  
    ?>

    </body>
    </html>

我使用PHPExcel库将excel文件导入mysql。PHPExcel库在导入mysql时只处理日期转换问题。 谢谢......