zoukankan      html  css  js  c++  java
  • php 导出excel

    <?php
    class Excel {
    var $inEncode;
    var $outEncode;
    public function _construct() {
    }
    public function setEncode($incode, $outcode) {
    $this->inEncode = $incode;
    $this->outEncode = $outcode;
    }
    public function setTitle($titlearr) {
    $title = "";
    foreach ( $titlearr as $v ) {
    if ($this->inEncode == $this->outEncode) {
    $title .= iconv ( $this->inEncode, $this->outEncode, $v ) . " ";
    } else {
    $title .= $v . " ";
    }
    }
    $title .= " ";
    return $title;
    }
    public function setRow($array) {
    $content = "";
    foreach ( $array as $k => $v ) {
    foreach ( $v as $vs ) {
    if ($this->inEncode != $this->outEncode) {
    $content .= iconv ( $this->inEncode, $this->outEncode, $vs ) . " ";
    } else {
    $content .= $vs . ". ";
    }
    }
    $content .= " ";
    }
    return $content;
    }
    public function getExcel($titlearr, $array, $filename = '') {
    if ($filename == '') {
    $filename = date ( 'Y-m-d' );
    }
    $title = $this->setTitle ( $titlearr );
    $content = $this->setRow ( $array );
    $filename=iconv("UTF-8","GB2312",$filename);
    header ( "Content-type:application/vnd.ms-exce8 charset=UTF-8" );
    header ( "Content-Disposition:attachment; filename=" . $filename . ".xls" );
    echo $title;
    echo $content;
    }
    }

    $excel = new Excel ();
    // 设置编码:
    $excel->setEncode ( "utf-8", "gb2312" ); // 如果不转码,参数写一样即可,例如$excel->setEncode("utf-8","utf-8");
    // 设置标题栏
    $titlearr = array (
    "a",
    "b",
    "c",
    "d"
    );
    // 设置内容栏
    $contentarr = array (
    1 => array (
    "中国",
    "ac",
    "ad",
    "ae"
    ),
    2 => array (
    "abc",
    "acc",
    "adc",
    "aec"
    ),
    3 => array (
    "abd",
    "acd",
    "add",
    "aed"
    ),
    4 => array (
    "abe",
    "ace",
    "ade",
    "aee"
    )
    );
    $excel->getExcel ( $titlearr, $contentarr, "导出excel" );

    ?>

  • 相关阅读:
    hadoop 主机名 无法访问问题解决汇总
    Linux 集群时间同步(Ubuntu)
    odoo里面的一些ORM操作
    odoo12动作里添加向导
    odoo看板笔记
    odoo中接口开发
    odoo视图 “动作” 里添加菜单按钮:案例
    odoo源码学习之任务中的阶段字段stage_id
    python中的abstractmethod
    U盘启动盘安装win10出现cdboot:couldn't find ntldr
  • 原文地址:https://www.cnblogs.com/cxlings/p/3979044.html
Copyright © 2011-2022 走看看