php操作excel⽂件的⽅法⼩结
复制代码代码如下:
<?
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
共享打印机拒绝访问echo "test1\t";
echo "test2\t\n";
echo "test1\t";
echo "test2\t\n";
>
在php环境运⾏上⾯的代码,⼤家就可以看到浏览器询问⽤户是否下载excel⽂档,点击保存,硬盘上就多了⼀个excel的⽂件,使⽤excel打开就会看到最终的结果,怎么样不错吧。
其实在做真正的应⽤的时候,⼤家可以将数据从数据库中取出,然后按照每⼀列数据结束后加\t,每⼀⾏数据结束后加\n的⽅法echo出来,在php的开头⽤header("Content-type:application/vnd.ms-excel");表⽰输出的是excel⽂件,⽤
header("Content-Disposition:filename=test.xls");表⽰输出的⽂件名为text.xls。这样就ok了。
我们更可以修改header让他输出更多格式的⽂件,这样php在处理各种类型⽂件⽅⾯就更加⽅便了.
复制代码代码如下:
<?php
$DB_Server = "localhost";
$DB_Username = "mydowns";
$DB_Password = "";
$DB_DBName = "mydowns";
$DB_TBLName = "user";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn@#t connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date(@#Y-m-d H:i@#);
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份⽇期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn@#t select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$title\n");
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
$i = 0;2015感动中国十大人物
while($row = mysql_fetch_row($result))
{
$schema_insert = "";总进球数算加时赛吗
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
$i++;
}
return (true);
>
这是对于那些只喜欢简单处理⼀下excel朋友来说的
复制代码代码如下:
<?php
//定义⼀个excel⽂件
$workbook = "C:/My Documents/test.xls";
$sheet = "Sheet1";
//⽣成⼀个com对象$ex
$ex = new COM("Excel.sheet") or Die ("连不上");
//打开⼀个excel⽂件
$book = $ex->application->Workbooks->Open($workbook) or Die ("打不开"); $sheets = $book->Worksheets($sheet);
$sheets->activate;
//获取⼀个单元格
$cell = $sheets->Cells(5,5);
$cell->activate;
//给该单元格赋值
$cell->value = 999;
高一物理知识点总结//保存为另⼀⽂件newtest.xls
$ex->Application->ActiveWorkbook->SaveAs("newtest.xls");
//关掉excel,如果想看效果,则注释掉下⾯两⾏,由⽤户⼿动关掉excel
$ex->Application->ActiveWorkbook->Close("False");
unset ($ex);
>
可以通过PHP来产⽣EXCEL档。
----------------------------
Excel Functions
----------------------------
将下⾯的代码存为excel.php ,然后在页⾯中包括进来
然后调⽤
1. Call xlsBOF()
2. 将⼀些内容写⼊到xlswritenunber() 或者 xlswritelabel()中.
3.然后调⽤ Call xlsEOF()
也可以⽤ fwrite 函数直接写到服务器上,⽽不是⽤echo 仅仅在浏览器上显⽰。
复制代码代码如下:
<?php
// ----- begin of function library -----
/
/ Excel begin of file header
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
// Excel end of file footer
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
// Function to write a Number (double) into Row, Col
高考成绩怎么查询
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
// Function to write a label (text) into Row, Col
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;学员自我鉴定
}
// ----- end of function library -----
>
//
// To display the contents directly in a MIME compatible browser
// add the following lines on TOP of your PHP file:
<?php
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header (@#Content-type: application/x-msexcel@#);
header ("Content-Disposition: attachment; filename=EmplList.xls" ); header ("Content-Description: PHP/INTERBASE Generated Data" );
//
// the next lines demonstrate the generation of the Excel stream
//
xlsBOF(); // begin Excel stream
xlsWriteLabel(0,0,"This is a label"); // write a label in A1, use for dates too xlsWriteNumber(0,1,9999); // write a number B1
xlsEOF(); // close the stream
>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论