首页 > 编程技术 > php

php生成 execel表格

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


<?php
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 key -> write value into 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) {
        // 返回数组并保存到单元格中去
        foreach ($array as $k => $v):
            $this->addRow ($v);
        endforeach;
    }
 
    function setWorksheetTitle ($title) {
      
        $title = preg_replace ("/[|:|/|?|*|[|]]/", "", $title);
        // 取得标题长度
        $title = substr ($title, 0, 31);
        // 赋值
        $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 stripslashes ($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;
    }
}
/**
 *  CakePHP中使用方法
 *  注意 ** cakePHP 配置文件 define('DEBUG', 0);
 *
 *  vendor ('Excel');
 *  $doc = array (
 *       0 => array ('中国', '中国人', '中国人民', '123456');
 *  );
 *  $xls = new Excel;
 *  $xls->addArray ( $doc );
 *  $xls->generateXML ("mytest");
 */
/**
 *  非框架使用方法
 *
 *  require_once('excel.php');
 *  $doc = array (
 *       0 => array ('中国', '中国人', '中国人民', '123456');
 *  );
 *  $xls = new Excel;
 *  $xls->addArray ( $doc );
 *  $xls->generateXML ("mytest");
 */
?>
<?php
class Calendar{
   /**
    * @desc       :简单的日历类,供大家学习
    * @author     :Allen Wu
    * @Email      :wukewei00o@126.com
    * @Date       :2008-09-12
    * @version    :v1.0
    */
    /*定义变量年、月、日*/
    private $year,$month,$day;
    /*定义数组星期并初始化*/
    private $week    = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    /*定义数组月份并初始化*/
    private $monthes = array("01"=>"一月",
                             "02"=>"二月",
                             "03"=>"三月",
                             "04"=>"四月",
                             "05"=>"五月",
                             "06"=>"六月",
                             "07"=>"七月",
                             "08"=>"八月",
                             "09"=>"九月",
                             "10"=>"十月",
                             "11"=>"十一月",
                             "12"=>"十二月"
                             );

    function __construct(){
        $year  = isset($_POST['year']) ? $_POST['year'] : date('Y');
        $month = isset($_POST['month']) ? $_POST['month'] : date('m');
        $day   = isset($_POST['day']) ? $_POST['day'] : date('d');
        $this->set($year, $month, $day);
    }
    /**
     * @desc   设置年、月、日的值
     * @params String $year
     * @params String $month
     * @params String $day
     * @return
     */
    private function set($year, $month, $day){
        $this->year  = $year;
        $this->month = $month;
        $this->day   = $day;
    }

    /**
     * @desc   获取年、月、日的值并以数组形式返回
     * @params Array $info
     * @retrun Array
     */
    function get(array $info){
        $info = array('year' => $this->year,
                      'month'=> $this->month,
                      'day'  => $this->day);
        return $info;
    }
    /**
     * @desc   获得指定日期的星期值
     * @params String $year
     * @params String $month
     * @params String $day
     * @return String
     */
    private function getWeek($year, $month, $day){
        $weekday = date("w",mktime(0,0,0,$month,$day,$year));
        return $weekday;
    }
 

