Search code examples
file-uploadphpexcelfile-read

Can not load file using PHPExcel


I am currently using PHPExcel library to read my excel file which is to be uploaded by user . But I can not load the uploaded file.
I am using this code, i cannot get the file path right at this moment and if there is some one who could tell me , what to write in load portion => $objPHPExcel = PHPExcel_IOFactory::load();
The uploaded file is being transferred to the folder named "upload".

<?php
$storagename = $_FILES["file"]["name"];
$new_file_name=$storagename.'.xlsx';
move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $new_file_name);
include ("PHPExcel/IOFactory.php"); 
$html="<table border='1'>";
$objPHPExcel = PHPExcel_IOFactory::load('"../upload/" . $new_file_name');  

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
    $highestRow = $worksheet->getHighestRow();  
    for ($row=2; $row<=$highestRow; $row++)  
    {
      $html.="<tr>";  
      $site_name = mysqli_real_escape_string($conn, $worksheet->getCellByColumnAndRow(0, $row)->getValue());  
      $solution_type = mysqli_real_escape_string($conn, $worksheet->getCellByColumnAndRow(1, $row)->getValue());  

      $html.= '<td>'.$site_name.'</td>';  
      $html .= '<td>'.$solution_type.'</td>';
      $html .= "</tr>";
      }  
    }  
    $html .= '</table>';  
    echo $html;
?>

Solution

  • This is working to load file usinh PHPExcel.

    <?php
    $storagename = $_FILES["file"]["name"];
    $new_file_name=$storagename.'.xlsx';
    move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" .$new_file_name);
    $upload_file = "../upload/" . $new_file_name;
    
    include ("PHPExcel/IOFactory.php"); 
    $html="<table border='1'>";
    $objPHPExcel = PHPExcel_IOFactory::load($upload_file);  
    
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
    {
    $highestRow = $worksheet->getHighestRow();  
    for ($row=2; $row<=$highestRow; $row++)  
    {
      $html.="<tr>";  
      $site_name = mysqli_real_escape_string($conn, $worksheet->getCellByColumnAndRow(0, $row)->getValue());  
      $solution_type = mysqli_real_escape_string($conn, $worksheet->getCellByColumnAndRow(1, $row)->getValue());  
    
      $html.= '<td>'.$site_name.'</td>';  
      $html .= '<td>'.$solution_type.'</td>';
      $html .= "</tr>";
      }  
    }  
    $html .= '</table>';  
    echo $html;
    ?>