php 生成excel文件

 代码如下 复制代码

class excel{   
  
 
    var $header = "<?xml version="1.0" encoding="utf-8"?>
<workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/tr/rec-html40">";   

    var $footer = "</workbook>";
    var $lines = array ();   
    var $worksheet_title = "table1";   
   
    function addrow ($array) {   
  
 
        $cells = "";   
           
 
        foreach ($array as $k => $v):   
               
            // 加个字符串与数字的判断 避免生成的 excel 出现数字以字符串存储的警告   
            if(is_numeric($v)) {   
                // 防止首字母为 0 时生成 excel 后 0 丢失   
                if(substr($v, 0, 1) == 0) {   
                    $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> ";   
                 } else {   
                    $cells .= "<cell><data ss:type="number">" . $v . "</data></cell> ";   
                 }   
             } else {   
                $cells .= "<cell><data ss:type="string">" . $v . "</data></cell> ";   
             }   
  
        endforeach;   
  
        // transform $cells content into one row   
        $this->lines[] = "<row> " . $cells . "</row> ";   
  
     }   
     
    function addarray ($array) {   
  
        // run through the array and add them into rows   
        foreach ($array as $k => $v):   
            $this->addrow ($v);   
        endforeach;   
  
     }   
   
    function setworksheettitle ($title) {   
  
        // strip out special chars first   
        $title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);   
  
        // now cut it to the allowed length   
        $title = substr ($title, 0, 31);   
  
        // set title   
        $this->worksheet_title = $title;   
  
     }   
 
    function generatexml ($filename) {   
  
 
         header("content-type: application/vnd.ms-excel; charset=utf-8");   
         header("content-disposition: inline; filename="" . $filename . ".xls"");   
 
        echo strips教程lashes ($this->header);   
        echo " <worksheet ss:name="" . $this->worksheet_title . ""> <table> ";   
        echo "<column ss:index="1" ss:autofitwidth="0" ss:width="110"/> ";   
        echo implode (" ", $this->lines);   
        echo "</table> </worksheet> ";   
        echo $this->footer;   
  
     }   
  
}   
 
  
/**
*   非框架使用方法
*
*   require_once('excel.php');
*   $doc = array (
*        0 => array ('中国', '中国人', '中国人民', '123456");
*   );
*   $xls = new excel;
*   $xls->addarray ( $doc );
*   $xls->generatexml ("mytest");
*/  

?>

方法二
其实在做真正的应用的时候,大家可以将数据从数据库教程中取出,然后按照每一列数据结束后加t,每一行数据结束后加n的方法echo出来,在php的开头用header("content-type:application/vnd.ms-excel");表示输出的是excel文件,用header("content-disposition:filename=test.xls");表示输出的文件名为text.xls。这样就ok了。

 代码如下 复制代码

<?

       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";

?>

方法三

<?
  header("content-type:   application/octet-stream"); 
  header("accept-ranges:   bytes"); 
  header("content-type:application/vnd.ms-excel");   
  header("content-disposition:attachment;filename=export_excel_gshjsl.xls");   
  
  $tx='表头'; 
  echo   $tx." ";
  echo   "编号"." "; 
  echo   "姓名"." "; 
  echo   " ";

 echo "="411481198507150666""." ";
 echo "="0123456""." ";
 echo " ";
 ?>

时间: 2024-09-20 14:30:01

php 生成excel文件的相关文章

php,不用COM,生成excel文件

excel|生成excel 用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";

使用.NET生成Excel文件

excel|生成excel   我在工作中遇到了一些生成Excel的问题.在网络上查找了很多方法,各有优劣.最后选择了使用OFFICE 的Excel 自动化服务来生成Excel文件的办法,也就是使用Excel提供的Com对象.具体的代码如下:public static void CreateExcel()  {   for(int i = 0 ; i<1 ; i++)   {    string staFile = "D:\\test\\x";    System.Reflect

用PHP生成excel文件到指定目录

  这篇文章主要介绍了用PHP生成excel文件到指定目录的相关资料,需要的朋友可以参考下 最近公司要生成报表,用PHP生成. header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=test_data.xls"); 我百度了下,貌似这个很快能够实现,但是这个文件却是生成在在浏览器下载的地方, 我想把生成的文件生成到指定的目录,这

python通过openpyxl生成Excel文件的方法

  本文实例讲述了python通过openpyxl生成Excel文件的方法.分享给大家供大家参考.具体如下: 使用前请先安装openpyxl: ? 1 easy_install openpyxl 通过这个模块可以很方便的导出数据到Excel ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 from op

php生成excel文件的简单方法

 生成excel文件,最简单的莫过于把数据库的数据导入到excel就行了,下面有个不错的示例,大家可以参考下 生成excel 当然使用的是 phpExcel这个类库了,可是它太麻烦了,对于只要简单生成来说有点不值得    什么叫简单,把数据库的数据导入到excel就行了, 这个就是简单了    下面看一段代码(代码来自网络)    注意要双引号的字符串   代码如下: <?php  header("Content-type:application/vnd.ms-excel"); 

java使用poi开源框架生成excel文件并对其进行加密登入用户名若为汉字则出现乱码

问题描述 java使用poi开源框架生成excel文件并对其进行加密登入用户名若为汉字则出现乱码 public static byte[] encryptExcel(byte[] bytes String password String userName) { byte[] enExcelBytes = null; try { // 创建一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

客户服务器没有EXCEL我的.exe怎样在服务器端生成EXCEL文件

问题描述 客户服务器没有EXCEL我的.exe怎样在服务器端生成EXCEL文件 解决方案 解决方案二:各位大侠帮帮忙啊,小弟先谢谢了

java sql server jsp-JSP java SQL servlet实现页面表格生成Excel文件

问题描述 JSP java SQL servlet实现页面表格生成Excel文件 使用JSP java SQL server做了个小系统,现在想实将现通过查询功能生成的表,通过页面上的按钮触发,生成Excel文件.求大神指导! 解决方案 http://271788203.iteye.com/blog/491052

谢谢帮忙-如何实现数据抓取并生成Excel文件

问题描述 如何实现数据抓取并生成Excel文件 如何实现 网络数据抓取,那个需求分析如何去设计啊,要这么样才能达到在抓取数据时并进行筛选,并保存到本地的数据库中去 解决方案 这个要看你使用的什么语言,不同的语言有不同的做法 而Excel本身有标准的xls文件和Excel可以加载的xml和csv文件等,生成方法也不一样,难易程度却相差很大 解决方案二: 这个要看你使用的什么语言,不同的语言有不同的做法 而Excel本身有标准的xls文件和Excel可以加载的xml和csv文件等,生成方法也不一样,

php,不用COM,生成excel文件_php基础

用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 "tes