冰凌尘埃

对于明天,你是否有勇气有能力去承担?

用户操作
[即时聊天] [发私信] [加为好友]
冰凌尘埃ID:fzjw
21125次访问,排名5943(-2),好友1人,关注者13人。
己所不欲,勿施于人
fzjw的文章
原创 19 篇
翻译 2 篇
转载 2 篇
评论 39 篇
冰凌尘埃的公告

#include<string>

string message == none;

最近评论
allstar:哦 那谢谢你了。 我非常喜欢这个配置。 可惜一不小心,被我弄丢了。 现在找了好久丢哦找不到了
bob:链接的网站停了,我也想要一份,请更新网站或传到信箱:ren_shijun@163.com
xiaofeng:忘了存哪里了......

等我找到后发给你
allstar:连接都失效了. 不知道你还有没有? 我用过一段时间, 非常喜欢他的简洁, 后来重装弄丢了, 你有的话可以发给我吗?
邮 箱:hewenxin81#yahoo.com.cn
请把#改为@ 谢谢!
allstar:连接都失效了. 不知道你还有没有? 我用过一段时间, 非常喜欢他的简洁, 后来重装弄丢了, 你有的话可以发给我吗?
邮 箱:hewenxin81#yahoo.com.cn
请把#改为@ 谢谢!
文章分类
收藏
    相册
    我的相册
    BSD/Linux
    FreeBSD CHINA
    FreeBSD官方网站
    中文FreeBSD用户组
    Database
    Mysql官方网站
    PostgreSQL官方网站
    Developer
    Apache官方网站
    C Programming.com
    C/C++ Reference
    Eclipse官方
    GCC Home Page
    Minimalist GNU for Windows
    PHP-MVC网站
    PHP官方网站
    Zend官方网站
    超越PHP
    常去的地方
    China UNIX
    CSDN论坛
    FreeBSD CHINA中文论坛
    IBM DeveloperWorks
    SourceForge.net
    ZDNet CHINA
    中国Linux公社
    西祠胡同
    朋友的blog
    drixe的专栏
    livid的blog
    黑暗天使的blog
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 PHP5调用COM生成Word文档的类收藏

    新一篇: 你的手机半夜为谁开? | 旧一篇: Apache 2.0.54 + mod_gzip 2.0.50 + mod_perl 2.0.1 for Win32配置

    开发环境:

    Windows Server 2003 Enterprise Edition,Microsoft Office 2003,PHP 5.0.4

    word.inc

    1. <?php
    2. # +-----------------------------------------------------------------------------------------+
    3. # | PHP Version 5                                                                                             |
    4. # +-----------------------------------------------------------------------------------------+
    5. # | Copyright (c) 1997-2005 The PHP Group                                              |
    6. # +-----------------------------------------------------------------------------------------+
    7. # | This source file is subject to version 2.02 of the PHP license,          |
    8. # | that is bundled with this package in the file LICENSE, and is           |
    9. # | available at through the world-wide-web at                                           |
    10. # | http://www.php.net/license/2_02.txt.                                                        |
    11. # | If you did not receive a copy of the PHP license and are unable to   |
    12. # | obtain it through the world-wide-web, please send a note to            |
    13. # | license@php.net so we can mail you a copy immediately.                |
    14. # +-----------------------------------------------------------------------------------------+
    15. # | Authors: bingbing <feng0629@gmail.com>                                         |
    16. # $Id: word.inc,v 0.1.0 dev  2005-7-8 23:10:19 Exp $
    17. #
    18. class Word {
    19.    /**
    20.     * document save path
    21.     * $savePath = "C:/worddocuments/";
    22.     * @param string $savePath
    23.     * @access public
    24.     */
    25.    public $savePath = "";
    26.    /**
    27.     * Class constructor.
    28.     *
    29.     * @param string $path
    30.     * @access public
    31.     */
    32.    public function __construct($path = "") {
    33.        $this->word              = new COM("word.application");
    34.        $this->word->WindowState = 0;    // 0: default; 1: maximize; 2: minimize;
    35.        $this->word->Visible     = true; // true: display the MSWord window
    36.                                                               // false: hide the MSWord window
    37.        if ("" == $this->savePath) {
    38.            if (!is_dir($path)) {
    39.                $this->error("<b>".$path."</b> is not a directory.");
    40.            }
    41.            $this->savePath = $path;
    42.        }
    43.    }
    44.    /**
    45.     * Get the MSWord version and language code.
    46.     *
    47.     * @access public
    48.     * @return void
    49.     */
    50.    public function getInfo() {
    51.        printf  ("<h1>MSWord information</h1>");
    52.        printf ("<p>MSWord version: %s </p>\n",$this->word->Version());
    53.        printf ("<p>MSWord language code: %s </p>\n",$this->word->System->Country());
    54.    }
    55.    /**
    56.     * Create a new document.
    57.     *
    58.     * @access public
    59.     * @return boolean
    60.     */
    61.    public function newDoc() {
    62.        $this->doc = $this->word->Documents->Add();
    63.        return true;
    64.    }
    65.    /**
    66.     * document page setup
    67.     *
    68.     * @param float $topMargin             in points
    69.     * @param float $rightMargin           in points
    70.     * @param float $bottomMargin          in points
    71.     * @param float $leftMargin            in points
    72.     * @access public
    73.     * @return boolean
    74.     */
    75.    public function pageSetup($topMargin,$rightMargin,$bottomMargin,$leftMargin) {
    76.        $this->doc->PageSetup->TopMargin    = $topMargin;
    77.        $this->doc->PageSetup->RightMargin  = $rightMargin;
    78.        $this->doc->PageSetup->BottomMargin = $bottomMargin;
    79.        $this->doc->PageSetup->LeftMargin   = $leftMargin;
    80.        return true;
    81.    }
    82.    /**
    83.     * Open a document
    84.     *  
    85.     * @param string $fileName     document name
    86.     *  $fileName="C:\worddocuments\test.doc"
    87.     * @access public
    88.     * @return boolean
    89.     */
    90.    public function openDoc($fileName) {
    91.        try {
    92.            $this->word->Documents->Open($fileName);
    93.        } catch (Exception $e) {
    94.            echo "<pre>".$e->getMessage()."</pre>";
    95.        }
    96.        return true;
    97.    }
    98.    /**
    99.     * To Write a new MSWord document and to change the font
    100.     *
    101.     * @param string  $fontName
    102.     * @param int     $fontSize
    103.     * @param boolean $fontBold
    104.     * @param boolean $fontItalic
    105.     * @param boolean $fontUnderline
    106.     * @param int     $alignment
    107.     * @param string  $content
    108.     * @access public
    109.     * @return void
    110.     */
    111.    public function writeText($fontName,$fontSize,$fontBold,$fontItalic,
    112.                             $fontUnderline,$alignment,$content) {
    113.        $this->word->Selection->Font->Name = $fontName;
    114.        $this->word->Selection->Font->Size = $fontSize;
    115.        $this->word->Selection->Font->Bold   = $fontBold;
    116.        $this->word->Selection->Font->Italic = $fontItalic;
    117.        $this->word->Selection->Font->Underline = $fontUnderline;
    118.        $this->word->Selection->ParagraphFormat->Alignment = $alignment;
    119.        $this->word->Selection->TypeText($content."\n");
    120.    }
    121.    /**
    122.     * Replace words in a MSWrod document
    123.     *
    124.     * @param string $str
    125.     * @param string $replace_str
    126.     * @access public
    127.     * @return void
    128.     */
    129.    public function replaceText($str,$replace_str) {
    130.        $this->word->Selection->Find->Execute($str,false,true,false,false,false,
    131.                                             true,1,false,$replace_str,2);
    132.    }
    133.    /**
    134.     * Create a table in MSWord
    135.     *
    136.     * @param int $cols
    137.     * @param int $rows
    138.     * @access public
    139.     * @return void
    140.     */
    141.    public function createTable($cols,$rows) {
    142.        $this->table = $this->doc->Tables->Add($this->word->Selection->Range,$cols,$rows);
    143.    }
    144.    /**
    145.     * write in cells
    146.     *
    147.     * @param int     $col
    148.     * @param int     $row
    149.     * @param string  $fontName
    150.     * @param int     $fontSize
    151.     * @param boolean $fontBold
    152.     * @param boolean $fontItalic
    153.     * @param boolean $fontUnderline
    154.     * @param int     $alignment
    155.     * @param string  $content
    156.     * @access public
    157.     * @return void
    158.     */
    159.    public function writeCell($col,$row,$fontName,$fontSize,$fontBold,
    160.                             $fontItalic,$fontUnderline,$alignment,$content) {
    161.        $this->table->Cell($col,$row)->Range->Font->Name = $fontName;
    162.        $this->table->Cell($col,$row)->Range->Font->Size = $fontSize;
    163.        $this->table->Cell($col,$row)->Range->Bold = $fontBold;
    164.        $this->table->Cell($col,$row)->Range->Font->Italic = $fontItalic;
    165.        $this->word->Selection->Font->Underline = $fontUnderline;
    166.        $this->table->Cell($col,$row)->Range->ParagraphFormat->Alignment = $alignment;
    167.        $this->table->Cell($col,$row)->Range->Text = $content;
    168.    }
    169.    /**
    170.     * Change MSWord document properties
    171.     *
    172.     * @param array $properties[]
    173.     * [0]      word_Title:             0x00000001
    174.     * [1]      word_Subject:           0x00000002
    175.     * [2]      word_Author:            0x00000003
    176.     * [3]      word_Keywords:          0x00000004
    177.     * [4]      word_Comments:          0x00000005
    178.     * [5]      word_Template:          0x00000006
    179.     * [6]      word_LastAuthor:        0x00000007
    180.     * [7]      word_Revision:          0x00000008
    181.     * [8]      word_AppName:           0x00000009
    182.     * [9]      word_TimeLastPrinted:   0x0000000A
    183.     * [10]     word_TimeCreated:       0x0000000B
    184.     * [11]     word_TimeLastSaved:     0x0000000C
    185.     * [12]     word_VBATotalEdit:      0x0000000D
    186.     * [13]     word_Pages:             0x0000000E
    187.     * [14]     word_Words:             0x0000000F
    188.     * [15]     word_Characters:        0x00000010
    189.     * [16]     word_Security:          0x00000011
    190.     * [17]     word_Category:          0x00000012
    191.     * [18]     word_Format:            0x00000013
    192.     * [19]     word_Manager:           0x00000014
    193.     * [20]     word_Company:           0x00000015
    194.     * [21]     word_Bytes:             0x00000016
    195.     * [22]     word_Lines:             0x00000017
    196.     * [23]     word_Paras:             0x00000018
    197.     * [24]     word_Slides:            0x00000019
    198.     * [25]     word_Notes:             0x0000001A
    199.     * [26]     word_HiddenSlides:      0x0000001B
    200.     * [27]     word_MMClips:           0x0000001C
    201.     * [28]     word_HyperlinkBase:     0x0000001D
    202.     * [29]     word_CharsWSpaces:      0x0000001E
    203.     * @access public
    204.     * @return void
    205.     */
    206.    public function properties($properties = array()) {
    207.        if (!is_array($properties)) {
    208.            $this->error("\$properties is not a array.");
    209.        }
    210.        
    211.        for ($i=1; $i<31; $i++) {
    212.            $j = strtoupper(dechex($i));
    213.            if (strlen($j) == 1) {
    214.                $j = "0x0000000".$j;
    215.            } elseif (strlen($j) == 2) {
    216.                $j = "0x000000".$j;
    217.            }
    218.            $this->word->ActiveDocument->BuiltInDocumentProperties($j)->Value = $properties[($i-1)];
    219.        }
    220.    }
    221.    /**
    222.     * Save the document
    223.     *
    224.     * @param $fileName
    225.     * @access public
    226.     * @return void
    227.     */
    228.    public function saveAs($fileName) {
    229.        if ("" == $fileName) {
    230.            $fileName = date("YmdHis");
    231.        }
    232.        printf ("<pre>File save as directory: %s</pre>",$this->savePath);
    233.        $this->word->Documents[1]->SaveAs($this->savePath.$fileName);
    234.    }
    235.    /**
    236.     * @param $msg              error message
    237.     * @access private
    238.     * @return void
    239.     */
    240.    private function error($msg) {
    241.        printf("<h1>Error:</h1>\n<hr size=\"1\" color=\"#CCCCCC\" />\n<pre>%s</pre>",$msg);
    242.        exit();
    243.    }
    244.    /**
    245.     * Class destructor.
    246.     *
    247.     * @access public
    248.     */
    249.    public function __destruct() {
    250.        //Close Word
    251.        $this->word->Quit();
    252.    }
    253. }
    254. ?>

    test.php

    1. <?php
    2. require_once "word.inc";
    3. $path = "C:/worddocuments/";
    4. $word = new Word($path);
    5. // Create new document
    6. // Create new document
    7. $word->newDoc();
    8. //setup page margin
    9. //setup page margin
    10. $word->pageSetup(90,60,80,80);
    11. $title = "Title";
    12. $content = "This is a test.
    13. ......";
    14. //write content.
    15. //write content.
    16. $word->writeText("Arial",20,true,false,false,1,$title);
    17. $word->writeText("Times New Roman",10,false,false,false,0,$content);
    18. $word->writeText("Times New Roman",10,false,false,false,2,date("Y-m-d"));
    19. $word->writeText("Times New Roman",10,false,false,false,0,"");
    20. $word->writeText("Times New Roman",10,true,false,false,1,"Table Title");
    21. //Create tables
    22. $word->createTable(4,5);
    23. //write in cells
    24. $word->writeCell(1,1,"Times New Roman",10,true,false,false,1,"ID.");
    25. $word->writeCell(1,2,"Times New Roman",10,true,false,false,1,"NAME");
    26. $word->writeCell(1,3,"Times New Roman",10,true,false,false,1,"UNIT");
    27. for ($i=2; $i<=4; $i++) {
    28.    $word->writeCell($i,1,"Times New Roman",10,false,false,false,1,($i-1));
    29. }
    30. $properties = array (
    31.    0  => "Title",
    32.    1  => "Subject",
    33.    2  => "fengzhiju",
    34.    3  => "test",
    35.    4  => "Comments",
    36.    5  => "Template",
    37.    6  => "LastAuthor",
    38.    7  => "Revision",
    39.    8  => "AppName",
    40.    9  => "TimeLastPrinted",
    41.    10 => "TimeCreated",
    42.    11 => "TimeLastSaved",
    43.    12 => "VBATotalEdit",
    44.    13 => "Pages",
    45.    14 => "Words",
    46.    15 => "Characters",
    47.    16 => "Security",
    48.    17 => "Category",
    49.    18 => "Format",
    50.    19 => "Manager",
    51.    20 => "Company",
    52.    21 => "Bytes",
    53.    22 => "Lines",
    54.    23 => "Paras",
    55.    24 => "Slides",
    56.    25 => "Notes",
    57.    26 => "HiddenSlides",
    58.    27 => "MMClips",
    59.    28 => "HyperlinkBase",
    60.    29 => "CharsWSpaces"
    61. );
    62. $word->properties($properties);
    63. //save document.
    64. //save document.
    65. $word->saveAs("test");
    66. ?>

    replace_test.php

    <?php
    require_once "word.inc";

    $path = "C:/worddocuments/";

    $word = new Word($path);

    $word->openDoc("C:/worddocuments/test.doc");

    $str = "Title";
    $replace_str = "New Title";

    $word->replaceText($str,$replace_str);
    $word->saveAs("test");
    ?>

    由于M$的东东大多不开放,无法获取其他资料,目前只能完成这些。等找到新的资料,我会继续加入新的方法

    发表于 @ 2005年07月10日 15:33:00|评论(loading...)|编辑

    新一篇: 你的手机半夜为谁开? | 旧一篇: Apache 2.0.54 + mod_gzip 2.0.50 + mod_perl 2.0.1 for Win32配置

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 冰凌尘埃