Hi,
I tried to import excel content using :

module_load_include('inc', 'phpexcel');
  
  // The path to the excel file
  $path = 'sites/default/files/export.xls';
  
  $result = phpexcel_import($path);
  
  if (is_array($result)) {
    drupal_set_message(t("We did it !"));
  }
  else {
    drupal_set_message(t("Oops ! An error occured !"), 'error');
  }

in a view but it is showing "Oops ! An error occured !"
I have download the entire library (1.8.*) from Github and put it under sites/*/libraries/PHPExcel/ as well as made sure changelog.txt file is present.
I am new to drupal so I don't know where am going wrong. Can someone help me?

Comments

_lakshmi_ created an issue. See original summary.

arunkumark’s picture

@lakshmi

First Thanks for using this module.

Can you please confirm "export.xls" file is exist or not before processing. And also confirm given file is readable.

Try the below code may help useful to resolve your problem.

// The path to the excel file
$path = 'sites/default/files/export.xls';
if (file_exists(DRUPAL_ROOT . '/' . $path)) {
  if (is_readable($path)) {
    module_load_include('inc', 'phpexcel');
    $result = phpexcel_import($path);
    if (is_array($result)) {
      drupal_set_message(t("We did it !"));
    }
    else {
      drupal_set_message(t("Oops ! An error occured !"), 'error');
    }
  }
  else {
    drupal_set_message(t("File cant readable"), 'error');
  }
}
else {
  drupal_set_message(t("File doesn't exist"), 'error');
}
_lakshmi_’s picture

@arun I am getting the error : Oops ! An error occured !

arunkumark’s picture

Status: Active » Postponed (maintainer needs more info)

Can you please detail your Problem.

_lakshmi_’s picture

@arun I executed the code that you suggested and it's showing error as "Oops ! An error occured !" as per

if (is_array($result)) {
      drupal_set_message(t("We did it !"));
    }
    else {
      drupal_set_message(t("Oops ! An error occured !"), 'error');
    }
  }

When I checked the configuration tab I noticed the error:
" One or more problems were detected with your Drupal installation. Check the status report for more information."

and the status report is showing

"PHPExcel >1.7.8
In order to use PHPExcel, you must first download the entire library (1.8.*) at Github and put it under sites/*/libraries/PHPExcel/ (so you should have sites/*/libraries/PHPExcel/Classes/PHPExcel.php). Make sure to include the changelog.txt file as well (sites/*/libraries/PHPExcel/changelog.txt), as Library API requires this file for loading the classes."

I tried pasting the entire library multiple times, restarted my local server multiple times,but still both errors are not resolving.

_lakshmi_’s picture

@arun The file path is being read but the content( a table of numbers ) is not getting read when I tried running the following code

$path = 'sites/default/files/webform/docs/exceltry.xlsx';
if (file_exists(DRUPAL_ROOT . '/' . $path)) {
  if (is_readable($path)) {
    module_load_include('inc', 'phpexcel');
    $result = phpexcel_import($path);
    echo $result;
    if (is_array($result)) {
      drupal_set_message(t("We did it !"));
      
    }
    else {
      drupal_set_message(t("Oops ! An error occured !"), 'error');
    }
  }
  else {
    drupal_set_message(t("File cant readable"), 'error');
  }
}
else {
  drupal_set_message(t("File doesn't exist"), 'error');
}


?>

Result is coming as
Notice: Array to string conversion in eval() (line 35 of C:\xampp\htdocs\drupal\modules\php\php.module(80) : eval()'d code).
Status message: We did it !
$result val: Array

Can you help me understand where am going wrong?

arunkumark’s picture

@lakshmi,

Can you please confirm you are using this code inside any UI(PHP code in DB). If it is then please write in your custom modules. So, you can able to find out the exact error reason.
From your result, the XLS reading operation done properly. To access the result array values by their keys so you get the processed values.

$path = 'sites/default/files/webform/docs/exceltry.xlsx';
if (file_exists(DRUPAL_ROOT . '/' . $path)) {
  if (is_readable($path)) {
    module_load_include('inc', 'phpexcel');
    $result = phpexcel_import($path);
    print_r($result); // Array is print as using print_r() instead of echo or print.
    if (is_array($result)) {
      drupal_set_message(t("We did it !"));
    }
    else {
      drupal_set_message(t("Oops ! An error occured !"), 'error');
    }
  }
  else {
    drupal_set_message(t("File cant readable"), 'error');
  }
}
else {
  drupal_set_message(t("File doesn't exist"), 'error');
}
_lakshmi_’s picture

@arun Thank you. Using print_r($result); I was able to view the content of the excel document.
I made a content type with fields Title, Body and Upload(file field type) and a corresponding view. Am using the code inside an editor of this content type.

arunkumark’s picture

Title: $result = phpexcel_import($path) issue » Import file path issue [phpexcel_import($path)]
Status: Postponed (maintainer needs more info) » Closed (works as designed)

@laskshmi Instead of writing code inside Editor try to write you own codes in your module files and call to your view.

Expecting that this problem resolved, Closing this issue.

Cheers!

_lakshmi_’s picture

@arun Thank you the problem is resolved.
I haven't tried making my own modules as am new to Drupal. Will look into it. thank you.