如何将XLSX文件导入/上载到数据库

时间:2013-04-26 20:14:48

标签: php codeigniter

我正在使用PHPExcel将XLSX文件导入我的相关数据库。但在运行该功能时,我收到错误。我的代码如下所示。

控制器

public function readExcel()
    {
      //load library phpExcel
      $this->load->library("PHPExcel");

      //here i used microsoft excel 2007
      $objReader = PHPExcel_IOFactory::createReader('Excel2007');
      //set to read only
      $objReader->setReadDataOnly(true);
      //load excel file
      $objPHPExcel = $objReader->load("a.xls");
      $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
      //load model
      $this->load->model("User_model");
      //loop from first data until last data
      for($i=2; $i<=77; $i++)
      {
        $name = $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
        $address = $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
        $data_user = array(
        "name" => $name,
        "username" => $address );
        $this->User_model->add_data($data_user);
      }
}

模型

function add_data($datauser)
{
  $this->db->insert('data',$datauser);
  return $this->db->insert_id();
} 

错误::

致命错误:未捕获的异常'PHPExcel_Reader_Exception',消息'无法打开a.xlsx进行阅读!文件不存在。'在C:\ xampp \ htdocs \ htdocs \ htdocs \ school_erp \ school \ school \ application \ libraries \ PHPExcel \ Reader \ Excel2007.php:343堆栈跟踪:#0 C:\ xampp \ htdocs \ htdocs \ htdocs \ school_erp \ school \ school \ application \ controllers \ trial_c.php(32):PHPExcel_Reader_Excel2007-&gt; load('a.xls')#1 [内部函数]:Trial_c-&gt; readExcel()#2 C:\ xampp \ htdocs \ htdocs \ htdocs \ school_erp \ school \ school \ system \ core \ CodeIgniter.php(359):call_user_func_array(Array,Array)#3 C:\ xampp \ htdocs \ htdocs \ htdocs \ school_erp \ school \ school \ index.php(202) ):require_once('C:\ xampp \ htdocs ...')#4 {main}抛出C:\ xampp \ htdocs \ htdocs \ htdocs \ school_erp \ school \ school \ application \ libraries \ PHPExcel \ Reader \ Excel2007。 php在343行

1 个答案:

答案 0 :(得分:2)

使用xml文件的绝对路径:

 /var/www/html/mysite/a.xsl

C:\inthepubs\mysite\a.xsl