phpcms 用phpexcel导入导出excel

2021-05-18 07:27

阅读:616

标签:defaults   amp   itemid   res   tca   open   富文本   default   mit   

html

 


    

导入excel表:


    

//导入的方法

 

 

public function public_add_excel()
    {
         $file_path=$_FILES[‘file_stu‘][‘tmp_name‘];
         require PC_PATH.‘libs/classes/PHPExcel.php‘;
         require PC_PATH.‘libs/classes/PHPExcel/IOFactory.php‘;
         require PC_PATH.‘libs/classes/PHPExcel/Reader/Excel2007.php‘;
         require PC_PATH.‘libs/classes/PHPExcel/Reader/Excel5.php‘;
        
        $this->db_hb2 = pc_base::load_model(‘huanbao2_model‘);
        
        
         move_uploaded_file($file_path, ‘./tmp.xls‘);
         $array=format_excel2array(‘./tmp.xls‘);
         //array_shift($array);
         //将数组入库按照官方方法入库
         //var_dump($array);die;
           foreach ($array as $key => $value) {
                $mzinfo=array();
                $mzinfo[‘c3‘]=$value[‘B‘];
                $mzinfo[‘com1‘]=$value[‘C‘];
                $mzinfo[‘carname‘]=$value[‘D‘];
                $mzinfo[‘fdj‘]=$value[‘E‘];
                $mzinfo[‘com2‘]=$value[‘F‘];
                $mzinfo[‘shangbiao‘]=$value[‘G‘];
                $mzinfo[‘jieduan‘]=$value[‘H‘];
                $mzinfo[‘cat‘]=$value[‘I‘];
                $time = (((double)$value[‘J‘])-25569)*24*60*60-28800;//时间的转换
                
                $mzinfo[‘time‘]=str_replace("00:00","",date(‘Y-m-d H:i‘,$time)) ;
                $mzinfo[‘flag‘]=2;
                $ids=$this->db_hb2->insert($mzinfo,true);
           }
           
           showmessage(L(‘operation_success‘), HTTP_REFERER);
                

    }

 

//format_excel2array

这个方法我写在phpcms/libs/functions/global.func.php里面

function format_excel2array($filePath=‘‘,$sheet=0){
        if(empty($filePath) or !file_exists($filePath)){die(‘file not exists‘);}
        $PHPReader = new PHPExcel_Reader_Excel2007();        //建立reader对象
        if(!$PHPReader->canRead($filePath)){
                $PHPReader = new PHPExcel_Reader_Excel5();
                if(!$PHPReader->canRead($filePath)){
                        echo ‘no Excel‘;
                        return ;
                }
        }
        $PHPExcel = $PHPReader->load($filePath);        //建立excel对象
        $currentSheet = $PHPExcel->getSheet($sheet);        //**读取excel文件中的指定工作表*/
        $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/
        $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/
        $data = array();
        for($rowIndex=1;$rowIndex
                for($colIndex=‘A‘;$colIndex
                        $addr = $colIndex.$rowIndex;
                        $cell = $currentSheet->getCell($addr)->getValue();
                        if($cell instanceof PHPExcel_RichText){ //富文本转换字符串
                                $cell = $cell->__toString();
                        }
                        $data[$rowIndex][$colIndex] = $cell;
                }
        }
        return $data;
    }

 

 

//导出的方法

