php生成word两种方法
1.正常的touch创建word 2.fopen 打开word 3.fwrite 写入word 并保存
这样会出现一个问题,如果写入的东西里面含有html代码的话,它将直接写入word而不是 排版了.
这个问题,需要在输出html代码头部加一段代码:
<?php $headert='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/tr/rec-html40">'; $footer="</html>"; ?>
Copy after login
比如你的内容是$text;
那么写入的时候$text=$header.$text.$footer;
这样的话fck里面的东西就能按排版的样式输出了!
方法一,实例代码如下:
<?php $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(); //设置边距 这个有错误 // $word->selection->agesetup->rightmargin ='3"'; //设置字体 这 $word->selection->font->name ='helvetica'; //设置字号 $word->selection->font->size = 8; //设置颜色 $word->selection->font->colorindex= 13; //wddarkred= 13 //输出到文档 $word->selection->typetext("hello world "); //开源代码phprm.com $range = $word->activedocument->range(0,0); $table_t =$word->activedocument->tables->add($range,3,4); $table_t->cell(1,2)->range->insertafter('aaa'); //保存 //$word->sections->add(1); $word->documents[1]->saveas(dirname(__file__)."/create_test.doc"); //退出 $word->quit(); ?>
Copy after login
方法二,实例代码如下:
<?php class word { function start() { ob_start(); print '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/tr/rec-html40">'; } function save($path) { print "</html>"; $data = ob_get_contents(); ob_end_clean(); $this->wirtefile($path, $data); } function wirtefile($fn, $data) { $fp = fopen($fn, "wb"); fwrite($fp, $data); fclose($fp); } } ?>
Copy after login
调用方法,代码如下:
$word=new word; $word->start(); echo $cout; $wordname="word/".time().".doc"; $word->save($wordname);//保存word并且结束
Copy after login
本文地址:
转载随意,但请附上文章地址:-)
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5055518 fails to install in Windows 10?
1 months ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
1 months ago
By DDD
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
Java Tutorial
1664
14


CakePHP Tutorial
1423
52


Laravel Tutorial
1317
25


PHP Tutorial
1268
29


C# Tutorial
1246
24

