转载请注明来源:https://www.cnblogs.com/hookjc/

Excel:

<?php
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=11.xls ");
header("Content-Transfer-Encoding: binary ");

$filename="<table>
   <tr>
    <th>left_1</th>
    <td>right_1</td>
   </tr>
   <tr>
    <th>left_2</th>
    <td>right_2</td>
   </tr>
  </table>";

//$filename=iconv("utf-8", "gb2312", $filename);

echo $filename;
?>

<?php
class Db {
    var $conn;

    /***************************************************************************
     * 连接数据库
     * return:MySQL 连接标识,失败返回FALSE
     **************************************************************************/
    function Db($host="localhost",$user="root",$pass="123456",$db="juren_gaokao") {
        if(!$this->conn=mysql_connect($host,$user,$pass))
            die("can't connect to mysql sever");
        mysql_select_db($db,$this->conn);
        mysql_query("SET NAMES 'UTF-8'");
    }

    /***************************************************************************
     * 执行SQL查询
     * return:查询结构集 resource
     **************************************************************************/
    function execute($sql) {
        return mysql_query($sql,$this->conn);
    }

    /***************************************************************************
     * 返回结构集中行数
     * return:number 数字
     **************************************************************************/
    function findCount($sql) {
        $result=$this->execute($sql);
        return mysql_num_rows($result);
    }

    /***************************************************************************
     * 执行SQL查询
     * return:array 数组
     **************************************************************************/
    function findBySql($sql) {
        $array=array();
        $result=mysql_query($sql);
        $i=0;
        while($row=mysql_fetch_assoc($result)) {
            $array[$i]=$row;
            $i++;
        }
        return $array;
    }

    /***************************************************************************
    *$con的几种情况
    *空:返回全部记录
    *array:eg. array('id'=>'1') 返回id=1的记录
    *string :eg. 'id=1' 返回id=1的记录
    * return:json 格式数据
    ***************************************************************************/
    function toExtJson($table,$start="0",$limit="10",$cons="") {
        $sql=$this->generateSql($table,$cons);
        $totalNum=$this->findCount($sql);
        $result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
        $resultNum = count($result);//当前结果数
        $str="";
        $str.= "{";
        $str.= "'totalCount':'?$totalNum',";
        $str.="'rows':";
        $str.="[";
        for($i=0;$i<$resultNum;$i++) {
            $str.="{";
            $count=count($result[$i]);
            $j=1;
            foreach($result[$i] as $key=>$val) {
                if($j<$count) {
                    $str.="'".$key."':'".$val."',";
                }
                elseif($j==$count) {
                    $str.="'".$key."':'".$val."'";
                }
                $j++;
            }

            $str.="}";
            if ($i != $resultNum-1) {
                $str.= ", ";
            }
        }
        $str.="]";
        $str.="}";
        return  $str;
    }

    /***************************************************************************
     * $table:表名
     * $cons:sql条件
     * return:SQL语句
     **************************************************************************/
    function generateSql($table,$cons) {
        $sql="";//sql条件
        $sql="select * from ".$table;
        if($cons!="") {
            if(is_array($cons)) {
                $k=0;
                foreach($cons as $key=>$val) {
                    if($k==0) {
                        $sql.="where '";
                        $sql.=$key;
                        $sql.="'='";
                        $sql.=$val."'";
                    }else {
                        $sql.="and '";
                        $sql.=$key;
                        $sql.="'='";
                        $sql.=$val."'";
                    }
                    $k++;
                }
            }else {
                $sql.=" where ".$cons;
            }
        }
        return $sql;
    }

   
    /***************************************************************************
     * $table:表名
     * $cons:条件
     * return:XML格式文件
     **************************************************************************/
    function toExtXml($table,$start="0",$limit="10",$cons="") {
        $sql=$this->generateSql($table,$cons);
        $totalNum=$this->findCount($sql);
        $result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
        $resultNum = count($result);//当前结果数
        header("Content-Type: text/xml");
        $xml='<?xml version="1.0"  encoding="utf-8" ?>';
        $xml.="<xml>";
        $xml.="<totalCount>".$totalNum."</totalCount>";
        $xml.="<items>";
        for($i=0;$i<$resultNum;$i++) {
            $xml.="<item>";
            foreach($result[$i] as $key=>$val)
                $xml.="<".$key.">".$val."</".$key.">";
            $xml.="</item>";
        }
       
        $xml.="</items>";
        $xml.="</xml>";
        return $xml;
    }

