2017-05-11 18:19:50 2784次浏览 6条回答 0 悬赏 10 金钱

PHPExcel 导出

QQ:1844594430

补充于 2017-05-11 18:54

`

/**
 *
 * 导出Excel
 */

public function actionExport($month){//导出Excel

    $xlsName  = "Staffstatic";
    $xlsCell  = array(
        array('id','序号'),
        array('staffName','姓名'),
        array('staffCode','操作员'),
        array('month','考核月份'),
        array('total','业务量'),
        array('compute','业务量计算数')
    );
 $laws=Protfolio::findOne(1);
 $compute=Staffstatic::find()->select([
     "id",
     "staffCode",
     "staffName",
     "month",
     "createTime",
     "sum(total)",
 ])->groupBy(['staffName','staffCode'])->where(['month'=>$month])->asArray()->all();
 $xlsData=null;
 foreach ($compute as $key=>$v){
     $xlsData[]=array(
         "id"=>$v["id"],
         "staffCode"=>$v["staffCode"],
         "staffName"=>$v["staffName"],
         "month"=>$v["month"],
         "createTime"=>$v["createTime"],
         "total"=>$v["sum(total)"],
         "compute"=>$v["sum(total)"]*$laws['protfolio']
     );
    }

    $this->exportExcel($xlsName,$xlsCell,$xlsData);
}

public function exportExcel($xlsName,$xlsCell,$xlsData){

    header("Content-type:text/html;charset=utf-8");
    set_time_limit(0);
    require dirname(dirname(__DIR__)).'/vendor/PHPExcel/PHPExcel.php';
    $xlsTitle = iconv('utf-8', 'gb2312', $xlsName);//文件名称
    $fileName = date('YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
    $cellNum = count($xlsCell);
    $dataNum = count($xlsData);
    $objPHPExcel = new \PHPExcel();
    $objPHPExcel = new \PHPExcel();
    $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

// $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格

     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $xlsName.'  Export time:'.date('Y-m-d H:i:s'));
    for($i=0;$i<$cellNum;$i++){
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $xlsCell[$i][1]);
    }
    // Miscellaneous glyphs, UTF-8
    for($i=0;$i<$dataNum;$i++){
        for($j=0;$j<$cellNum;$j++){
            $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $xlsData[$i][$xlsCell[$j][0]]);
        }
    }

    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
    header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    exit();
    $objWriter->save('php://output');

}

`

补充于 2017-05-12 18:07

我终于搞定了 谢谢你们宝贵的意见 提议

  • 回答于 2017-05-11 18:26 举报

    ....好歹描述要详细点额。红包就不必了,肯定是你不会用这个类

  • 回答于 2017-05-11 20:26 举报

    照它的例子写呗,这货给了不少的例子

  • 回答于 2017-05-11 20:28 举报

    你的学费不止该交在这里

  • 回答于 2017-05-11 21:02 举报

    一个比较简单的例子,你参考一下

    $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');
    
    1 条回复
    回复于 2017-05-12 10:35 回复

    谢谢你 我试试

    觉得很赞
  • 回答于 2017-05-12 09:59 举报
    1 条回复
    回复于 2017-05-12 10:36 回复

    3Q 我试试

  • 回答于 2017-05-12 18:05 举报

    之前还想写个教程的,自己当时也是研究好久,结果这编辑器太不好用,写了几次都错误放弃了。。。

您需要登录后才可以回答。登录 | 立即注册
ali
主管

ali 乌鲁木齐

注册时间:2016-12-21
最后登录:2024-10-17
在线时长:17小时39分
  • 粉丝3
  • 金钱10
  • 威望40
  • 积分580

热门问题