php execel 导出xml 程序
php execel 导出xml 程序
<?php
class Excel_XML
{
private $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">";
private $footer = "</Workbook>";
private $lines = array ();
private $worksheet_title = "Table1";
private function addRow ($array)
{
// initialize all cells for this row
$cells = "";
// foreach key -> write value into cells
foreach ($array as $k => $v):
$cells .= "<Cell><Data ss:Type="String">" . utf8_encode($v) . "</Data></Cell>n";
endforeach;
// transform $cells content into one row
$this->lines[] = "<Row>n" . $cells . "</Row>n";
}
public function addArray ($array)
{
// run through the array and add them into rows
foreach ($array as $k => $v):
$this->addRow ($v);
endforeach;
}
public 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)
{
// deliver header (as recommended in php manual)
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename="" . $filename . ".xls"");
// print out document to the browser
// need to use stripslashes for the damn ">"
echo stripslashes ($this->header);
echo "n<Worksheet ss:Name="" . $this->worksheet_title . "">n<Table>n";
echo "<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/>n";
echo implode ("n", $this->lines);
echo "</Table>n</Worksheet>n";
echo $this->footer;
}
}
?>
实例
<?php
// include the php-excel class
require (dirname (__FILE__) . "/class-excel-xml.inc.php");
// create a dummy array
$doc = array (
1 => array ("Oliver", "Peter", "Paul"),
array ("Marlene", "Lucy", "Lina")
);
// generate excel file
$xls = new Excel_XML;
$xls->addArray ( $doc );
$xls->generateXML ("mytest");
?>
preg_match_all -- 进行全局正则表达式匹配
说明
int preg_match_all ( string pattern, string subject, array matches [, int flags] )
在 subject 中搜索所有与 pattern 给出的正则表达式匹配的内容并将结果以 flags 指定的顺序放到 matches 中。
搜索到第一个匹配项之后,接下来的搜索从上一个匹配项末尾开始。
flags 可以是下列标记的组合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起来用没有意义):
PREG_PATTERN_ORDER
对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。
例子:
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
?>
本例将输出:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。
PREG_SET_ORDER
对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。
例子:
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
?>
本例将输出:
<b>example: </b>, example:
<div align=left>this is a test</div>, this is a test
本例中,$matches[0] 是第一组匹配结果,$matches[0][0] 包含匹配整个模式的文本,$matches[0][1] 包含匹配第一个子模式的文本,以此类推。同样,$matches[1] 是第二组匹配结果,等等。
PREG_OFFSET_CAPTURE
如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其在 subject 中的偏移量。本标记自 PHP 4.3.0 起可用。
如果没有给出标记,则假定为 PREG_PATTERN_ORDER。
返回整个模式匹配的次数(可能为零),如果出错返回 FALSE。
例子 1. 从某文本中取得所有的电话号码:
<?php
preg_match_all ("/(? (d{3})? )? (?(1) [-s] ) d{3}-d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
?>
例子 2. 搜索匹配的 HTML 标记(greedy):
<?php
// 是一个逆向引用的例子,其在 PCRE 中的含义是
// 必须匹配正则表达式本身中第二组括号内的内容,本例中
// 就是 ([w]+)。因为字符串在双引号中,所以需要
// 多加一个反斜线。
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all ("/(<([w]+)[^>]*>)(.*)(</>)/", $html, $matches);
for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: ".$matches[0][$i]."n";
echo "part 1: ".$matches[1][$i]."n";
echo "part 2: ".$matches[3][$i]."n";
echo "part 3: ".$matches[4][$i]."nn";
}
?>
本例将输出:
matched: <b>bold text</b>
part 1: <b>
part 2: bold text
part 3: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a>
php strtok 与 strstr 比较
// 完整匹配中间的box
04 echo strstr('aaaaaaaaaaaboaaaaaaaaaaaaboxcccccccccbcccccccccccccc','box')."n";
05 // 完整匹配中间的box 不因前而的b而停止
06 echo strstr('aaaaaaAbaaa aaaa aaaaaaaaaboxccccccccccccboxccccccccccc','box')."n";
07 // 有两个关键字时, 遇到第一个停止.
08 echo strstr('Subscrtibe our to free newsletter about New Freew to','to')."n";
09
10 // 用strtok往回取
11
12 // 没有box冲突时, 截取是正常的.
13 echo strtok('aaaaaaaaaaaaaaaaaaaaaaaboxccccccccccccccccccccccc','box')."n";
14 // 当前面有关键字中的任意一个字节时, 变态现象开始.注意看sb 及io那儿.
15 echo strtok('aaaaaaAaaa aaaa aaaaa sb aaaacccccc iox iccccccccccccccccc','box')."n";
16 // 难理解吧, 你应该看到了our中的o在关键<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D7%D6%B7%FB">字符</SPAN>中..
17 echo strtok('Subscribe our free newsletter about New Freew','to');
18
19 //事实证明, strstr 是支持多字符串的, 而strtok在关键字符冲突时,仅支持单个字节截取.
php 常用的排序算法代码[冒泡,递归排序
冒泡排序算法
function bubblesort($arr)
{
$n=count($arr);
for($i=0;$i<$n;$i++)
{
for($j=$i;$j<=$n-1;$j++)
{
if($arr[$i]>$arr[$j])
{
$temp=$arr[$i];
$arr[$i]=$arr[$j];
$arr[$j]=$temp;
}
}
}
return $arr;
}
//直接插入排序
function insertsort($arr)
{
$n=count($arr);
for($i=1;$i<$n;++$i)
{
if($arr[$i]<$arr[$i-1])
{
$tmp=$arr[$i];
$j=0;
for($j=$i-1;$j>=0&&$tmp<$arr[$j];--$j)
{
$arr[$j+1]=$arr[$j];
}
$arr[$j+1]=$tmp;
}
}
return $arr;
}简单排序算法
function simplesort($arr)
{
$tmp=0;
$t=0;
$n=count($arr);
for($i=0;$i<$n;++$i)
{
$t=$i;
for($j=$i+1;$j<$n;++$j)
{
if($arr[$t]>$arr[$j])
{
$t=$j;
}
}
$tmp=$arr[$i];
$arr[$i]=$arr[$t];
$arr[$t]=$tmp;
}
return $arr;
}快速排序算法
function qsort($array)
{
$n=count($array);
if($n<=1){return $array;}
$key=$array[0];
$l=array();
$r=array();
//分别进行递归排序,然后合成一个数组
for($i=1;$i<$n;$i++)
{
if($array[$i]<=$key)
{
$l[]=$array[$i];
}
else
{
$r[]=$array[$i];
}
}
$l=qsort($l);
$r=qsort($r);
return array_merge($l,array($key),$r);
}
php 生成随机函数
function genRandomString($len)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
还有一种是php 自带的mt_rand 与rand 哦,不过建意最好用mt_rand这个事速度上优与rand
相关文章
- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
- 这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
js导出table数据到excel即导出为EXCEL文档的方法
复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta ht...2013-10-13- 大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
- 这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
- 本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文
时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25- 这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
- 这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
- 这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
- C# 导出 Excel 的6种简单方法:数据表导出到 Excel,对象集合导出到 Excel,数据库导出到 Excel,微软网格控件导出到 Excel,数组导出到 Excel,CSV 导出到 Excel,你都会了吗?需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要介绍了Python爬取微信小程序通用方法代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-29
- 这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
- 这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
- 本文主要介绍了手把手教你uniapp和小程序分包,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-02
- 这篇文章主要为大家详细介绍了微信小程序实现canvas分享朋友圈海报,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
- 这篇文章主要介绍了微信小程序 页面跳转传递值几种方法详解的相关资料,需要的朋友可以参考下...2017-01-16