2017-06-19 70 views
-5

我很容易導入整個excel文件數據,但是當我嘗試先讀取excel文件數據並且只導入我應用的條件時。我在哪裏插入查詢和邏輯的PHP代碼?

如下表excel文件data.and我只嘗試,我有一個選擇日期導入。如果我選擇日期2017-06-03。此日期數據只插入數據庫其他數據未插入。

否恩諾INOUT日期時間 1的I2S 2017年6月2日八時35分00秒2 28號第2017年6月2日十點10分00秒3 28e的2017年6月2日13:00:00 4 12 E 2017-06-02 14:02:00 5 12 S 2017-06-02 15:02:00 6 12 E 2017-06-02 19:15:00 7 12 S 2017-06- 03 9時45分00秒 8 11 E 2017年6月3日14時15分00秒

但我怎麼能做到這一點,我不知道?????

HTML代碼:

<form method="post" action="dataread.php" enctype="multipart/form-data">   
      <p> 
      <label for="import">Import your data into database:</label> 
      </p> 
      <p> 
     <input type="date" name="date" required/> 
     </p> 
     <input type="file" name="file" required/> 
      </p> 
      <p><input type="submit" name="submit" value="Submit"/></p> 
      </form> 

PHP代碼:

<?php 
include("dbconnection.php"); 


/** Set default timezone (will throw a notice otherwise) */ 
date_default_timezone_set('Asia/Kolkata'); 

include 'PHPExcel\Classes\PHPExcel\IOFactory.php'; 

if(isset($_FILES['file']['name'])) 
{ 

    $file_name = $_FILES['file']['name']; 
    $ext = pathinfo($file_name, PATHINFO_EXTENSION); 

    //Checking the file extension 
    if($ext == "xlsx") 
    { 

      $file_name = $_FILES['file']['tmp_name']; 
      $inputFileName = $file_name; 


     // Read your Excel workbook 
     try { 
      $inputFileType = PHPExcel_IOFactory::identify($inputFileName); //Identify the file 
      $objReader = PHPExcel_IOFactory::createReader($inputFileType); //Creating the reader 
      $objPHPExcel = $objReader->load($inputFileName); //Loading the file 
     } catch (Exception $e) { 
      die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) 
      . '": ' . $e->getMessage()); 
     } 

     //Table used to display the contents of the file 
     echo '<center><table style="width:50%;" border=1>'; 

     // Get worksheet dimensions 
     $sheet = $objPHPExcel->getSheet(0);  //Selecting sheet 0 
     $highestRow = $sheet->getHighestRow();  //Getting number of rows 
     $highestColumn = $sheet->getHighestColumn();  //Getting number of columns 

     // Loop through each row of the worksheet in turn 
     for ($row = 1; $row <= 15; $row++) 
     { 

      // Read a row of data into an array 

      $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, 
      NULL, TRUE, FALSE); 


      // This line works as $sheet->rangeToArray('A1:E1') that is selecting all the cells in that row from cell A to highest column cell 

      echo "<tr>"; 

      //echoing every cell in the selected row for simplicity. You can save the data in database too. 
      foreach($rowData[0] as $k=>$v) 
       echo "<td>".$v."</td>"; 




      echo "</tr>"; 
     } 

     echo '</table></center>'; 

    } 

    else{ 
     echo '<p style="color:red;">Please upload file with xlsx extension only</p>'; 
    } 

} 
?> 

回答

0
  • PhpExcelReader是可以不使用微軟Office讀取Excel文件數據的免費PHP類。它支持XLS和XLSX類型。

•下載PhpExcelReader。

這個類是中小型有用excel文件大小(幾MB),因爲它讀取整個電子表格一次,如果你已經有了一個大的電子表格,內存被耗盡。在這種情況下,最好將excel數據轉換爲CSV格式,然後在PHP中使用它。

  • 在PHP中使用Excel文件的工作較新的PHP庫是此頁: http://coursesweb.net/php-mysql/phpspreadsheet-read-write-excel-libreoffice-files
  • 它可以讀取和寫入Excel文件在PHP中,也可以創建PDF文檔和圖表。 PhpExcelReader類將excel文件數據存儲到$ sheets屬性中的多維數組中,可通過以下方式訪問: $ objectClass-> sheets
  • $ objectClass是類的對象實例。

例如,該Excel表: Excel工作表

要讀這個Excel文件的PhpExcelReader類,使用此代碼:

  • 與PhpExcelReader類檔案,你會發現一個用於測試的「test.xls」文件,以及兩個示例,以及代碼中的詳細信息。 •此類是針對Spreadsheet_Excel_Reader類的PHP 5.3+的版本,來自:PHP-ExcelReader網站。