2017-06-19 73 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网站。