我从一位较老的Stack Over Flow Question那里找到了建议,我在让它开始工作时遇到了困难。我添加了他们描述的方法,但是生成的word文件没有换行符(在解压缩之后,通过视觉和检查docx文件)。没有错误,所以这个方法似乎是在做一些事情,而不是我想要的。我之所以这样做,是因为我需要\n字符,而且我使用PHPRtfText确实让它工作得很好,但在兼容性方面存在问题,而且它作为Word2007文件似乎工作得更好,因此我相应地转换了代码。不过,也许我把它放在了错误的位置,但下面是修改后的函数:
/lib/PHPWord/Writer/Word2007/Base.php:
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun)
{
$elements = $textrun->getElements();
$styleParagraph = $textrun->getParagraphStyle();
$SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
$objWriter->startElement('w:p');
if($SpIsObject)
{
$this->_writeParagraphStyle($objWriter, $styleParagraph);
}
elseif(!$SpIsObject && !is_null($styleParagraph))
{
$objWriter->startElement('w:pPr');
$objWriter->startElement('w:pStyle');
$objWriter->writeAttribute('w:val', $styleParagraph);
$objWriter->endElement();
$objWriter->endElement();
}
if(count($elements) > 0)
{
foreach($elements as $element)
{
if($element instanceof PHPWord_Section_Text)
{
$this->_writeText($objWriter, $element, true);
}
elseif($element instanceof PHPWord_Section_Link)
{
$this->_writeLink($objWriter, $element, true);
}
}
}
elseif($element instanceof PHPWord_Section_TextBreak)
{
$objWriter->writeElement('w:br');
}
$objWriter->endElement();
}我是通过以下方式引用它的:
test1.php:
$PHPWord = new PHPWord();;
$PHPWord->setDefaultFontName('Calibri');
$PHPWord->setDefaultFontSize(11);
$section = $PHPWord->createSection();
$textrun = $section->createTextRun();
$textrun->addText( "---------------");
$textrun->addTextBreak();
$textrun->addText( "$name", array('bold'=>true ) );最后在/lib/PHPWord/Section/TextRun.php:中
public function addTextBreak($count = 1)
{
for($i=1; $i<=$count; $i++)
{
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
}
}还有什么需要我帮忙的吗?我真的需要它来工作,不能再把这个类改变成其他的东西。
我对文本进行了很大的格式化,因此需要文本,但是我需要在每一行之后使用\n,而不是一个部分,因为它需要相同的段落,所以使用\n\n是不能接受的。
发布于 2016-02-02 18:59:27
我当然认为我试过了,但也许我并没有把它保存在网络服务器上,但是把它移到更高的位置
if(count($elements) > 0)
{
foreach($elements as $element)
{
if($element instanceof PHPWord_Section_Text)
{
$this->_writeText($objWriter, $element, true);
}
elseif($element instanceof PHPWord_Section_Link)
{
$this->_writeLink($objWriter, $element, true);
}
elseif($element instanceof PHPWord_Section_TextBreak)
{
$objWriter->writeElement('w:br');
}
}
}
$objWriter->endElement();
}https://stackoverflow.com/questions/35065575
复制相似问题