PHP實現excel
導入php
- public function excel_put(){
-
- $path=$_FILES['file'];
- $filePath = "uploads/".$path["name"];
- move_uploaded_file($path["tmp_name"],$filePath);
-
-
- $data=array('B'=>'name','C'=>'pwd','D'=>'money1','E'=>'salt');
- $tablename='user1';
- $this->excel_fileput($filePath,$data,$tablename);
- }
- private function excel_fileput($filePath,$data,$tablename){
- $this->load->library("phpexcel");
- $PHPExcel = new PHPExcel();
- $PHPReader = new PHPExcel_Reader_Excel2007();
- if(!$PHPReader->canRead($filePath)){
- $PHPReader = new PHPExcel_Reader_Excel5();
- if(!$PHPReader->canRead($filePath)){
- echo 'no Excel';
- return ;
- }
- }
-
- $PHPExcel = $PHPReader->load($filePath);
-
-
- $currentSheet = $PHPExcel->getSheet(0);
-
- $allColumn = $currentSheet->getHighestColumn();
-
- $allRow = $currentSheet->getHighestRow();
-
-
- for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
-
-
-
- for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
- $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();
-
-
-
- if($currentColumn == 'A')
- {
-
- }else if($currentColumn <= $allColumn){
- $data1[$currentColumn]=$val;
- }
- }
- foreach($data as $key=>$val){
- $data2[$val]=$data1[$key];
- }
- $this->db->insert($tablename,$data2);
-
-
- }
-
- echo "導入成功";
- }
導出app
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:attachment;filename=123.xls");
-
- $array=$this->db->get("shop_address")->result_array();
- $str = "Id\tName\tPid\n";
- foreach ($array as $val) {
- $str .= $val['id'] . "\t" .$val['name'] . "\t" . $val['pid'] . "\n";
- }
- echo $str;
歡迎關注本站公眾號,獲取更多信息