Home >Backend Development >PHP Tutorial >Two ways to generate word in php_PHP tutorial

Two ways to generate word in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:09:17856browse

Two ways to generate word in php tutorial

1. Create word with normal touch

2.fopen open word

3.fwrite write word and save

There will be a problem if the written thing contains html code, it will be written directly into word instead of typesetting

This problem requires adding a piece of code at the head of the output html code

$headert=' xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';
$footer="";

For example, your content is $ text;

Then when writing $text=$header.$text.$footer;

In this way, the things in fck can be output according to the layout style!


Method 1

$word= new com("word.application") or die("unable to
create word document") ;
print "loaded word, version{$word->version}n";
$word->visible =0;
$word->documents->add();

//There is an error in setting the margin
// $word->selection->agesetup->rightmargin ='3"';

//Set the font
$word->selection->font->name ='helvetica';

//Set font size
$word->selection->font->size = 8 ;

//Set color
$word->selection->font->colorindex= 13; //wddarkred= 13

//Output to document
$word->selection->typetext("hello world ");
$range = $word->activedocument->range(0,0);
$table_t =$word-> activedocument->tables->add($range,3,4);
$table_t->cell(1,2)->range->insertafter('aaa');
/ /save
//$word->sections->add(1);
$word->documents[1]->saveas(dirname(__file__)."/create_test.doc") ;

//Exit
$word->quit();

?>

Method 2

class word
{
function start()
{
ob_start();
print'xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/tr/rec-html40">';
}
function save($path)
{
print "";
$data = ob_get_contents();
ob_end_clean();
$this-> ;wirtefile ($path,$data);
}
function wirtefile ($fn,$data)
{
$fp=fopen($fn,"wb");
fwrite ($fp,$data);
fclose($fp);
}
}
?>

Call method

$word=new word ;
$word->start();
echo $cout;
$wordname="word/".time().".doc";
$word->save( $wordname);//Save word and end


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444807.htmlTechArticlePHP tutorial two methods to generate word 1. Normal touch to create word 2.fopen to open word 3.fwrite to write word and save it. This will cause a problem if the written thing contains html code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn