PHPWord从数据库读取多于docx以生成新的docx

时间:2018-04-06 22:52:35

标签: php phpword phpoffice

我正在尝试从数据库中读取2个或更多docx文件,并在新的docx中编写所有文件,并可能在之后添加文本 但它只读取一个文件,甚至不读取循环下面的文本 我是php和phpword的新手 我正在使用netbean IDE 8.0.2

这是我的代码:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setThemeFontLang($languageEnGb);

try {
    include ('DbConnection.php');

    $GenratedFileID = "1";
    $GenFileName = '';
    $sql = "SELECT S.FileID as fileID, G.GenratedFileName as GenFile, L.FileName 
 as Fname , L.FilePath as FilePth,"
        . "G.GenratedFileName as GName "
        . "FROM sections S,genratedfiles G, fileslibrary L "
        . " where G.GenratedFileID = S.GenratedFileID "
        . "and S.FileID = L.FileID and G.GenratedFileID =" . 
    $GenratedFileID;
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {

        while ($row = $result->fetch_assoc()) {            //Read Library File
            $GenFileName = $row['GName'];
            $name = str_replace('/', '\\', $row['FilePth']);
            $source = __DIR__ . "\\{$name}";

            $phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
        }//end of while loop
        //  $section->addOLEObject($row['FilePth']);
        $section = $phpWord->addSection();
        $section->addTitle('Welcome to first database Genrated File', 1);
        $section->addText('created by me');
        $section->addTextBreak(2);


        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('Generated Files/' . $GenFileName . '.docx');
    }
} 
catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

mysqli_close($conn);

0 个答案:

没有答案