0
phpExcelを使用してデータベースにExcelをインポートして挿入したいのですが、エラーが発生しました。正しいエントリの配列をデータベースに挿入できません.Iコードを抽出してここに投稿しましたが、それは実際には長いので、読んで疲れています。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>
してください。常に問題を解決するのに役立ちます – safarov
あなたは$ readerType = PHPExcel_IOFactory :: createReader($ file)を使う方が良いでしょう。ファイルタイプを特定するためのファイル名のユーザ入力を信頼し、使用するリーダを信頼するよりも... try/catchブロックを使用する:PHPExcelは問題を特定するのに役立つ例外をスローする –