如何通过phpword库读取表格单元格内容

这是我目前的代码


$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

$sections = $phpWord->getSections();

foreach ($sections as $s) {

    $els = $s->getElements();

    foreach ($els as $e) {

        switch (get_class($element)) {

            case 'PhpOffice\PhpWord\Element\TextRun':

                $this->logger->debug("TextRun");

                $this->getTextFromTextRun($element);

                break;


            case 'PhpOffice\PhpWord\Element\Table':

                $this->logger->debug("Table");

                $this->iterateOverRows($element);

                break;

        }

    }

}



private function getTextFromTextRun($element) {

    for ($index = 0; $index < $element->countElements(); $index++) {

        $textRunElement = $element->getElement($index);

        $text = $textRunElement->getText();

        if (strlen($text) > 0) {

            $this->logger->debug("TextRun Text: " . $text);

        }

    }

}


private function iterateOverRows($table) {

    $rows = $table->getRows();

    foreach ($rows as $row) {

        foreach ($row->getCells() as $cell) {

            // What to do here???

        }

    }

}

我不知道如何从单元格中获取内容?


这些是单元对象中可用的 getter 方法:


获取评论范围结束

获取评论范围开始

获取文档部分

获取文档部分ID

获取元素

获取元素Id

获取元素索引

获取元素

获取嵌套级别

获取父级

获取PHPWord

获取关系Id

获取SectionId

获取样式

获取轨迹更改

获取宽度


撒科打诨
浏览 114回答 1
1回答

阿晨1998

已经在这里解决了这个问题:private function iterateOverRows($table) {&nbsp; &nbsp; $rows = $table->getRows();&nbsp; &nbsp; foreach ($rows as $row) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($row->getCells() as $cell) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $els = $cell->getElements();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($els as $e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->switchElements($e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}private function switchElements($element) {&nbsp; &nbsp; switch (get_class($element)) {&nbsp; &nbsp; &nbsp; &nbsp; case 'PhpOffice\PhpWord\Element\TextRun':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->getTextFromTextRun($element);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'PhpOffice\PhpWord\Element\Table':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->iterateOverRows($element);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}private function getTextFromTextRun($element) {&nbsp; &nbsp; for ($index = 0; $index < $element->countElements(); $index++) {&nbsp; &nbsp; &nbsp; &nbsp; $textRunElement = $element->getElement($index);&nbsp; &nbsp; &nbsp; &nbsp; switch (get_class($textRunElement)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'PhpOffice\PhpWord\Element\Text':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'PhpOffice\PhpWord\Element\TextRun':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $text = $textRunElement->getText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strlen($text) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->logger->debug("Text: " . $text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->getFont($textRunElement);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'PhpOffice\PhpWord\Element\TextBreak':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP