2011-11-22 16 views
9

Tamam, bu yüzden excel .xls sayfasındaki verileri göstermek için php elde edebildim, ancak bu tabloya ekleyebilmem için aynı verileri veriyorum.Bir Excel dosyasını PHPExcel ile bir MySQL tablosuna aktarın.

$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>'; 
} 

Btw PHPExcel müthiş ve ben tamamen anlamak için her şeyi okumaya zamanım olmadı :(Ben: Burada şimdiye kadar ne var, bu bölümü anlamaya gibi olamaz peşin .. çarşamba tarafından Teşekkür bunda açmak için

Düzenleme:. Bu da bölüm I emin değilim biridir değerleri do..the gerektiği fikirdir

$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); 
} 
+0

, bu ödev mi? Eğer öyleyse, bu şekilde etiketleyebilirsiniz .... – Jason

+0

Hayır, ev ödevi değil, bu seçenek için istediğim bir serbestlik ve bu seçenek için son dakikada istedikleri ve tüm projenin Çarşamba gününe kadar olması gerekiyor. Eğer .net olsaydı ben şimdi tarafından yapılmış olurdu ama bu php içinde yapılması gerekiyordu :( – Andres

+0

MySQL tablonuza kıyasla nasıl bir dosya gibi görünüyorsunuz? – Jason

cevap

7

bir dizi oluşturmalıdır ve bunun gibi bir veritabanında saklayın:

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); 


} 
+0

Bu harika görünüyor, henüz test etmedim çünkü şu anda değişiklikleri yüklemek için ftp'ye erişimim yok, ama denediğimde cevap olarak işaretleyeceğim. Teşekkürler! – Andres

+0

teşekkürler güzel çalıştı! – Andres

1

kontrol edin: Yani

<?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]. "')"; 

} 
?> 
İlgili konular