English 中文(简体)
向MySQL表格输入一个Excel文件
原标题:Import an excel file into a MySQL table with PHPExcel

因此,我得以在外壳表上显示其数据,但这种数据可插入我的表格。 我看不出这个部分,这里是我迄今所看到的:

    $path = $_GET[ file ];
include("../class/sql.php");
require  ../class/PHPExcel.php ;
require_once  ../class/PHPExcel/IOFactory.php ;
$objPHPExcel = PHPExcel_IOFactory::load($path);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g  F 
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo  <br>Data: <table width="100%" cellpadding="3" cellspacing="0"><tr> ;
    for ($row = 1; $row <= $highestRow; ++ $row) {

        echo  <tr> ;
        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
            if($row === 1)
            echo  <td style="background:#000; color:#fff;">  . $val .  </td> ;
            else
                echo  <td>  . $val .  </td> ;
        }
        echo  </tr> ;
    }
    echo  </table> ;
}

b 以前的PHPExcel是一件很麻烦的事,我没有时间通过它来读,以充分理解: 我不得不在午宴上这样做。 预 收

Edit:这是它应该做的思想,价值观部分是我所不知的。

$sql = "insert into tablename (col1, col2, col3) values(...)";
//start at row 2 so headers are not inserted
for ($row = 2; $row <= $highestRow; ++ $row) {

    for ($col = 0; $col < $highestColumnIndex; ++ $col) {
        $cell = $worksheet->getCellByColumnAndRow($col, $row);
        $val = $cell->getValue();
        //here s my prob..
        echo $val;
    }
    $result = mysql_query($sql);
}
最佳回答

您应创建一个阵列,并将其储存在数据库中,例如:

for ($row = 2; $row <= $highestRow; ++ $row) {
$val=array()
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
    $cell = $worksheet->getCellByColumnAndRow($col, $row);
    $val[] = $cell->getValue();
    //here s my prob..
    //echo $val;
}

$sql="insert into tablename (col1, col2, col3) values(`".$val[0]."`, `".$val[1]."`, `".$val[2].")";
$result = mysql_query($sql);


}
问题回答

如果你想要利用PHPExcel来做到这一点:

<?php
//include the following 2 files
require  Classes/PHPExcel.php ;
require_once  Classes/PHPExcel/IOFactory.php ;

$SERVER =  localhost ;
$USERNAME =  username ;
$PASSWORD =   password ;
$DB =  database ;
$DSN = "mysql:host=".$SERVER.";dbname=".$DB."";
$connection = new PDO($DSN,$USERNAME,$PASSWORD);

$path = "test.xlsx";

$objPHPExcel = PHPExcel_IOFactory::load($path);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g  F 
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo "<br>The worksheet ".$worksheetTitle." has ";
    echo $nrColumns .   columns (A-  . $highestColumn .  )  ;
    echo   and   . $highestRow .   row. ;
    echo  <br>Data: <table border="1"><tr> ;
    for ($row = 1; $row <= $highestRow; ++ $row) {
        echo  <tr> ;
        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
            $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
            echo  <td>  . $val .  <br>(Typ   . $dataType .  )</td> ;
        }
        echo  </tr> ;
    }
    echo  </table> ;
}

for ($row = 2; $row <= $highestRow; ++ $row) {
    $val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
   $cell = $worksheet->getCellByColumnAndRow($col, $row);
   $val[] = $cell->getValue();
}

 $Connection="INSERT INTO `users` (name, family, type) VALUES ( ".$val[1] . " , " . $val[2] . " , " . $val[3]. " )";

}
?>

这是一种使用早期图书馆面读书的良好文章。

http://major.io/2008/11/07/importing-excel-files-into-mysql-with-php/

检查





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签