    /***************************************************************************
     * $table:表名
     * $mapping:数组格式头信息$map=array('No','Name','Email','Age');
     * $fileName:WORD文件名称
     * return:WORD格式文件
     **************************************************************************/
    function toWord($table,$mapping,$fileName) {
        header('Content-type: application/doc');
        header('Content-Disposition: attachment; filename="'.$fileName.'.doc"');
        echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
       xmlns:w="urn:schemas-microsoft-com:office:word"
       xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <title>'.$fileName.'</title>
                </head>
                <body>';
        echo'<table border=1><tr>';
        if(is_array($mapping)) {
            foreach($mapping as $key=>$val)
                echo'<td>'.$val.'</td>';
        }
        echo'</tr>';
        $results=$this->findBySql('select * from '.$table);
        foreach($results as $result) {
            echo'<tr>';
            foreach($result as $key=>$val)
                echo'<td>'.$val.'</td>';
            echo'</tr>';
        }
        echo'</table>';
        echo'</body>';
        echo'</html>';
    }

    /***************************************************************************
     * $table:表名
     * $mapping:数组格式头信息$map=array('No','Name','Email','Age');
     * $fileName:Excel文件名称
     * return:Excel格式文件
     **************************************************************************/
    function toExcel($table,$mapping,$fileName) {
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:filename=".$fileName.".xls");
        echo'<html xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
        <head>
        <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
        <meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
        <!--[if gte mso 9]><xml>
        <x:ExcelWorkbook>
        <x:ExcelWorksheets>
        <x:ExcelWorksheet>
        <x:Name></x:Name>
        <x:WorksheetOptions>
        <x:DisplayGridlines/>
        </x:WorksheetOptions>
        </x:ExcelWorksheet>
        </x:ExcelWorksheets>
        </x:ExcelWorkbook>
        </xml><![endif]-->
        </head>
        <body link=blue vlink=purple leftmargin=0 topmargin=0>';
        echo'<table width="100%" border="0" cellspacing="0" cellpadding="0">';
        echo'<tr>';
        if(is_array($mapping)) {
            foreach($mapping as $key=>$val)
                echo'<td>'.$val.'</td>';
        }
        echo'</tr>';
        $results=$this->findBySql('select * from '.$table);
        foreach($results as $result) {
            echo'<tr>';
            foreach($result as $key=>$val)
                echo'<td>'.$val.'</td>';
            echo'</tr>';
        }
        echo'</table>';
        echo'</body>';
        echo'</html>';
    }

   
    function Backup($table) {
        if(is_array ($table)) {
            $str="";
            foreach($table as $tab)
                $str.=$this->get_table_content($tab);
            return $str;
        }else {
            return $this->get_table_content($table);
        }
    }

   
    /***************************************************************************
     * 备份数据库数据到文件
     * $table:表名
     * $file:文件名
     **************************************************************************/
    function Backuptofile($table,$file) {
        header("Content-disposition: filename=?$file.sql");//所保存的文件名
        header("Content-type: application/octetstream");
        header("Pragma: no-cache");
        header("Expires: 0");
        if(is_array ($table)) {
            $str="";
            foreach($table as $tab)
                $str.=$this->get_table_content($tab);
            echo $str;
        }else {
            echo $this->get_table_content($table);
        }
    }
   
    function Restore($table,$file="",$content="") {
        //排除file,content都为空或者都不为空的情况
        if(($file==""&&$content=="")||($file!=""&&$content!=""))
            echo"参数错误";
        $this->truncate($table);
        if($file!="") {
            if($this->RestoreFromFile($file))
                return true;
            else
                return false;
        }
        if($content!="") {
            if($this->RestoreFromContent($content))
                return true;
            else
                return false;
        }
    }
   
    //清空表,以便恢复数据
    function truncate($table) {
        if(is_array ($table)) {
            $str="";
            foreach($table as $tab)
                $this->execute("TRUNCATE TABLE ?$tab");
        }else {
            $this->execute("TRUNCATE TABLE ?$table");
        }
    }
   
    function get_table_content($table) {
        $results=$this->findBySql("select * from $table");
        $temp = "";
        $crlf="rn";
        foreach($results as $result) {
            /*(";
          foreach(?$result as ?$key=>?$val)
          {
            $schema_insert .= " `".?$key."`,";
          }
           $schema_insert = ereg_replace(",?$", "", ?$schema_insert);
           $schema_insert .= ")
            */
            $schema_insert = "INSERT INTO  ?$table VALUES (";
            foreach($result as $key=>$val) {
                if($val != "")
                    $schema_insert .= " '".addslashes($val)."',";
                else
                    $schema_insert .= "NULL,";
            }
            $schema_insert = ereg_replace(",?$", "", $schema_insert);
            $schema_insert .= ");?$crlf";
            $temp = $temp.$schema_insert ;
        }
        return $temp;
    }

   
    function RestoreFromFile($file) {
        if (false !== ($fp = fopen($file, 'r'))) {
            $sql_queries = trim(fread($fp, filesize($file)));
            $this->splitMySqlFile($pieces, $sql_queries);
            foreach ($pieces as $query) {
                if(!$this->execute(trim($query)))
                    return false;
            }
            return true;
        }
        return false;
    }
   
