PHPWord 导出生成word

function outword(){
        vendor('PHPWord.PHPWord');
        vendor('PHPWord.PHPWord.Shared.Font');
        $PHPWord = new \PHPWord();
        $section = $PHPWord->createSection();//添加默认页
        //定义table样式
        $styleTable = array('borderSize'=>6,'borderColor'=>'006699','cellMargin'=>80);
        //第一行样式
        $styleFirstRow = array('borderBottomSize'=>18,'borderBottomColor'=>'0000FF','bgColor'=>'66BBFF');
        //定义列样式
        $styleCell = array('valign'=>'center');
        //使用addCell的第二个参数来给单元格设置样式
        //$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
        //为第一行定义字体样式
        $fontStyle = array('bold'=>true, 'align'=>'center');
        //添加table样式
        $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
        //添加表格table
        $table = $section->addTable('myOwnTableStyle');
        
        $PHPWord->setDefaultFontName('仿宋'); // 全局字体
        $PHPWord->setDefaultFontSize(16);     // 全局字号为3号
        //添加行的高度
        $table->addRow(900);
        //添加列
        $table->addCell(1000, $styleCell)->addText('品牌id', $fontStyle);
        $table->addCell(2000, $styleCell)->addText('品牌名称', $fontStyle);
        $table->addCell(1000, $styleCell)->addText('父类', $fontStyle);
        $table->addCell(1000, $styleCell)->addText('状态', $fontStyle);
        $table->addCell(2000, $styleCell)->addText('添加时间', $fontStyle);
        $table->addCell(4000, $styleCell)->addText('图片', $fontStyle);
        
        //准备数据
        $brand = M('Brand');
        $where = "status = '1'";
        $list = $brand->where($where)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        //dump($list);exit;
        //遍历数据
        foreach ($list as $k=>$v){
            $table->addRow();
            $table->addCell(2000)->addText($v['id']);
            $table->addCell(2000)->addText($v['name']);
            $table->addCell(2000)->addText($v['pid']);
            $table->addCell(2000)->addText($v['status']);
            $table->addCell(2000)->addText($v['addtime']);
            $table->addCell(2000)->addText($v['logo']);
        }
        
        $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $objWriter->save('AdvancedTable.docx');
        $fileName = "word报表".date("YmdHis");
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition:attachment;filename=".$fileName.".docx");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $objWriter->save('php://output');
        
    }
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值