public function daochuexcel()
    {
        require PC_PATH.‘libs/classes/PHPExcel.php‘;
        require PC_PATH.‘libs/classes/PHPExcel/Writer/Excel5.php‘;
        require PC_PATH.‘libs/classes/PHPExcel/Writer/Excel2007.php‘;
        require PC_PATH.‘libs/classes/PHPExcel/IOFactory.php‘;
        
        if(empty($_POST[‘ids‘])) showmessage(L(‘you_do_not_check‘));
        $where=" itemid in(".implode(",",$_POST[‘ids‘]).")";
        $result=$this->db_hb->select($where,‘*‘);
        //var_dump($result);die;
        //创建一个excel对象
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("ctos")
                    ->setLastModifiedBy("ctos")
                    ->setTitle("Office 2007 XLSX Test Document")
                    ->setSubject("Office 2007 XLSX Test Document")
                    ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                    ->setKeywords("office 2007 openxml php")
                    ->setCategory("Test result file");

            //set width  
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(8);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(10);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(25);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(50);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘F‘)->setWidth(10);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘G‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘H‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘I‘)->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension(‘J‘)->setWidth(30);

            //设置行高度  
            $objPHPExcel->getActiveSheet()->getRowDimension(‘1‘)->setRowHeight(22);

            $objPHPExcel->getActiveSheet()->getRowDimension(‘2‘)->setRowHeight(20);

            //set font size bold  
            $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
            $objPHPExcel->getActiveSheet()->getStyle(‘A2:J2‘)->getFont()->setBold(true);

            $objPHPExcel->getActiveSheet()->getStyle(‘A2:J2‘)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘A2:J2‘)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

            //设置水平居中  
            $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
            $objPHPExcel->getActiveSheet()->getStyle(‘A‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘B‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘D‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘F‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘G‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘H‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle(‘I‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //  
            $objPHPExcel->getActiveSheet()->mergeCells(‘A1:J1‘);

            // set table header content  
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue(‘A1‘, ‘环保数据报表汇总  时间:‘ . date(‘Y-m-d H:i:s‘))
                    ->setCellValue(‘A2‘, ‘ID‘)
                    ->setCellValue(‘B2‘, ‘车辆型号‘)
                    ->setCellValue(‘C2‘, ‘生产企业名称‘)
                    ->setCellValue(‘D2‘, ‘车辆名称‘)
                    ->setCellValue(‘E2‘, ‘发动机型号‘)
                    ->setCellValue(‘F2‘, ‘发动机生产厂家‘)
                    ->setCellValue(‘G2‘, ‘商标‘)
                    ->setCellValue(‘H2‘, ‘阶段‘)
                    ->setCellValue(‘I2‘, ‘车辆类别‘)
                    ->setCellValue(‘J2‘, ‘目录日期‘);

            // Miscellaneous glyphs, UTF-8  
                
            for ($i =0; $i
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘A‘ . ($i + 3), $result[$i][‘itemid‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘B‘ . ($i + 3), $result[$i][‘c3‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘C‘ . ($i + 3), $result[$i][‘com1‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘D‘ . ($i + 3), $result[$i][‘carname‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘E‘ . ($i + 3), $result[$i][‘fdj‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘F‘ . ($i + 3), $result[$i][‘com2‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘G‘ . ($i + 3), $result[$i][‘shangbiao‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘H‘ . ($i + 3), $result[$i][‘jieduan‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘I‘ . ($i + 3), $result[$i][‘cat‘]);
                $objPHPExcel->getActiveSheet(0)->setCellValue(‘J‘ . ($i + 3), $result[$i][‘time‘]);
                $objPHPExcel->getActiveSheet()->getStyle(‘A‘ . ($i + 3) . ‘:J‘ . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle(‘A‘ . ($i + 3) . ‘:J‘ . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
                $objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16);
            }


            // Rename sheet  
            $objPHPExcel->getActiveSheet()->setTitle(‘环保数据汇总表‘);


            // Set active sheet index to the first sheet, so Excel opens this as the first sheet  
            $objPHPExcel->setActiveSheetIndex(0);


            // Redirect output to a client’s web browser (Excel5)  
            ob_end_clean();//清除缓冲区,避免乱码
            header(‘Content-Type: application/vnd.ms-excel‘);
            header(‘Content-Disposition: attachment;filename="环保数据汇总表(‘ . date(‘Ymd-His‘) . ‘).xls"‘);
            header(‘Cache-Control: max-age=0‘);

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
            $objWriter->save(‘php://output‘);
        
    }

phpcms 用phpexcel导入导出excel

标签:defaults   amp   itemid   res   tca   open   富文本   default   mit   

原文地址:http://www.cnblogs.com/jiaoaozuoziji/p/7738228.html


评论


亲,登录后才可以留言!