phpexcel导入导出

data = db('goods')->where('id<100')->limit(10)->select();
        if( PHP_SApi == 'cli' ) {
            die('this example should only be run from a web server');
        }
        require_once VENDOR_PATH . 'PHPExcel/PHPExcel.php';
    }

    public function index()
    {
        //dump($this->data);
        return view();
    }

//export
    public function export()
    {
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $arr = 'A,B,C,D,E,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z';
        $arr = explode(',' , $arr);
//        dump($arr);die;
        //设置标题
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue("A1" , 'id')
            ->setCellValue('B1' , '名称')
            ->setCellValue('C1' , '售价')
            ->setCellValue('D1' , '商场价格')
            ->setCellValue('E1' , '上架时间')
            ->setCellValue('F1' , '库存');
        //设置数据
        foreach( $this->data as $k => $v ) {
            $row = $k + 2;
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("A" . "$row" , $v['id'])
                ->setCellValue("B" . "$row" , $v['name'])
                ->setCellValue("C" . "$row" , $v['sell_price'])
                ->setCellValue("D" . "$row" , $v['market_price'])
                ->setCellValue("E" . "$row" , date('Y/m/d H:i' , $v['create_time']))
                ->setCellValue("F" . "$row" , $v['store_nums']);
        }
        $objPHPExcel->setActiveSheetIndex(0)->setTitle('数据');
        $objPHPExcel->setActiveSheetIndex(0);
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="01simple.xls"');
        header('Cache-Control: max-age=0');

// If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel , 'Excel5');
        $objWriter->save('php://output');
        exit;
        /*
        //create new phpexcel object
        $objPHPExcel = new \PHPExcel();
        // Set document properties 设置文档的属性
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->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");
// Add some data
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1' , 'Hello')
            ->setCellValue('B2' , 'world!')
            ->setCellValue('C1' , 'Hello')
            ->setCellValue('D2' , 'world!');
        // Miscellaneous glyphs, UTF-8
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A4' , 'Miscellaneous glyphs')
            ->setCellValue('A5' , 'éàèùâêîôûëïüÿäöüç');
// Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('Simple');
// 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)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="01simple.xls"');
        header('Cache-Control: max-age=0');

// If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel , 'Excel5');
        $objWriter->save('php://output');
        exit;
        */
    }

//import
    public function import()
    {
        $file = ROOT_PATH . 'a.xls';
        // 读取excel文件
        try {
            $inputFileType = \PHPExcel_IOFactory::identify($file);
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file);
        } catch( Exception $e ) {
            die('加载文件发生错误:"' . pathinfo($file , PATHINFO_BASENAME) . '": '.$e->getMessage());
        }

        // 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();//10
        $highestColumn = $sheet->getHighestColumn();//F
        // 获取一行的数据
        for ($row = 1; $row <= $highestRow; $row++){
            // Read a row of data into an array
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
            //这里得到的rowData都是一行的数据,得到数据后自行处理,我们这里只打出来看看效果
            var_dump($rowData);
            echo "
"; } } }

你可能感兴趣的:(phpexcel导入导出)