PHPExcel无法将excel文件导入数据库

时间:2012-03-27 19:02:48

标签: php phpexcel

我想使用phpExcel来获取导入excel并插入到数据库中,但是,我遇到错误。似乎正确的条目数组无法插入到数据库中。我已提取代码并在此处发布,它确实很长,读起来很累,所以我会投票支持任何人回答这个问题。

非常感谢您的帮助。

这部分用于检查错误和重复并获得有效列表:

<body>

<?
session_start();

$file='../excelUpload/'.$_SESSION['username'].'/'.$_POST['excel'];
include '../plugin/excel/Classes/PHPExcel/IOFactory.php';

$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext=='xlsx')
$readerType='Excel2007';
elseif ($ext=='xls')
$readerType='Excel5';
elseif ($ext=='csv')
$readerType='CSV';

$reader = PHPExcel_IOFactory::createReader($readerType);
$PHPExcel = $reader->load($file);
$sheet = $PHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

$pattern="/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/";

for ($row = 1; $row <= $highestRow; $row++){
for ($head = 0; $head < $highestColumn; $head++){
$testMail = $sheet->getCellByColumnAndRow($head, $row)->getValue();
if (preg_match($pattern,$testMail))
$mailColumn=$head;
}}
if(!isset($mailColumn))
{die('No email column detected, please check your file and import again.');}


$invaild[] = NULL ;
$email[] = NULL ;
$duplicate[] = NULL ;

for ($row = 1; $row <= $highestRow; $row++) {
    for ($y = 0; $y < $highestColumn; $y++) {
    $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();

    if ($y == $mailColumn && !preg_match($pattern,$val))
    {$invaild[]=$row;}
    elseif ($y == $mailColumn && in_array($val,$email))
    {$duplicate[]=$val;
    $duplicate[]=$row;}
    //elseif (!in_array($row,$duplicate) && !in_array($row,$invaild) )
    //{echo $val;}

    if ($y == $mailColumn)
    {$email[]=$val;
    $email=array_unique($email);}

  }
}
$invaild=array_unique($invaild);
?>

<!-- To Show mail import result -->
<div id="stylized" class="view">
<h1><?echo $_POST['excel'].' Result';?></h1> 
<p>Import from spreadsheet files</p>
   <div id="container">
<table cellpadding="0" cellspacing="0" border="2" class="display" id="viewImport">
<thead>
<tr>

<?
for ($head = 0; $head < $highestColumn; $head++){
if ($head==$mailColumn)
echo "<th field='col'$head> Email address </th>";
else
echo "<th field='col'$head><input id='$head' name='$head' class='required' value='Input Header' size='1'/> </th>";
}
?>
</tr>
</thead>

<?
for ($row = 1; $row <= $highestRow; $row++) {
    echo "<tr>";
    for ($y = 0; $y < $highestColumn; $y++) {
        if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
            $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();
            echo "<td>";
            if (!$val)
            echo "-";
            else
            echo $val;
            echo "</td>";}
        }
    echo "</tr>";   
    }



?>

</table>
</div>


<form id="insertResult" method="post" action="excelSQL.php" >
<br><br><br>
<input type="hidden" id="file" name="file" value="<?echo $file;?>" />
<label>Insert into list:</label>
<input id="listName" name="listName"/>
<div class="spacer"></div>
<input class="submit" type="submit" name="submit" value="Insert into Database"/>
</form>
<div class="spacer"></div>
</div>
</body>
</html>

这部分用于插入数据库:

<body>

<?
session_start();
include("../connection/conn.php");
$file=$_POST['file'];
include ("../plugin/excel/Classes/PHPExcel/IOFactory.php");

$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext=='xlsx')
$readerType='Excel2007';
elseif ($ext=='xls')
$readerType='Excel5';

$reader = PHPExcel_IOFactory::createReader($readerType);
$PHPExcel = $reader->load($file);
$sheet = $PHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

$pattern="/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/";

for ($row = 1; $row <= $highestRow; $row++){
for ($head = 0; $head < $highestColumn; $head++){
$testMail = $sheet->getCellByColumnAndRow($head, $row)->getValue();
if (preg_match($pattern,$testMail))
$mailColumn=$head;
}}
if(!isset($mailColumn))
{die('No email column detected, please check your file and import again.');}


$invaild[] = NULL ;
$email[] = NULL ;
$duplicate[] = NULL ;

for ($row = 1; $row <= $highestRow; $row++) {
    for ($y = 0; $y < $highestColumn; $y++) {
    $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();

    if ($y == $mailColumn && !preg_match($pattern,$val))
    {$invaild[]=$row;}
    elseif ($y == $mailColumn && in_array($val,$email))
    {$duplicate[]=$val;
    $duplicate[]=$row;}
    //elseif (!in_array($row,$duplicate) && !in_array($row,$invaild) )
    //{echo $val;}

    if ($y == $mailColumn)
    {$email[]=$val;
    $email=array_unique($email);}

  }
}
$invaild=array_unique($invaild);

for ($head = 0; $head < $highestColumn; $head++){
    if ($head != $mailColumn)
    {echo $_POST[$head];
    $sql="INSERT INTO subatt (AttID,AttName) VALUES ('','$_POST[$head]')";
    if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }}
    else 
    echo "email";
     }
?>


<div id="stylized" class="view">
<h1><?echo $file.' Result';?></h1> 
<p>Import from spreadsheet files</p>


<?

for ($row = 1; $row <= $highestRow; $row++) {
    for ($y = 0; $y < $highestColumn; $y++) {
        if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
            $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();
            if (!$val)
            {$sql="INSERT INTO subdata (DataID,DataContent) VALUES ('','')";}
            else
            $sql="INSERT INTO subdata (DataID,DataContent) VALUES ('','$val')";
                if (!mysql_query($sql,$conn))
                 {
                die('Error: ' . mysql_error());
                }
            $result = mysql_query("SELECT AttID FROM subatt WHERE AttName='$_POST[$y]'");
            if (!$result) 
            {die('Could not query:' . mysql_error());}
            $attID = mysql_result($result, 0);


            $sql="INSERT INTO subscriber (SubID,AttID,DataID) VALUES ('',$attID,'')";
                if (!mysql_query($sql,$conn))
                 {
                die('Error: ' . mysql_error());
                }
                }
        }
    }



?>

<div class="spacer"></div>
</div>
</body>

1 个答案:

答案 0 :(得分:0)

我可以看到您的查询中有一个DataID。为什么总是空的?是否有任何唯一/主要索引?

我也想知道确切的错误是什么