文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用PHP生成和处理电子表格文件(PEAR)

用PHP生成和处理电子表格文件(PEAR)

时间:2005-11-25  来源:manbuzhe0301

Spreadsheet_Excel_Writer 一个不需要COM组件来创建.xls文件的方式,详情参考http://pear.php.net,其中有所需的相关类。
The most common use for Spreadsheet_Excel_Writer
    will be spitting out large (or not so large) amounts of information
    in the form of a spreadsheet, which is easy to manipulate with a fairly
    ubiquitous spreadsheet program such as Excel (or OpenOffice).
   
    So let's cut to the chase and see how this is done:
   
Typical usage
[color="#000000"]
[color="#7f007f"]require_once [color="#007f00"]'Spreadsheet/Excel/Writer.php'[color="#7f007f"];
[color="#666666"]// Creating a workbook
[color="#0000bb"]$workbook [color="#7f007f"]= new [color="#0000bb"]Spreadsheet_Excel_Writer[color="#7f007f"]();
[color="#666666"]// sending HTTP headers
[color="#0000bb"]$workbook[color="#7f007f"]->[color="#0000bb"]send[color="#7f007f"]([color="#007f00"]'test.xls'[color="#7f007f"]);
[color="#666666"]// Creating a worksheet
[color="#0000bb"]$worksheet [color="#7f007f"]=& [color="#0000bb"]$workbook[color="#7f007f"]->[color="#0000bb"]addWorksheet[color="#7f007f"]([color="#007f00"]'My first worksheet'[color="#7f007f"]);
[color="#666666"]// The actual data
[color="#0000bb"]
$worksheet->write(0, 0, 'Name');
$worksheet->write(0, 1, 'Address');
$worksheet->write(0, 2, 'Age');
for ($i=1; $iwrite($i, 0, "name$i");
        for ($j=1; $jwrite($i, 1, "address$j");
        }
        for ($k=1; $kwrite($i, 2, "age$j");
        }
}
[color="#7f007f"]
[color="#666666"]// Let's send the file
[color="#0000bb"]$workbook[color="#7f007f"]->[color="#0000bb"]close[color="#7f007f"]();
[color="#0000bb"]?>
   The first thing you should notice, is that we created a workbook before any
   worksheets. All worksheets are contained within a workbook, and a workbook
   may contain several worksheets.
   
   Another important thing, which you should have in mind when programming
   with Spreadsheet_Excel_Writer, is that ampersand
   sign (&) that appears when we created our worksheet. That ampersand means
   we are referencing a Worksheet object instead of copying it. If you don't
   know what that means, don't worry, all you have to remember is to always
   use ampersands when calling
   addWorksheet() for creating a worksheet, or
   
addFormat()
for creating a format.
   
Saving to a regular file
    You may have noticed also the following line:
   
// sending HTTP headers
$workbook->send('test.xls');
    What that means is that we are sending our spreadsheet to a browser.
    But what if we just want to save the spreadsheet in our machine? Well, you
    just have to omit that line and give a valid file path to the workbook
    constructor.
   
    For example, if we wanted to save the same spreadsheet we created in our
    first example to a file named 'test.xls', we would do it like so:
   
Example 37-2. Saving to a regular file
[color="#000000"]
[color="#7f007f"]require_once [color="#007f00"]'Spreadsheet/Excel/Writer.php'[color="#7f007f"];
[color="#666666"]// We give the path to our file here
[color="#0000bb"]$workbook [color="#7f007f"]= new [color="#0000bb"]Spreadsheet_Excel_Writer[color="#7f007f"]([color="#007f00"]'test.xls'[color="#7f007f"]);
[color="#0000bb"]$worksheet [color="#7f007f"]=& [color="#0000bb"]$workbook[color="#7f007f"]->[color="#0000bb"]addWorksheet[color="#7f007f"]([color="#007f00"]'My first worksheet'[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]0[color="#7f007f"], [color="#0000bb"]0[color="#7f007f"], [color="#007f00"]'Name'[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]0[color="#7f007f"], [color="#0000bb"]1[color="#7f007f"], [color="#007f00"]'Age'[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]1[color="#7f007f"], [color="#0000bb"]0[color="#7f007f"], [color="#007f00"]'John Smith'[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]1[color="#7f007f"], [color="#0000bb"]1[color="#7f007f"], [color="#0000bb"]30[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]2[color="#7f007f"], [color="#0000bb"]0[color="#7f007f"], [color="#007f00"]'Johann Schmidt'[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]2[color="#7f007f"], [color="#0000bb"]1[color="#7f007f"], [color="#0000bb"]31[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]3[color="#7f007f"], [color="#0000bb"]0[color="#7f007f"], [color="#007f00"]'Juan Herrera'[color="#7f007f"]);
[color="#0000bb"]$worksheet[color="#7f007f"]->[color="#0000bb"]write[color="#7f007f"]([color="#0000bb"]3[color="#7f007f"], [color="#0000bb"]1[color="#7f007f"], [color="#0000bb"]32[color="#7f007f"]);
[color="#666666"]// We still need to explicitly close the workbook
[color="#0000bb"]$workbook[color="#7f007f"]->[color="#0000bb"]close[color="#7f007f"]();
[color="#0000bb"]?>


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载