【问题标题】:Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c使用 PHPWord 格式化表格单元格中的文本,例如粗体、字体、大小等
【发布时间】:2023-03-14 10:43:01
【问题描述】:

我下面有代码sn-p

  //create a new word document

    $word= new PHPWord();

    //create potrait orientation
    $section=$word->createSection();   


    $table = $section->addTable();
    $word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
    //header row
    $table->addRow(400, array('bgColor'=>'dbdbdb'));
    $table->addCell(2000, array('bgColor'=>'dbdbdb'))->addText('Cell 1','rStyle');
    $table->addCell(3500, array('bgColor'=>'dbdbdb'))->addText('Cell 1');
    $table->addCell(1500, array('bgColor'=>'dbdbdb'))->addText('Cell 1','rStyle');
    $table->addCell(2000, array('bgColor'=>'dbdbdb'))->addText('Cell 1');       

    // Save File
    $objWriter = PHPWord_IOFactory::createWriter($word, 'Word2007');
    $objWriter->save('Text.docx');
    echo 'Text.docx created successfully';
}

如何将单元格值的文本格式添加为粗体、斜体、字体大小等,我已尝试如上所示,但它不起作用

【问题讨论】:

    标签: php phpword


    【解决方案1】:
    $myFontStyle = array('bold' => true, 'align' => 'center');
    
    $table->addCell(1750)->addText("Testing", $myFontStyle, array('align' => 'center'));
    

    【讨论】:

      【解决方案2】:

      以下对我有用:

      $tdwidth = 1440 * 3;
      $table
        ->addCell($tdwidth)
        ->addText(
          "TO",
          array(
            'size' => 12,
            'bold' => true,
            'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE
          )
        );
      $table
        ->addCell($tdwidth, array('align'=>'left'))
        ->addText(
          "FROM",
          array(
            'size' => 12,
            'bold' => true,
            'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE,
          )
        );
      

      唯一的区别是 - 我传入一个数组而不是样式定义...

      HTH

      【讨论】:

        【解决方案3】:

        应该这样做:

        $word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
        $centered= array('align'=>'center');
        
        //header row
        $table->addRow(400, array('bgColor'=>'dbdbdb'));
        $table->addCell(2000, array('bgColor'=>'dbdbdb'))->addText('Cell 1','rStyle', $centered);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-21
          • 1970-01-01
          • 2013-02-03
          • 1970-01-01
          • 2014-05-15
          • 1970-01-01
          • 2012-04-10
          相关资源
          最近更新 更多