2011-11-07 22 views
6

Bir xlsx dosyasını okumak ve kapalı MYSQLPHPExcel - Hücreleri Okuma ve MYSQL

burada

içine hücrelerden bilgi aktarmak için bir php komut dosyası kullanmaya çalışıyorum içine geçme ben değilim, benim kodudur PHPExcel sürüm 1.7.6 ve PHP kullanarak 5.3.5

require_once 'PHPExcel.php'; 
$inputFileType = 'Excel2007'; 
$inputFileName = $upload_path . $filename; 

/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */ 
class chunkReadFilter implements PHPExcel_Reader_IReadFilter 
{ 
    private $_startRow = 0; 
    private $_endRow = 0; 

    /** Set the list of rows that we want to read */ 
    public function setRows($startRow, $chunkSize) { 
     $this->_startRow = $startRow; 
     $this->_endRow = $startRow + $chunkSize; 
    } 

    public function readCell($column, $row, $worksheetName = '') { 
     // Only read the heading row, and the configured rows 
     if (($row == 1) || 
      ($row >= $this->_startRow && $row < $this->_endRow)) { 
      return true; 
     } 
     return false; 
    } 
} 


/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 


/** Define how many rows we want to read for each "chunk" **/ 
$chunkSize = 2048; 
/** Create a new Instance of our Read Filter **/ 
$chunkFilter = new chunkReadFilter(); 

/** Tell the Reader that we want to use the Read Filter **/ 
$objReader->setReadFilter($chunkFilter); 

/** Loop to read our worksheet in "chunk size" blocks **/ 
for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) { 
    /** Tell the Read Filter which rows we want this iteration **/ 
    $chunkFilter->setRows($startRow,$chunkSize); 
    /** Load only the rows that match our filter **/ 
    $objPHPExcel = $objReader->load($inputFileName); 

// Need to pass the cell values into the variables 

Ben phpexcelreader için çalışacak bu

for ($x = 2; $x < = count($data->sheets[0]["cells"]); $x++) { 
    $item_number = $data->sheets[0]["cells"][$x][1]; 
    $qty_sold = $data->sheets[0]["cells"][$x][2]; 
    $cost_home = $data->sheets[0]["cells"][$x][3]; 

gibi bir şey kullanmak zorunda budur, ama sadece fonksiyonları yapacağını hangi bilmiyorumphpExcel

//here is where I would pass those values into MYSQL 

$sql = "INSERT INTO sales_report (`item_number`,`qty_sold`, `cost_home`) 
     VALUES ('$item_number',$qty_sold,'$cost_home')"; 
     echo $sql."\n"; 
    mysql_query($sql); 
} 
?> 

ben mysql içine elektronik tablodan veri almak için nasıl bir toplam kayıp değilim için aynı

DÜZENLEME:

Veri kullanarak basılan almak başardınız aşağıdaki diziler Ancak, tabloya ekleyemiyorum. Ben de işlevini kullanarak denedim ama hiçbir şey olmuyor.

+0

alternatif: http://dev.mysql.com/doc/refman/5.1/en /load-data.html –

+0

Zaten sordu, şu sayfaya bakın: http://stackoverflow.com/questions/7156228/reading-a-xlsx-sheet-to-feed-a-mysql-table-using-phpexcel/7286159 # 7286159 –

+0

Bunu sormadan önce denedim. Benim için çalışmadı. – Fahad

cevap

2

Bunu yapmanın en kolay yolu, xlsx dosyasını csv dosyasına anında dönüştürmek ve normal CSV ayrıştırma kullanmaktır. Sadece (Yarın örnek kod sağlayabilir) CsvWriter örneğini ve geçici bir konuma kaydetmek

örnek kod:

$objReader = PHPExcel_IOFactory::load ($file_path); 
    $writer = PHPExcel_IOFactory::createWriter ($objReader, 'CSV'); 
    $writer->save ($csv_path);  
    if (($handle = fopen ($csv_path, "r")) !== false) { 

    while (($data = fgetcsv ($handle)) !== false) { 
     print_r($data); 
    } 
    } 
+0

teşekkürler darhazer, eğer bana yardım ederseniz, bana yardım edin. Bunu yapmak için bir yol arıyordum ama hiçbir şey henüz lol. Tekrar teşekkürler – Fahad

+0

@Fahad örnek kodla güncellendi. –

+0

kod için teşekkürler, iyi çalışıyor. Ama excel2007'nin bazı işlevlerini (gelecekte) kullanabilmek istiyorum. Onu CSV'ye dönüştürmem, formüllerim ve benzeri şeylerimi kaybetmemi sağladı mı? Veriyi doğrudan nasıl ekleyeceğimi bir kez düşündüğümde, hücrelerdeki formülleri de çekebilmeliyim. – Fahad