    /**
     * 输出日历,有兴趣的可以改进!
     * 其实这不是一个方法,不希望在类里出现html和样式
     * 有兴趣的可以改进下!给大家起个抛砖引玉的作用
     *
     */
    public function out(){
        $firstDay = $this->getWeek($this->year, $this->month, 1);
        echo "<div style="margin:0;border:1 solid black;width:300;font:9pt">".
             "<form action=$_SERVER[PHP_SELF] method="post" style="margin:0">".
             "<select name="month" onchange="this.form.submit();">";

        /*打印12个月*/
        for($month = 1; $month <= 12; $month++){
            $tmp = sprintf("%02d", $month);
            if(strcmp($tmp, $this->month) == 0){
                $select = "selected style="background-color:#c0c0c0"";
            }else{
                $select = "";
            }
            echo "<option value="$tmp" $select>".$this->monthes[$tmp]."</option>rn";
        }
        echo "</select><select name="year" onchange="this.form.submit();">";

        /*打印年份,前后10年*/
        for($year = $this->year - 10; $year < $this->year + 10; $year++){
            if($year > 2037){break;}
            if($year < 1970){continue;}
            if(strcmp($year, $this->year) == 0){
                $select = "selected style="background-color:#c0c0c0"";
            }else{
                $select = "";
            }
            echo "<option value="$year" $select>$year</option>rn";
        }
        echo "</select></form><table border=0 align=center>";

        /*打印星期标头*/
        for($week = 0; $week < count($this->week); $week++){
            echo "<td>".$this->week[$week];
        }

        /*打印所有日期*/
        for($tmpd = 1; $tmpd <= date("t",mktime(0,0,0,$this->month,$this->day,$this->year)); $tmpd++){
            if(strcmp($tmpd, $this->day) == 0){   //获得当前日期,做标记
                $flag="bgcolor='#ff0000'";
            }else{
                $flag=" bgcolor='#ffffff'";
            }
            if($tmpd == 1){
                echo "<tr>";      //补充打印
                for($i = 0; $i < $firstDay; $i++){
                    echo "<td>";
                }
            }
            if(strcmp($this->getWeek($this->year, $this->month, $tmpd), 0) == 0){
                echo "<tr><td align=center $flag>$tmpd";
            }else{
                echo "<td align=center $flag>$tmpd";
            }
        }
        echo "</table></div>";
    }
}
$obj = new Calendar();
$obj->out();
?>

php 模拟登陆$url = "http://www.discuz.net/";
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TheWorld)";
$cookie = "dznet_cookietime=2592000;dznet_onlineusernum=7816;dznet_sid=QtlC87;dznet_auth=6bbeCQrzGv4eliNMLgU%2FlGZSpzbrsauGO1l0OBp6VQw5p0bcEg0xd4slYCM2ks%2FL0YCVYSO7XP2z8GMaxkPDUbXZCWft;checkpm=1";
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_REFERER, "http://www.discuz.net/index.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
curl_exec($ch);
$tempCn = curl_multi_getcontent($ch);
curl_close($ch);
echo  $tempCn;


 function makeDirectory($directoryName) {
  $directoryName = str_replace("","/",$directoryName);
  $dirNames = explode('/', $directoryName);
  $total = count($dirNames) ;
  $temp = '';
  for($i=0; $i<$total; $i++) {
   $temp .= $dirNames[$i].'/';
   if (!is_dir($temp)) {
    $oldmask = umask(0);
    if (!mkdir($temp, 0777)) exit("不能建立目录 $temp");
    umask($oldmask);
   }
  }
  return true;
 }

ajax验证用户名二

<?php
header("Content-type:text/html;charset=gb2312");
require_once('inc/connect.php');
$xm=isset($_GET['txt'])?$_GET['txt']:'null';

if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $xm)){
  echo("<div id='error'>对不起,不能使用中文作为用户名! </div>");
  exit();
 }
if($xm=='null' || $xm=='' || strlen($xm)>10 || strlen($xm)<3){
 echo("<div id='error'>对不起,用户由3-12Aa_zZ及数字组成! </div>");
 exit();
}else{
 $result=mysql_query("select m_uid from wk_member where m_uid='$xm' ") or die('Error !'.mysql_error());
 if(!mysql_num_rows($result)){
  echo("<div id='success'><font color=red>&nbsp;&nbsp;&nbsp;&nbsp;恭喜你!此用户可以注册!</font> </div>");
 }else{
  echo("<div id='error'>对不起,用户己被注册请选择其它名称!</div>");
 }
}

?>

本站原创: www.111cn.net 

标签:[!--infotagslink--]

您可能感兴趣的文章: