2011-09-19 14 views
2

私はこれにかなり新しいので質問したいと思っていました。私は、HTMLを使用してExcelファイルをアップロードし、私はPHPを使用してExcelファイルを読み込む方法を学習しようとすると、これは私がIBMから持っているものです:PHPでxls(Excel)ファイルを読む

<?php 

$filename = basename($_FILES["file"]["name"]); 

$ext = substr($filename, strrpos($filename, '.') + 1); 

function get_data($qnum, $qtype, $qname, $qtext, $atext, $pcredit, $rcounts, $r, $qcount, $cfac, $sd, $disc_inx, $disc_coef) 
{ 
    global $data; 

    $data[] = array('QNum' => $qnum, 'Qtype' => $qtype, 'Questionname' => $qname, 'Questiontext' => $qtext, 
     'Answertext' => $atext, 'PartialCredit' => $pcredit, 'RCounts' => $r, 'QCount' => $qcount, 
     'Correctionfacility' => $cfac, 'DiscriminationIndex' => $disc_inx, 'DiscriminationCoefficient' => $disc_coef); 
} 

if ($ext == "xls") { 
    $dom = DOMDocument::load($_FILES['file']['tmp_name']); 
    $rows = $dom->getElementsByTagName('Row'); 
    $first_row = true; 
    foreach ($rows as $row) { 
     if (!$first_row) { 
      $qnum = ""; 
      $qtype = ""; 
      $qname = ""; 
      $qtext = ""; 
      $atext = ""; 
      $pcredit = ""; 
      $r = ""; 
      $qcount = ""; 
      $cfac = ""; 
      $disc_inx = ""; 
      $disc_coef = ""; 

      $index = 1; 
      $cells = $row->getElementsByTagName('Cell'); 
      foreach ($cells as $cell) { 
       $ind = $cell->getAttribute('Index'); 
       if ($ind != null) 
        $index = $ind; 

       if ($index == 1) 
        $qnum = $cell->nodeValue; 
       if ($index == 2) 
        $qtype = $cell->nodeValue; 
       if ($index == 3) 
        $qname = $cell->nodeValue; 
       if ($index == 4) 
        $qtext = $cell->nodeValue; 
       if ($index == 5) 
        $atext = $cell->nodeValue; 
       if ($index == 6) 
        $pcredit = $cell->nodeValue; 
       if ($index == 7) 
        $r = $cell->nodeValue; 
       if ($index == 8) 
        $qcount = $cell->nodeValue; 
       if ($index == 9) 
        $cfac = $cell->nodeValue; 
       if ($index == 10) 
        $disc_inx = $cell->nodeValue; 
       if ($index == 11) 
        $disc_coef = $cell->nodeValue; 

       $index += 1; 
      } 
      get_data($qnum, $qtype, $qname, $qtext, $atext, $pcredit, $r, $qcount, $cfac, $disc_inx, $disc_coef); 
     } 
     $first_row = false; 
    } 
} 

else { 
    echo "Invalid file!"; 
} 
?> 

そして私は、構文エラーを得た

Warning: DOMDocument::load(): Start tag expected, '<' not found in /tmp/phpwUIpyZ, line: 1 in /var/www/moodle/question/upload_file.php on line 16 Fatal error: Call to a member function getElementsByTagName() on a non-object in /var/www/moodle/question/upload_file.php on line 17.

私のコードのエラーは何ですか?感謝の助けが必要です!あなたはそれのためのDOMDocumentを使用することはできませんもちろんのよう:)

私はPHPExcelReaderなどの準備ができて何かを使用することをお勧めし

答えて

5

Excelは、有効のDOMDocumentではありません。

Good Luck、
Shai。

+0

ありがとうございます! :D – POGI

1

エラーメッセージは$dom is a non-objectと言い換えられます。つまり、DOMDocument::loadの何かを返しましたが、オブジェクトではありません。そこそのための様々な理由であってもよいが、最も可能性の高いものであることができます:

  • ファイルが存在しないか、読み取れない
  • ファイルが(いない有効なDOM文書)不正な形式で、構文解析は
を失敗しました

マニュアルを参照してください:http://php.net/manual/en/domdocument.load.php

また、XLSファイルをDOM文書として解析しようとしているように見えます - 飛行しない、まったく異なるファイル形式です。

関連する問題