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

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

以下是function。 但有一些限制。

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; } 

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

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

但是,如果您使用关系数据库卡住,则可以使用EAV模型 。

这里是一个基本的例子:

  1. 为实体(excel文件)创build一个表:EntityID,ExcelFileName
  2. 为属性(列信息)创build一个表:AttributeID,EntityID,AttributeName
  3. 为值(excel行/列)创build一个表:ValueID,RowNumber,AttributeID,AttributeValue

缺点是AttributeValue没有专门的types(只是varchar / text)。 你可以通过在属性表中添加一个“AttributeType”来解决这个问题,然后在你的代码中使用它来知道该列应该包含哪种types的数据。 但是,除非您事先知道Excel文件的内容/格式,否则您可能需要GUESS什么样的列是什么……只要excel文件没有混乱,这不是很难。

如果你只是显示导入的数据,这可能不是什么大不了的事情。

如果您有这样的需求,还有其他(更复杂的)实现EAV的方法,包括一个带有types列的方法。

你尝试过PHPExcel吗?

他们也有一个代码库 。

这篇文章可能会让你感兴趣: 如何使用phpexcel读取数据并插入数据库?

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

  <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 } }