Home php教程 php手册 php 生成excel文件

php 生成excel文件

Jun 13, 2016 am 10:08 AM
excel php for code collect document article payment Compare generate program

php 生成excel文件 本文章收藏了三款php 生成excel文件代码程序,第一款为比较全面的生成函数,后面二款很简单,但是不如第一款好,好了现在来看看生成excel的原理吧。

php教程 生成excel文件
本文章收藏了三款php 生成excel文件代码程序,第一款为比较全面的生成函数,后面二款很简单,但是不如第一款好,好了现在来看看生成excel的原理吧。
*/

class excel{   
  
 
    var $header = "
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 = "";
    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 .= "" . $v . "n";   
                 } else {   
                    $cells .= "" . $v . "n";   
                 }   
             } else {   
                $cells .= "" . $v . "n";   
             }   
  
        endforeach;   
  
        // transform $cells content into one row   
        $this->lines[] = "n" . $cells . "n";   
  
     }   
     
    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 "nworksheet_title . "">n

n";   
        echo "n";   
        echo implode ("n", $this->lines);   
        echo "
nn";   
        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."nn";
  echo   "编号"."t"; 
  echo   "姓名"."t"; 
  echo   "n";

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles