2011-11-07 12 views
6

मैं एक xlsx फ़ाइल को पढ़ने, एवं माई SQLPHPExcel - प्रकोष्ठों पढ़ना और MYSQL

यहाँ

में बंद कोशिकाओं से जानकारी भेजने के लिए एक php स्क्रिप्ट का उपयोग करने की कोशिश कर रहा हूँ में पासिंग मेरी कोड है, मैं कर रहा हूँ PHPExcel संस्करण 1.7.6 और PHP का उपयोग 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 

यह जहां मैं इस

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

जो phpexcelreader के लिए काम करेगा की तरह कुछ का उपयोग करने की आवश्यकता है, लेकिन मैं सिर्फ इतना पता है जो कार्यों करना होगा नphpExcel

//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); 
} 
?> 

मैं mysql में स्प्रेडशीट से डेटा प्राप्त करने के लिए के रूप में एक कुल नुकसान में हूँ के लिए एक ही

संपादित करें:

मैं डेटा का उपयोग करके मुद्रित करने के लिए प्रबंधित किया है निम्नलिखित arrays

foreach ($objWorksheet->getRowIterator() as $row) { 
    $j = 1; 

    $cellIterator = $row->getCellIterator(); 
    $cellIterator->setIterateOnlyExistingCells(false); 

    foreach ($cellIterator as $cell) { 
     $data->sheets[0]['cells'][$i][$j] = $cell->getValue(); 

     $j++; 
    } // end cell getter 

    $i++; 

} // end row getter 

लेकिन मुझे लगता है कि यह मेरी तालिका में डालने के लिए प्रतीत नहीं होता है। मैंने इंपोड फ़ंक्शन का उपयोग करने का भी प्रयास किया है लेकिन कुछ भी नहीं होता है।

+0

विकल्प: http://dev.mysql.com/doc/refman/5.1/en /load-data.html –

+0

पहले से ही पूछा गया है, इसे देखें: http://stackoverflow.com/questions/7156228/reading-a-xlsx-sheet-to-feed-a-mysql-table-using-phpexcel/7286159 # 7286159 –

+0

मैंने सवाल पूछने से पहले ही कोशिश की है। यह मेरे लिए काम नहीं किया। – Fahad

उत्तर

2

ऐसा करने का सबसे आसान तरीका है xlsx को सीएसवी फ़ाइल को फ्लाई पर परिवर्तित करना और सामान्य सीएसवी पार्सिंग का उपयोग करना। बस CSVWriter का दृष्टांत और एक अस्थायी स्थान पर सहेजें (मैं कल तक उदाहरण कोड प्रदान कर सकते हैं)

नमूना कोड:

$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

धन्यवाद darhazer अगर आप कृपया मेरी मदद कर सकते हैं क्योंकि यह मुझे दीवार चला रहा है। मैंने एक सप्ताह बिताया है ऐसा करने के लिए एक रास्ता तलाश रहा है लेकिन अभी तक कुछ भी नहीं है। धन्यवाद फिर से – Fahad

+0

@ फ़हाद नमूना कोड के साथ अद्यतन किया गया। कोड के लिए –

+0

धन्यवाद, यह अच्छी तरह से काम करता है। लेकिन मैं एक्सेल 2007 की कार्यक्षमता (भविष्य में) का उपयोग करने में सक्षम होना चाहता हूं। इसे सीएसवी में परिवर्तित नहीं करना मुझे अपने सूत्रों की तरह चीजें खोना है? मुझे लगता है कि एक बार जब मैं सीधे डेटा डालने का तरीका समझता हूं तो मुझे कोशिकाओं में सूत्रों को खींचने में सक्षम होना चाहिए। – Fahad