[Php Tutorial] sharing htmlword interchange methods in PHP

Source: Internet
Author: User
[Php Tutorial] sharing htmlword interchange methods in PHP

In php development, how does php implement mutual conversion of html word? Is there any way? The following describes how php transfers html to word. this method has been tested. the html page code should be written in php, but it can run properly. check the program.

Class word {

Function start (){

Ob_start ();

Echo'

Xmlns: w = "urn: schemas-microsoft-comffice: word"

Xmlns = "http://www.w3.org/TR/REC-html40"> ';

}

Function save ($ path)

{

Echo"";

$ Data = ob_get_contents ();

}

Function wirtefile ($ fn, $ data ){

$ Fp = fopen ($ fp, $ data );

Fwrite ($ fp, $ data );

}

}

$ Html ='

Name Gender Age Hobbies Remarks
Zhang San Male 32 Football None

Li Si

Male 43 Basketball None

';

// Batch generate

// For ($ I = 1; $ I <3; $ I ++ ){

$ Word = new word ();

$ Word-> start ();

Descriwordname='personal test phpgenerate Word.doc ';

Echo $ html;

$ Word-> save ($ wordname );

Header ('content-type: application/word ');

Header ('content-Disposition: attachment; filename = '. $ wordname .'');

// Readfile ($ wordname );

Ob_flush (); // refresh the cache before each execution

Flush ();

//}

?>

Below is a piece of code for php to convert word to html.

To solve this problem perfectly, we 'd better use windows office software to convert office to pdf or html. libreoffice cannot be converted perfectly, and wps has no APIs.

Check whether the com MODULE is enabled. if there is a com_dotnet module in phpinfo, it indicates that it is enabled. if not, modify php. ini,
The code is as follows:

Com. allow_dcom = true

The comment above is removed, and the restart will be OK. the Official php website said that the com MODULE was built in before php5.4.5, but not necessarily all. the official website's php 5.3.39, the com module is not built in.
If it is not a built-in module, add it to php. ini, provided that the extension is available in your ext folder.

The code is as follows:

Extension = php_com_dotnet.dll

Then restart the instance.

Function word2html ($ wordname, $ htmlname)

{

$ Word = new COM ("word. application") or die ("Unable to instancate Word ");

$ Word-> Visible = 1;

$ Word-> Documents-> Open ($ wordname );

$ Word-> Documents [1]-> SaveAs ($ htmlname, 8 );

$ Word-> Quit ();

$ Word = null;

Unset ($ word );

}

Word2html ('d:/www/test/6.docx ', 'd:/www/test/6.html ');

Note:

1. the converted html is messy to view the source code.

2. winword.exe will be used in the conversion process

3. if the page is being loaded, rename the document and re-convert it.

The above is all the content in this article. if you are interested, you can try it by yourself. if you have a better way to achieve mutual conversion of html word, please share it with you.

Recommended reading: a common analysis method for the Infinitus classification of data sorting in php


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.