首页 > 编程技术 > php

用php生成excel文件

发布时间:2016-11-25 16:33

nbsp;用php生成excel文件
哈哈,今天又学一招。php生成excel文档太简单了,估计大家都会用到,所以
共享出来。
大家来看代码:
<?
         header("Content-type:application/vnd.ms-excel");
         header("Content-Disposition:filename=test.xls");
         echo "test1 ";
         echo "test2 ";
         echo "test1 ";
         echo "test2 ";
         echo "test1 ";
         echo "test2 ";
         echo "test1 ";
         echo "test2 ";
         echo "test1 ";
         echo "test2 ";
         echo "test1 ";
         echo "test2 ";
?>
在php环境运行上面的代码,大家就可以看到浏览器询问用户是否下载excel
文档,点击保存,硬盘上就多了一个excel的文件,使用excel打开就会看到
最终的结果,怎么样不错吧。
其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每
一列数据结束后加 ,每一行数据结束后加 的方法echo出来,在php的开头
用header("Content-type:application/vnd.ms-excel");表示输出的是
excel文件,用header("Content-Disposition:filename=test.xls");表
示输出的文件名为text.xls。这样就ok了。
我们更可以修改header让他输出更多格式的文件,这样php在处理各种类型
文件方面就更加方便了。
 
Yorgo Sun (yorgo@163.net)
2000/11/14
欢迎转载,但请保证文档完整,通知作者。谢谢 :-)

Mark Nold
Joost Soeterbroek
 
The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples of using PHP and Java together.

Windows Installation


The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older versions of the JDK and the various MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.
Step 1: Install the JDK. This is fairly simple, as the JDK installs without many questions. It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.x.xin directory is in your path. This will make compiling your Java Classes easier. On Win9x add
PATH=%PATH%;C:jdk1.2.2in

to your autoexec.bat. On NT add
;C:jdk1.2.2in

to the end of the PATH environment variable. It is also important to note in your autoexec.bat file, the PHP Java extension ignores the JAVA_HOME and CLASSPATH set up in the environment. This is important because these items must be set correctly in your php.ini file for the Java extension to work.
Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file
 
[java]
extension=php_java.dll
java.library.path=c:webphp4extensionsjava.class.path="c:webphp4extensionsjdk1.2.2php_java.jar;c:myclasses"
P>         网上很的免费支持PHP的个人主页空间现在不少,这时可能会想到来个上传什么的,但毕竟属于免费的,当然会有很多的限制,不允许上传,这个很正常,有的连Mail()函数都不给用呢。遇到这种情况,也不是没有办法上传的哦,PHP支持强大的socket,当然少不了直接操作ftp了,看看函数表上就有了,通过ftp实现文件上传。不是很好,我们上传主页不是用ftp上传吗。
     实现代码如下:
//upload.php
<html>
<body>
<form enctype="multipart/form-data" action=upload.php method=post> 
上传文件:  <input type=file name=upload_file> 
<input type=submit name=action value=OK> 
</form> 
<?php 
$ftp_server="ftp服务器(最好用IP)"; 
$ftp_username="ftp用户名"; 
$ftp_password="登录密码"; 
$ftp_dir="目录(如设置为 /)";
if ($action == "OK") 

  $con = ftp_connect($ftp_server);  //连接ftp
  ftp_login($con, $ftp_username, $ftp_password); //用户登录
  $suss=$con;
  ftp_chdir($suss, $ftp_dir);  //cd到指定目录
  $ok_code = ftp_put($suss, $upload_file_name, $upload_file, FTP_BINARY); //put文件
 
  if ($ok_code == 1) 
  { 
      echo "文件上传成功! "; 
  } 
  else 
  { 
     echo "文件上传失败! "; 
  } 
      ftp_quit($suss);  // 关闭ftp连接

?> 
</body>
</html>

P>Java语言功能强大,因此在许多情况下在php中来调用Java的功能将十分有用。在php中调用Java语言有两种方法,一种是使用php中的Java扩展模块,另一种是使用minij2ee应用服务器提供的SJOP协议实现。下面我们来比较一下这两种方法各自的特点。
1.php的Java模块
php发布版中包含一个Java扩展模块,可以用来调用Java对象,例如:
<?php
$system=new Java("java.lang.System");
print "Java version=".$system->getProperty("java.version")." <br> ";
?>
使用这种方法的优点是比较方便,只要用new Java()来创建一个Java对象,就可以同php类一样来调用Java对象。但是这种方法也有以下明显的缺点:
1.由于php的Java模块根据php的数据类型选择最适合的Java方法,因此无法调用Java过载的函数。
2.php的Java模块将在当前Web Server的进程中载入JVM(Java虚拟机),因此系统开销极大,影响Web Server进程的执行效率。
3.在某些操作系统和Web Server环境中,php的Java模块将使Web Server进程僵死。见http://www.php.net/bugs.php?id=6122。
由于这些原因,php的Java模块一直无法应用到实际的的软件系统中。
2.minij2ee应用服务器SJOP协议实现
在介绍minij2ee应用服务器SJOP协议实现之前,先简单介绍一下minij2ee应用服务器。minij2ee应用服务器是第一款支持php的J2EE应用服务器产品,使php能够用于开发企业级应用系统。SJOP全称是Sample Java ORB Protocol(简单Java对象请求代理协议),是一种简单高效的对象请求代理协议。比如:
<?php
$conn=minij2ee_fetch_connection();
print "Java version=".minij2ee_callstatic_javaobj($conn,"java.lang.System","getProperty","java.lang.String","java.version")." <br> ";
?>
minij2ee应用服务器实现SJOP协议的主要目的是使php中能够访问EJB企业级组件,因此minij2ee提供了一个EJB-PHP编译器,可以把EJB组件编译成php的类,使php程序中能够方便的调用EJB组件,例如:
<?php
require("Cart.php"); file://Cart.php是编译Cart EJB后生成的Cart EJB的php类定义。
$home=new CartHome(); file://创建EJB的Home接口。
下面列出了当前在 PCRE 中可能使用的修正符。括号中是这些修正符的内部 PCRE 名。
 






标签:[!--infotagslink--]