2012-05-27 142 views
-1


我在Excel文件中有我學生表的數據。
現在我試圖在我的學生表中的mysql中導入這個文件。
這是我從excel導入數據到我的學生表的代碼。
導入excel文件時出錯

<?php 
    require_once("class/connection.php"); 
    include("libs/Excel/reader.php"); 

    //change-- table name,title and imgdir 
    $table_name = "student"; 
    $title="Excel Import"; 
    $page = $_SERVER['PHP_SELF']; 

    if($_REQUEST["Save"]) 
    { 
     // ExcelFile($filename, $encoding); 
     $data = new Spreadsheet_Excel_Reader(); 
     // Set output Encoding. 
     $data->setOutputEncoding('CP1251'); 
     //assign filename 
     $data->read($_FILES['file']['tmp_name']); 

    for ($x = 2; $x <= count($data->sheets[0]["cells"]); $x++) 
    { 
     $studname = $data->sheets[0]["cells"][$x][1]; 
     $phno = $data->sheets[0]["cells"][$x][2]; 
     $mobno = $data->sheets[0]["cells"][$x][3]; 
     $email = $data->sheets[0]["cells"][$x][4]; 
     $clgname = $data->sheets[0]["cells"][$x][5]; 
     $streamname = $data->sheets[0]["cells"][$x][6]; 
     $remarks = $data->sheets[0]["cells"][$x][7]; 
     $gender = $data->sheets[0]["cells"][$x][8]; 
     $address = $data->sheets[0]["cells"][$x][9]; 

    $query = "INSERT INTO " . $table_name . "(studname,phno,mobno,email,clgname,streamname,remarks,gender,address) 
    values('$studname','$phno','$mobno','$email','$clgname','$streamname','$remarks','$gender','$address')"; 

     mysql_query($query) or die(mysql_error()); 
    } 
    } 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Admin Panel -<?php echo $title;?></title> 
<link rel="SHORTCUT ICON" type="image/x-icon" href="images/cloelle_favicon.ico"> 
</head> 
<body> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td><?php include("header.php");?></td> 
    </tr> 
    <tr> 
    <td align="center" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"> 

     <tr> 



      <td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"> 
        </table></td> 
       <td width="6"><img src="images/home_box_rt.jpg" width="6" height="28" /></td> 
       </tr> 
       <tr> 
       <td background="images/home_box_lbg.jpg">&nbsp;</td> 
       <td height="120" align="center" valign="top" class="successmsg"> 
        <?php 
         if($_REQUEST['submit']) 
         { 
          echo "Your data Transfered Successfully!"; 
         } 
         else 
         { 
          echo "Please Upload Your Excel File Here!"; 
         } 
        ?> 
        <form id="StdForm" name="StdForm" enctype="multipart/form-data" method="post" onsubmit="javascript: return chkValid();"> 
        <p> 
         <input type="file" name="file" id="file" /> 
        </p> 
        <p> 
         <input type="submit" id="Save" name="Save" class="button" value="submit" /> 
        </p> 
        </form></td> 
       <td background="images/home_box_rbg.jpg">&nbsp;</td> 
       </tr> 

      </table></td> 

     </tr> 

     </table></td> 
    </tr> 
    <tr> 
    <td><?php include("footer.php"); ?></td> 
    </tr> 
</table> 
</body> 
</html> 

現在,當我運行此代碼時,會生成以下錯誤。

The filename C:\xampp\tmp\phpFCD3.tmp is not readable 

所以我錯過了什麼?
在此先感謝

回答

1

確保您嘗試讀取的文件以Microsoft Excel(.xls)格式保存。當我用excel閱讀器閱讀時發現同樣的錯誤,發現我的.xls擴展名文件是CSV格式的。所以我打開我的文件,並保存爲微軟excell(.xls),它的工作。

+0

我已經檢查這一點,它是.xls格式。 – KDeogharkar

+0

可能有一些設置我缺少在php.ini中? 你有什麼想法嗎?@Smita – KDeogharkar

+0

是allow_url_fopen在php.ini中設置的?如果是的話,我不認爲它與php.ini有任何關係。 – Smita

0

<?php //for CSV 
 

 
function myprintCSV($aryData = array(), $filetype = "csv", $fileNamesalt = "csv", $bolCols = true) 
 
    { 
 

 

 
if (!is_array($aryData) || empty($aryData)) 
 
    { 
 
     exit(1); 
 
    } 
 

 
    // header 
 
    header('Content-Description: File Transfer'); 
 
\t header('Content-Encoding: UTF-8'); 
 
\t if($filetype=="csv"){ 
 
    header("Pragma: public"); 
 
\t header('Content-Type: text/csv; charset=utf-8'); 
 
\t header("Content-Disposition: attachment; filename=" . $fileNamesalt . time().".csv"); 
 
\t } 
 
\t 
 
\t 
 
    header('Expires: 0'); 
 
    header('Cache-control: private, must-revalidate'); 
 
    header("Pragma: public"); 
 
//echo "<pre>"; 
 
//print_r($aryData);die; 
 
    // Spaltenoberschriften 
 
    if ($bolCols) 
 
    { 
 
     $aryCols = array_keys($aryData[0]); 
 
     array_unshift($aryData, $aryCols); 
 
    } 
 

 
    // Ausgabepuffer for fputcsv 
 
    ob_start(); 
 

 
    // output Stream for fputcsv 
 
    $fp = fopen("php://output", "w"); 
 
    if (is_resource($fp)) 
 
    { 
 
     foreach ($aryData as $aryLine) 
 
     { 
 
     
 
\t \t fputcsv($fp, $aryLine, ',', '"'); 
 
     } 
 

 
     $strContent = ob_get_clean(); 
 

 
     // Excel SYLK-Bug 
 
     // http://support.microsoft.com/kb/323626/de 
 
     $strContent = preg_replace('/^ID/', 'id', $strContent); 
 

 
     //$strContent = utf8_decode($strContent); 
 
     //$intLength = mb_strlen($strContent, 'utf-8'); 
 

 
     // length 
 
     //header('Content-Length: ' . $intLength); 
 

 
     // kein fclose($fp); 
 
\t \t echo "\xEF\xBB\xBF"; 
 
     echo $strContent; 
 
     exit(0); 
 
    } 
 
    ob_end_clean(); 
 
    exit(1); 
 
    } 
 

 
//end 
 

 
// 
 

 

 

 

 

 

 

 

 
    
 
    
 
function createColumnsArray($end_column, $first_letters = '') 
 
{ 
 
    $columns = array(); 
 
    $length = strlen($end_column); 
 
    $letters = range('A', 'Z'); 
 

 
    // Iterate over 26 letters. 
 
    foreach ($letters as $letter) { 
 
     // Paste the $first_letters before the next. 
 
     $column = $first_letters . $letter; 
 

 
     // Add the column to the final array. 
 
     $columns[] = $column; 
 

 
     // If it was the end column that was added, return the columns. 
 
     if ($column == $end_column) 
 
      return $columns; 
 
    } 
 

 
    // Add the column children. 
 
    foreach ($columns as $column) { 
 
     // Don't itterate if the $end_column was already set in a previous itteration. 
 
     // Stop iterating if you've reached the maximum character length. 
 
     if (!in_array($end_column, $columns) && strlen($column) < $length) { 
 
      $new_columns = createColumnsArray($end_column, $column); 
 
      // Merge the new columns which were created with the final columns array. 
 
      $columns = array_merge($columns, $new_columns); 
 
     } 
 
    } 
 

 
    return $columns; 
 
}