上传Excel并将其存储在数据库中?

时间:2014-01-23 10:34:49

标签: php excel codeigniter

我想将Excel文件上传到我们的网页,然后相应的数据将其存储在数据库中。然后我想检索所有数据并以表格格式显示它。我有一个代码,但使用它我无法上传所有Excel文件。只能上传一种格式。

以下是该功能。但是有一些限制。

 public function check_excel($filename)
        {   
            $path='./assets/uploads/excel/'.$filename;
            $this->load->library('excel');  
            $inputFileType = PHPExcel_IOFactory::identify($path);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = PHPExcel_IOFactory::load($path);
            $sheet = $objPHPExcel->getSheet(0); 
            $highestRow = $sheet->getHighestRow();
            $highestColumn = $sheet->getHighestColumn();
            $xf[]='';
            $result[]='';
            $first_check='';
            $var_check=0;

            for ($row = 13; $row <= $highestRow; $row++)
            {           
                $xf[$row]=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex(); // Get sheet index value
                if($row>13 && $row<16) //This block check first kpi data expand or not
                { 
                    if($xf[$row-1]==$xf[$row]) //check parent and child sheet index value same 
                        $first_check='false';
                    if ($row==15) 
                    {
                        if($xf[$row]==$xf[$row-1] || $xf[$row]==$xf[$row-2]) // check the grand-child sheet index value same in parent and child
                            $first_check='false';
                        else
                        {   
                            $first_check='true';
                            $a=$row-2;
                            $b=$row-1;
                            $check_kpi=$objPHPExcel->getActiveSheet()->getCell('A'.$a)->getXfIndex(); 
                            $check_unit=$objPHPExcel->getActiveSheet()->getCell('A'.$b)->getXfIndex(); 
                            $check_sub_unit=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex(); 
                        }
                    }       
                }
                if($first_check=='true') //This block check second kpi to upto last kpi data expand or not 
                {
                    if($row>15)
                    {
                        if($var_check==1) // This block check the child data expand or not
                        {
                            if($check_unit!=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex())
                            {
                                $result[$row]='false';
                                break;
                            }
                        }
                        if($var_check==2) // this block check the grand - child data expand or not
                        {
                            if($check_sub_unit!=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex())
                            {
                                $result[$row]='false';
                                break;                          
                            }
                        }
                        if($xf[$row]!=$check_sub_unit)
                        {
                            if($xf[$row]!=$check_unit)
                                $var_check=1; // var_check value is one, the kpi is present
                            else
                                $var_check=2; // var_check value is two, the unit is present
                        }
                        else
                            $var_check=0; // var_check value is zero, the sub_unit is present
                    }   
                }
                else if($first_check=='false')
                {
                    $result[$row]='false';
                    break;
                }           
            }
            $return='true';
            for ($row = 13; $row <= $highestRow; $row++)
            {
                if(!empty($result[$row]))
                {
                    if($result[$row]=='false'){
                        $return='false';
                        break;
                    }                   
                }
            }
            return $return;
        }

4 个答案:

答案 0 :(得分:6)

听起来你正在使用一个使用固定列表的关系数据库(例如MySQL,Postgres等)。

您应该使用基于文档的数据库(例如CouchDB,Mongo等)。这将是最好的解决方案。

但是,如果您使用关系数据库,则可以使用EAV model

这是一个基本的例子:

  1. 为实体创建一个表(excel文件):EntityID,ExcelFileName
  2. 为属性(列信息)创建表:AttributeID,EntityID,AttributeName
  3. 为值创建一个表(excel行/列):ValueID,RowNumber,AttributeID,AttributeValue
  4. 缺点是AttributeValue没有特别输入(它只是varchar / text)。您可以通过向属性表添加“AttributeType”来解决此问题,然后在您的代码中使用该属性表来了解该列应包含的数据类型。但是,除非您事先知道Excel文件的内容/格式,否则您可能必须GUESS列的类型是什么......只要excel文件没有搞砸就不难。

    如果您只是显示导入的数据,这可能不是什么大问题。

    如果您有这样的需要,还有其他(更复杂的)方法可以实现EAV,包括带有类型列的方法。

答案 1 :(得分:4)

您是否尝试过PHPExcel

他们还有codeigniter library

这篇文章可能会让您感兴趣:how to use phpexcel to read data and insert into database?

答案 2 :(得分:0)

当然,您可以使用PHPExcel,但请查看其他数据格式。使用逗号分隔或制表符分隔值可以帮助您轻松解决问题。 Excel可以以这些简单格式保存数据表。无论如何,您无法在数据库中保存公式或条件格式。此外,它更快更强大,您可以使用LOAD DATA INFILE查询导入CSV文件。

答案 3 :(得分:0)

 <form action="<?= base_url();?>Asset_controller/asset" method="POST" enctype="multipart/form-data">
<input type="file" class="form-control" name="xlxx" id="file_asset" required  accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
</form>
    $file=$_FILES['xlxx'];
            $time=time();
            $dir_upload = 'public/images/'; //where you are uploading file 
            $new_name = $time . '-' . $file['name'];
            $path= $dir_upload.$new_name;
            $fileTypes = array('xls','xlsx'); // File extensions
            $fileParts = pathinfo(strtolower($file['name']));
            if (in_array($fileParts['extension'],$fileTypes)) 
            {
              $ok= move_uploaded_file($file['tmp_name'],$path);
                if ($ok) {}
                else
                {
                    $this->session->set_flashdata('err_msg','Check File extension');
                    redirect('Asset_controller/asset_check_inout');
                }
            }
            $fname=$path;

          $xlreader=PHPExcel_IOFactory::createReaderForFile($fname); //creating reader for reading purpose
          $xlobj=$xlreader->load($fname);// creating object by loading the given file

          $sheet=$xlobj->getSheet(0);
          $rows=$sheet->getHighestRow();

          $col=$sheet->getHighestColumn();
          $cols=PHPExcel_Cell::columnIndexFromString($col);
          $xlsheet=$sheet->toArray(null,true,true,false);
          $com=0;
          $trans=0;
          $this->db->trans_begin();
           for($k=1;$k<$rows;$k++)
          {
    $data=array(
                            'asset_name'=>$xlsheet[$k][0], //column name=>excel sheets column
                            'serial_no'=>$xlsheet[$k][1],
                            'invoice_number'=>$xlsheet[$k][2],
                            'invoice_dt'=>$xlsheet[$k][3],
                            'model'=>$xlsheet[$k][4],
                            'service_tag'=>$xlsheet[$k][5],
                            'amount'=>$xlsheet[$k][6],
                            'asset_desc'=>$xlsheet[$k][7],
                            'org_id'=>$xlsheet[$k][8],
                        );
                $this->db->insert('tablename',$data);
                 $trans=++$trans;
                 if ($this->db->affected_rows())
                    {   
                        $com=++$com;
                    }
          }         
            if ($com==$trans)
                    {
                        $this->db->trans_commit();
                        $this->session->set_flashdata('succ_msg',"Upload Complete,<b> $com </b>records updated");
                        redirect('Asset_controller/asset');//path
                    }
                    else
                    {  
                        $this->db->trans_rollback();
                        $this->session->set_flashdata('err_msg','DataBase Problem');
                        redirect('Asset_controller/asset');//path
                    }




        }
相关问题