search
Homephp教程php手册php生成word两种方法

1.正常的touch创建word 2.fopen 打开word 3.fwrite 写入word 并保存

这样会出现一个问题,如果写入的东西里面含有html代码的话,它将直接写入word而不是 排版了.

这个问题,需要在输出html代码头部加一段代码:

<?php
$headert=&#39;<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">&#39;; 
    $footer="</html>";
?>

比如你的内容是$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 =&#39;3"&#39;; 
    //设置字体 这 
    $word->selection->font->name =&#39;helvetica&#39;; 
    //设置字号 
    $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(&#39;aaa&#39;); 
    //保存 
    //$word->sections->add(1); 
    $word->documents[1]->saveas(dirname(__file__)."/create_test.doc"); 
    //退出 
    $word->quit(); 
?>

方法二,实例代码如下:

<?php
class word {
    function start() {
        ob_start();
        print &#39;<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">&#39;;
    }
    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);
    }
}
?>

调用方法,代码如下:

$word=new word; 
$word->start(); 
echo $cout; 
$wordname="word/".time().".doc"; 
$word->save($wordname);//保存word并且结束


本文地址:

转载随意,但请附上文章地址:-)

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.