如何使用phpword包以文本格式阅读laravel中的docs文件

时间:2017-08-16 20:59:32

标签: php ms-word laravel-5.4 docx phpword

我想知道如何使用外部包og phpword在laravel中运行docs文件,需要读取相同格式的docs文件,这是reada文本但格式不正确请以某种方式帮助我

这里是控制器代码,它没有正确格式化

public  function view_file($file_name)
{
    $file_entries=Fileentry::all();

    $file=public_path('storage\\'.$file_name);

    $phpWord = IOFactory::createReader('Word2007')->load($file);

    if(method_exists($phpWord, 'getSections')) {
        foreach ($phpWord->getSections() as $section) {

            $body = '';

            if (method_exists($section, 'getElements')) {

                foreach ($section->getElements() as $element) {
                    if (method_exists($element, 'getText')) {
                        $body .= $element->getText();
                    }
                    if (class_exists(Text::class)) {
                        if (method_exists($element, 'getText')) {

                            if (class_exists(TextBreak::class)) {
                                $body .= '<br/>';
                            }
                        }
                    }
                }
            }
            if (class_exists(TextBreak::class)) {
                $body .= '<br/>';
            }

            if (class_exists(Table::class)) {
                $body .= '<table border="2px">';
                if (method_exists($element, 'getRows')) {
                    $row = $element->getRows();
                    foreach ($row as $rows) {
                        $body .= '<tr>';
                        if (method_exists($element, 'getCell')) {
                            $cells = $rows->getCell();
                            foreach ($cells as $cell) {
                                $body .= '<td style="width:' . $cell->getWidth . '">';
                                $celements = $cell->getElements();
                                foreach ($celements as $celem) {
                                    if (class_exists(Text::class) == $celem) {
                                        $body .= $celem->getText();
                                    } else if (class_exists(TextRun::class) == $celem) {
                                        foreach ($celements as $text) {
                                            $body .= $text->getText();
                                        }
                                    }
                                }
                                $body .= '</td>';
                            }
                        }
                        $body .= '</tr>';
                    }
                }
            }

            if (class_exists(TextBreak::class)) {
                $body .= '<br/>';
            }

            return view('doc_view', compact('body', 'file_entries'));
        }
    }
}

0 个答案:

没有答案