2017-09-13 36 views
1

我是laravel中的新成员,我想使用wordphp包编辑word文档。如何解决警告laravel 5.4中的非法字符串偏移量?

要试试我保持一个现有的代码来自互联网,但一旦我运行它,我得到这个错误:

(1/1) ErrorException 
Illegal string offset 'w:compatSetting' 
in Settings.php (line 173) 
at HandleExceptions->handleError(2, 'Illegal string offset \'w:compatSetting\'', 'C:\\wamp\\www\\Stage_2\\vendor\\phpoffice\\phpword\\src\\PhpWord\\Writer\\Word2007\\Part\\Settings.php', 173, array('compatibility' => object(Compatibility))) 
in Settings.php (line 173) 

这里是我 类editWordController功能编辑:

public function edit(){ 

    $phpWord = new \PhpOffice\PhpWord\PhpWord(); 
    $section = $phpWord->addSection(); 
    $section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. ' 
    . 'The important thing is not to stop questioning." ' 
    . '(Albert Einstein)' 
    ); 

$section->addText(
    '"Great achievement is usually born of great sacrifice, ' 
    . 'and is never the result of selfishness." ' 
    . '(Napoleon Hill)', 
    array('name' => 'Tahoma', 'size' => 10) 
); 

$fontStyleName = 'oneUserDefinedStyle'; 
$phpWord->addFontStyle(
    $fontStyleName, 
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true) 
); 

$section->addText(
    '"The greatest accomplishment is not in never falling, ' 
    . 'but in rising again after you fall." ' 
    . '(Vince Lombardi)', 
    $fontStyleName 
); 

// Adding Text element with font customized using explicitly created font style object... 
$fontStyle = new \PhpOffice\PhpWord\Style\Font(); 
$fontStyle->setBold(true); 
$fontStyle->setName('Tahoma'); 
$fontStyle->setSize(13); 
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'); 
$myTextElement->setFontStyle($fontStyle); 
// Saving the document as OOXML file... 
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); 
try { 
    $objWriter->save(storage_path('helloWorld.docx')); 
}catch(Exception $e) 
{} 
return reponse()->download(storage_path('helloWorld.docx')); 
} 

这个错误是什么意思,我该如何解决?

回答

7

供应商/ phpoffice/phpword/src目录/ PhpWord /作家/ Word2007中/零件/ settings.php配置

$this->settings['w:compat']['w:compatSetting'] = array('@attributes' => array(
    'w:name' => 'compatibilityMode', 
    'w:uri'  => 'http://schemas.microsoft.com/office/word', 
    'w:val'  => $compatibility->getOoxmlVersion(), 
)); 

替换此代码

$this->settings['w:compat'] = array('@attributes' => array(
    'w:name' => 'compatibilityMode', 
    'w:uri'  => 'http://schemas.microsoft.com/office/word', 
    'w:val'  => $compatibility->getOoxmlVersion(), 
)); 

这是为我工作

相关问题