    function RestoreFromContent($content) {
        $content = trim($content);
        $this->splitMySqlFile($pieces, $content);
        foreach ($pieces as $query) {
            if(!$this->execute(trim($query)))
                return false;
        }
        return true;
    }
   
    function splitMySqlFile(&$ret, $sql) {
        $sql= trim($sql);
        $sql=split('',$sql);
        $arr=array();
        foreach($sql as $sq) {
            if($sq!="");
            $arr[]=$sq;
        }
        $ret=$arr;
        return true;
    }
}

来源:python脚本自动迁移

php导出excel xml word的更多相关文章

  1. RDLC - 后台代码直接导出Excel/PDF/Word格式

    最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...

  2. ASP.NET-GridView之导出excel或word

    在CS阶段我们涉及到表格的导出,再Web开发同样可以实现,而且实现形式多种多样.以下面的例子说明表格导出到excel和word 这里用到了一个后台方法输出流形成***文件的的公共方法 DEMO < ...

  3. EasyOffice-.NetCore一行代码导入导出Excel,生成Word

    简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...

  4. c#通过datatable导出excel和word

    /// <summary> /// 导出datatable到word /// </summary> /// <param name="dg">需 ...

  5. 文件导出Excel、Word、Pdf

    如果要将查询结果导出Excel,只要将页面的Context-Type修改下: header( "Content-Type: application/vnd.ms-excel"> ...

  6. 导出excel、word、csv文件方法汇总

    http://www.woaic.com/2012/06/64 excel文件主要是输出html代码.以xls的文本格式保存文件. 生成excel格式的代码: /// <summary> ...

  7. mysql数据库导出excel xml等格式文件

    http://jingyan.baidu.com/article/ac6a9a5e43a62e2b653eac83.html

  8. Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布

    Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布 摘要: jeasypoi 功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导 ...

  9. Spring Boot 系列教程12-EasyPoi导出Excel下载

    Java操作excel框架 Java Excel俗称jxl,可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件,现在基本没有更新了 http://jxl.sourcef ...

随机推荐

  1. [炼丹术]yolact训练模型学习总结

    yolact训练模型学习总结 一.YOLACT介绍(You Only Look At CoefficienTs) 1.1 简要介绍 yolact是一种用于实时实例分割的简单.全卷积模型. (A sim ...

  2. CS229 机器学习课程复习材料-线性代数

    本文是斯坦福大学CS 229机器学习课程的基础材料,原始文件下载 原文作者:Zico Kolter,修改:Chuong Do, Tengyu Ma 翻译:黄海广 备注:请关注github的更新,线性代 ...

  3. 教你10分钟对接人大金仓EF Core 6.x

    前言 目前.NET Core中据我了解到除了官方的EF Core外,还用的比较多的ORM框架(恕我孤陋寡闻哈,可能还有别的)有FreeSql.SqlSugar(排名不分先后).FreeSql和SqlS ...

  4. 查收新年礼物丨DevEco Studio 3.0 Beta2发布,20个新变化详解

    HUAWEI DevEco Studio是开发HarmonyOS应用和原子化服务的一站式集成开发环境(IDE),为开发者提供工程模板创建.开发.编译.调试.发布等功能. 2021年12月31日,新版本 ...

  5. docker构建.net core运行的镜像

    在docker很火的今天,越来越多的应用现在都在往docker上迁移,.net core怎么能落后? 项目要运行在docker上,我们需要先制作镜像,可以基于centos来制作,当然也可以基于Ubun ...

  6. mysql语句2-单表查询

    mysql 查询以及多表查询 以下所有表格样例都采用下边这个表格 mysql> select * from benet; +------+------+----------+ | id   | ...

  7. 在使用jjwt时在配置文件中设置过期时间,取到的结果为0的原因

    在设置了过期时间后感觉没有起作用,打印日志查看了下为0,因为生成token的文件在一个公共模块中,而过期时间设置在服务模块 中的配置文件中. 原因是:没有为设置getter和setter方法 来自为知 ...

  8. 您应该知道的35个绝对重要的Linux命令

    https://mp.weixin.qq.com/s?__biz=MzU3NTgyODQ1Nw==&mid=2247499293&idx=2&sn=1353b78d6ad01d ...

  9. 创客系列教程——认识LED灯

    认识LED灯 一.初识LED灯   LED灯是一种能够将电能转化为可见光的固态的半导体器件,它可以直接把电转化为光.LED灯逐步融入到生活中的方方面面:室内外的照明.电子指示牌.酷炫的舞台灯光.车辆的 ...

  10. js数组清空的两种方式

    编辑器加载中...方式1,length赋值为0 这种方式很有意思, 其它语言如Java,其数组的length是只读的,不能被赋值.如 int[] ary = {1,2,3,4}; ary.length ...