如何使用phpword删除addtext函数之间的间距

时间:2015-10-14 14:30:46

标签: phpword

我有以下代码,但我无法弄清楚如何删除行空间或设置自定义行高等。

require_once '/opt/html/paradox/protected/extensions/PHPWord/src/PhpWord/Autoloader.php';
        \PhpOffice\PhpWord\Autoloader::register();
        $phpWord = new \PhpOffice\PhpWord\PhpWord();


        $filename = "Weekly-Report - " . date('d F Y') ;


        // adding the necessary font styles
        $phpWord->addFontStyle('font_default', array('name'=>'HelveticaNeueLT Std Lt', 'size'=>11, 'color'=>'000000'));

        $phpWord->addFontStyle('font_h2', array('name'=>'HelveticaNeueLT Std Med', 'size'=>14, 'color'=>'6D6D6D'));
        $phpWord->addFontStyle('font_h3', array('name'=>'HelveticaNeueLT Std Med', 'size'=>12, 'color'=>'949494'));

        //adding the necessary header/title styles
        $phpWord->addTitleStyle(1, array('name'=>'Cambria (Headings)', 'size'=>16, 'align'=>'center','underline'=>'UNDERLINE_SINGLE')); //h1
        //$phpWord->addTitleStyle(2, "font_h2"); //h2
        //$phpWord->addTitleStyle(3, "font_h3"); //h3

        //adding the necessary paragraph styles
        $phpWord->addParagraphStyle('paragraph_default', array('spaceBefore' => 0, 'spaceAfter' => 0));
        $paragraphOptions = array( 
                'spaceBefore' => 0, 'spaceAfter' => 0
        );

        $fontStyleSubHeading = new \PhpOffice\PhpWord\Style\Font();
        $fontStyleSubHeading->setBold(true);
        $fontStyleSubHeading->setName('Calibri');
        $fontStyleSubHeading->setSize(11);

        //############################### STARTING DOCUMENT AND DEFINING STYLES ###############################

        /* Note: any element you append to a document must reside inside of a Section. */
        $section = $phpWord->addSection();


        $section->addTitle(
                htmlspecialchars('Weekly Multi-Client Project Status Report '. date('d/M/Y'))
        );



        //$myTextElement->setFontStyle($fontStyleHeading);


        $criteria = new CDbCriteria;
        $criteria->select = "PROJECT, PERCENT, ExpectedCompletionDate, PROJINFO";
        $criteria->compare('NES',1);
        $criteria->addCondition('PERCENT < 100');
        $criteria->compare('PROJCODE','W');
        $criteria->compare('deleted','0');
        $projects = Projects::model()->findAll($criteria);

        foreach ($projects as $project) {

            $myTextElement = $section->addText(
                    htmlspecialchars($project->PROJECT . ":")

            );
            $myTextElement->setFontStyle($fontStyleSubHeading);

            // Adding Text element to the Section having font styled by default...
            $percent = round($project->PERCENT,2);
            $section->addText(
                    htmlspecialchars('Total % Complete - '.$percent),
                    $paragraphOptions
            );

            $section->addText(
                    htmlspecialchars($project->PROJINFO)
            );

            $myTextElement = $section->addText(
                    htmlspecialchars('Action items for SI')
            );
            $myTextElement->setFontStyle($fontStyleSubHeading);
            $section->addText(
                    htmlspecialchars('None')
            );          


            $myTextElement = $section->addText(
                    htmlspecialchars('Action items for MC')
            );
            $myTextElement->setFontStyle($fontStyleSubHeading);
            $section->addText(
                    htmlspecialchars('None')
            );

        }

它应该是一个标题,每个部分的每一个都应该是间隔开的,并且shoudl中的所有行都没有空格。

1 个答案:

答案 0 :(得分:1)

您正在将段落选项错误地提供给$ section-&gt; addText()函数。第二个参数是字体样式,即您需要将函数调用更新为:

@Entity
@Table(name="user")  
public class User {

  @ID
  public Long id;

  @Column(name="userCode")
  public String userCode;

  @ElementCollection
  @CollectionTable(name = "address", joinColumns = @JoinColumn(name = "user_id"))
  public List<Address> addressList;

  ....
}

@Embeddable  
public class Address {

  @EmbeddedId
  @Column(name = "id")
  public Long id;

  @Column(name="userId")
  public Long userId;

  @Column(name="address-line-1")
  public String addressLine1;

  @Column(name="address-line-2")
  public String addressLine2;

  @ElementCollection
  @CollectionTable(name = "phone", joinColumns = @JoinColumn(name = "user_id"))
  protected List<Phone> phoneList;

  ....
}

@Embeddable 
public class Phone {

  @EmbeddedId
  @Column(name = "id")
  public Long id;

  @Column(name="contact_no")
  public String contactNo;

  @Column(name="country_code")
  public int countryCode;

  @Column(name="address_id")
  public int addressId;

  ....
}

更新:

同时使用你的字体样式和段落样式,你可以将它们作为参数而不是使用setFontStyle函数(即,在将样式作为参数时,你根本不需要$ myTextElement变量需要时):

 $section->addText(
     htmlspecialchars('Total % Complete - '.$percent),
     null,
     $paragraphOptions
 );