Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c

两盒软妹~` 提交于 2019-12-24 01:28:42

问题


I have the code snippet below

  //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';
}

How can i add text formatting to a cell value to bold, italic, font-size etc, I have tried as shown above but it does not work


回答1:


$myFontStyle = array('bold' => true, 'align' => 'center');

$table->addCell(1750)->addText("Testing", $myFontStyle, array('align' => 'center'));



回答2:


The following works for me:

$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,
    )
  );

The only diff is - I pass in an array and not a style defn...

HTH




回答3:


This should do it:

$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);


来源:https://stackoverflow.com/questions/18020814/formatting-a-text-in-a-table-cell-with-phpword-e-g-bold-font-size-e-t-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!