PHP two ways to generate word _php tutorial

Source: Internet
Author: User
PHP tutorials Generate word two ways

1. Create Word with normal touch

2.fopen Open Word

3.fwrite write to Word and save

This will cause a problem if you write something that contains HTML code, it will be written directly to Word instead of typeset.

This problem requires a piece of code in the header 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 $text= $header when writing. $text. $footer;

In this way, the contents of FCK can be output according to the layout style!


Method One

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

Set margin this has an error
$word->selection->agesetup->rightmargin = ' 3 ";

Set the font for this
$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 (->range->insertafter) (' AAA ');
Save
$word->sections->add (1);
$word->documents[1]->saveas (DirName (__file__). " /create_test.doc ");

Exit
$word->quit ();

?>

Method Two

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);
}
}
?>

Calling methods

$word =new Word;
$word->start ();
Echo $cout;
$wordname = "word/". Time (). ". Doc ";
$word->save ($wordname);//save Word and end


http://www.bkjia.com/PHPjc/444807.html www.bkjia.com true http://www.bkjia.com/PHPjc/444807.html techarticle PHP Tutorial generates word two ways 1. Normal touch Create word 2.fopen open Word 3.fwrite write word and save this will cause a problem if you write something inside that contains HTML code ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.