【问题标题】:Try to use PhpWord on Laravel can someone helps?尝试在 Laravel 上使用 PhpWord 有人可以帮忙吗?
【发布时间】:2021-06-20 16:49:29
【问题描述】:

我第一次尝试使用 PhpWord 所以我写了这段代码,但它不起作用我认为问题我不知道如何获取页面数据

所有链接都运行良好 我对如何获取诸如查询之类的计划数据感到困惑

我的代码:

public function getDecumentReport(Request $request)
{

    // get the Page Data
    $datas = Decument::orderBy('id', 'ASC')->get();

    // Get the Word Document
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $document = $phpWord->loadTemplate('doc/dec_plan.docx');

    $management = Management::find($data->management);
    if($management != null) {
        $management = $management->name;
    } else {
        $management = "";
    }

    $department = Department::find($data->department);
    if($department != null) {
        $department = $department->name;
    } else {
        $department = "";
    }

    $document->setValue('management', ($management) );
    $document->setValue('department', ($department) );
    //---Use This Part Inside Foreach and Use a Word Table
    if ($datas) {
        foreach ($datas AS $data) {
            $document->setValue('name', ($datas->name));
            $document->setValue('describ', ($data->describ));
            $document->setValue('boll', ($data->boll));
            $document->setValue('type', ($data->place));
            $document->setValue('date', ($data->date));
            $document->setValue('unit', ($data->unit));
            $document->setValue('stitaiom', ($data->stitaiom));
        }
    }

    $name = 'Document' . time() . '.docx';
    $document->saveAs( "dec_plan/" . $name);
    $file = "dec_plan/{$name}";

    $headers = array(
        //'Content-Type: application/msword',
       'Content-Type: vnd.openxmlformats-officedocument.wordprocessingml.document'
   );

    $response = Response::download($file, $name, $headers);
    return $response;
}

页面是: that Schedule I want to export to word

【问题讨论】:

    标签: php mysql laravel phpword


    【解决方案1】:

    在 PHPWord 中使用 XML:

    class DecumentController extends Controller {
        public function DecumentPrintWord($id) {
            $datas = Decument::orderBy('id', 'ASC')->get();
    
            $phpWord    =   new \PhpOffice\PhpWord\PhpWord();
            
            $sectionStyle = array(
                'orientation'       =>      'landscape',
                'marginTop'         =>      600,
                'marginRight'       =>      600,
                'marginBottom'      =>      600,
                'marginLeft'        =>      600,
                'text-align'        =>      'right'
            );
    
            $section = $phpWord->addSection($sectionStyle);
    
            $all_xml = "";
            if ($datas) {
                $all_xml .= "<w:tbl>
                    <w:tblPr>
                        <w:tblStyle w:val='TableGrid'/>
                        <w:bidiVisual/>
                        <w:tblW w:w='0' w:type='auto'/>
                        <w:tblBorders>
                            <w:top w:val='none' w:sz='0' w:space='0' w:color='auto'/>
                            <w:left w:val='none' w:sz='0' w:space='0' w:color='auto'/>
                            <w:bottom w:val='none' w:sz='0' w:space='0' w:color='auto'/>
                            <w:right w:val='none' w:sz='0' w:space='0' w:color='auto'/>
                        </w:tblBorders>
                        <w:tblLook w:val='04A0' w:firstRow='1' w:lastRow='0' w:firstColumn='1' w:lastColumn='0' w:noHBand='0' w:noVBand='1'/>
                    </w:tblPr>
                    <w:tr w:rsidR='00292628' w:rsidTr='000C6BFD'>
                        <w:trPr>
                            <w:trHeight w:val='1000'/>
                        </w:trPr>";
                foreach($datas AS $data) {
                    $all_xml .= "<w:tc>
                        <w:tcPr>
                            <w:tcW w:w='".$sign_cwidth."' w:type='dxa'/>
                            <w:vAlign w:val='center'/>
                        </w:tcPr>
                        <w:p w:rsidR='00001066' w:rsidRPr='00001066' w:rsidRDefault='005C41BA' w:rsidP='00001066'>
                            <w:pPr>
                                <w:bidi/>
                                <w:jc w:val='center'/>
                                <w:spacing w:before='60' w:after='60' w:line='180' w:lineRule='auto'/>
                            </w:pPr>
                            <w:r>
                                <w:rPr>
                                    <w:rFonts w:asciiTheme='majorBidi' w:hAnsiTheme='majorBidi' w:cstheme='majorBidi' w:hint='cs'/>
                                    <w:b/>
                                    <w:bCs/>
                                    <w:rtl/>
                                </w:rPr>
                                <w:t>".$data->place."</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>";
                }
                $all_xml .= "</w:tr></w:tbl>";
            }
    
            //---Load Items In Each Page End
            $section->addText($all_xml);
    
            $footer = $section->addFooter();
            $footer->addPreserveText('{NUMPAGES} '.trans('from').' {PAGE} '.trans('to').'', array('color' => 'black'), array('align' => 'center'));
    
            $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
            try {
                $objWriter->save(storage_path('MyPHPWord.docx'));
            } catch (Exception $e) {}
            return response()->download(storage_path('MyPHPWord.docx'));
        }
    }
    

    首先创建您的word文件模板并压缩您的word文件并获取word文件的xml,如下所示:

    您可以复制您的 xml 文件并在以下站点中安排您的代码: https://codebeautify.org/xmlviewer

    注意:记住只复制xml文件的表格部分,“”标签内的代码,不要复制xml文件的多余部分。

    【讨论】:

    • 谢谢你,但我在 get() 中写了问题,它没有带来我认为应该有一些查询语句或其他东西的数据。我会尝试你的方式,但希望在不使用 XML 的情况下获得另一个答案。
    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多