首頁  >  文章  >  php教程  >  PHP导入excel数据到MYSQL - roucheng

PHP导入excel数据到MYSQL - roucheng

WBOY
WBOY原創
2016-05-20 10:13:151005瀏覽

这里介绍一个直接将excel文件导入mysql的例子。我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用。
PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader
说明: 
测试环境:MYSQL数据库采用utf8编码.导入EXCEL文档是xls格式,经过测试,xlsx 格式[excel 2007]也OK. 
文中红色标注为需要注意的地方,请替换成你配置好的数据,如数据库配置等。运行http://localost/test.php实现导入。 
以下是我贴出的详细代码,其中test.php为我写的测试文件,reader.php和oleread.inc文件是从上面提供的网址中下载的。 
1. test.php

代码如下:

<span style="color: #000000;">php
</span><span style="color: #0000ff;">require_once</span> './includes/reader.php'<span style="color: #000000;">; 
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ExcelFile($filename, $encoding); </span>
<span style="color: #800080;">$data</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Spreadsheet_Excel_Reader(); 
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Set output Encoding. </span>
<span style="color: #800080;">$data</span>->setOutputEncoding('gbk'<span style="color: #000000;">); 
</span><span style="color: #008000;">//</span><span style="color: #008000;">”data.xls”是指要导入到mysql中的excel文件 </span>
<span style="color: #800080;">$data</span>->read('date.xls'<span style="color: #000000;">); 
@ </span><span style="color: #800080;">$db</span> = <span style="color: #008080;">mysql_connect</span>('localhost', 'root', '1234'<span style="color: #000000;">) or 
</span><span style="color: #0000ff;">die</span>("Could not connect to database.");<span style="color: #008000;">//</span><span style="color: #008000;">连接数据库 </span>
<span style="color: #008080;">mysql_query</span>("set names 'gbk'");<span style="color: #008000;">//</span><span style="color: #008000;">输出中文 </span>
<span style="color: #008080;">mysql_select_db</span>('wenhuaedu'); <span style="color: #008000;">//</span><span style="color: #008000;">选择数据库 </span>
<span style="color: #008080;">error_reporting</span>(<span style="color: #ff00ff;">E_ALL</span> ^ <span style="color: #ff00ff;">E_NOTICE</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 1; <span style="color: #800080;">$i</span> $data->sheets[0]['numRows']; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { 
</span><span style="color: #008000;">//</span><span style="color: #008000;">以下注释的for循环打印excel表数据 </span><span style="color: #008000;">
/*</span><span style="color: #008000;"> 
for ($j = 1; $j sheets[0]['numCols']; $j++) { 
echo """.$data->sheets[0]['cells'][$i][$j]."","; 
} 
echo "n"; 
</span><span style="color: #008000;">*/</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> 何问起 hovertree.com </span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;">以下代码是将excel表数据【3个字段】插入到mysql中,根据你的excel表字段的多少,改写以下代码吧! </span>
<span style="color: #800080;">$sql</span> = "INSERT INTO test VALUES('". 
<span style="color: #800080;">$data</span>->sheets[0]['cells'][<span style="color: #800080;">$i</span>][1]."','". 
<span style="color: #800080;">$data</span>->sheets[0]['cells'][<span style="color: #800080;">$i</span>][2]."','". 
<span style="color: #800080;">$data</span>->sheets[0]['cells'][<span style="color: #800080;">$i</span>][3]."')"<span style="color: #000000;">; 
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$sql</span>.'<br>'<span style="color: #000000;">; 
</span><span style="color: #800080;">$res</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">); 
</span>?>

包含的文件 
OLERead.php 

<span style="color: #000000;">php 
</span><span style="color: #008080;">define</span>('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('ROOT_START_BLOCK_POS', 0x30<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('BIG_BLOCK_SIZE', 0x200<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('SMALL_BLOCK_SIZE', 0x40<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('EXTENSION_BLOCK_POS', 0x44<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('NUM_EXTENSION_BLOCK_POS', 0x48<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('PROPERTY_STORAGE_BLOCK_SIZE', 0x80<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('SMALL_BLOCK_THRESHOLD', 0x1000<span style="color: #000000;">); 
</span><span style="color: #008000;">//</span><span style="color: #008000;"> property storage offsets </span>
<span style="color: #008080;">define</span>('SIZE_OF_NAME_POS', 0x40<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('TYPE_POS', 0x42<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('START_BLOCK_POS', 0x74<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('SIZE_POS', 0x78<span style="color: #000000;">); 
</span><span style="color: #008080;">define</span>('IDENTIFIER_OLE', <span style="color: #008080;">pack</span>("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1<span style="color: #000000;">)); 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo 'ROOT_START_BLOCK_POS = '.ROOT_START_BLOCK_POS."\n"; 
//echo bin2hex($data[ROOT_START_BLOCK_POS])."\n"; 
//echo "a="; 
//echo $data[ROOT_START_BLOCK_POS]; 
//function log </span>
<span style="color: #0000ff;">function</span> GetInt4d(<span style="color: #800080;">$data</span>, <span style="color: #800080;">$pos</span><span style="color: #000000;">) 
{ 
</span><span style="color: #800080;">$value</span> = <span style="color: #008080;">ord</span>(<span style="color: #800080;">$data</span>[<span style="color: #800080;">$pos</span>]) | (<span style="color: #008080;">ord</span>(<span style="color: #800080;">$data</span>[<span style="color: #800080;">$pos</span>+1]) ord(<span style="color: #800080;">$data</span>[<span style="color: #800080;">$pos</span>+2]) ord(<span style="color: #800080;">$data</span>[<span style="color: #800080;">$pos</span>+3]) ); 
<span style="color: #0000ff;">if</span> (<span style="color: #800080;">$value</span>>=4294967294<span style="color: #000000;">) 
{ 
</span><span style="color: #800080;">$value</span>=-2<span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$value</span><span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> OLERead { 
</span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$data</span> = ''<span style="color: #000000;">; 
</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> OLERead(){ 
} 
</span><span style="color: #0000ff;">function</span> read(<span style="color: #800080;">$sFileName</span><span style="color: #000000;">){ 
</span><span style="color: #008000;">//</span><span style="color: #008000;"> check if file exist and is readable (Darko Miljanovic) </span>
<span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_readable</span>(<span style="color: #800080;">$sFileName</span><span style="color: #000000;">)) { 
</span><span style="color: #800080;">$this</span>->error = 1<span style="color: #000000;">; 
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; 
} 
</span><span style="color: #800080;">$this</span>->data = @<span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$sFileName</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">if</span> (!<span style="color: #800080;">$this</span>-><span style="color: #000000;">data) { 
</span><span style="color: #800080;">$this</span>->error = 1<span style="color: #000000;">; 
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; 
} 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo IDENTIFIER_OLE; 
//echo 'start'; </span>
<span style="color: #0000ff;">if</span> (<span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->data, 0, 8) !=<span style="color: #000000;"> IDENTIFIER_OLE) { 
</span><span style="color: #800080;">$this</span>->error = 1<span style="color: #000000;">; 
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; 
} 
</span><span style="color: #800080;">$this</span>->numBigBlockDepotBlocks = GetInt4d(<span style="color: #800080;">$this</span>->data,<span style="color: #000000;"> NUM_BIG_BLOCK_DEPOT_BLOCKS_POS); 
</span><span style="color: #800080;">$this</span>->sbdStartBlock = GetInt4d(<span style="color: #800080;">$this</span>->data,<span style="color: #000000;"> SMALL_BLOCK_DEPOT_BLOCK_POS); 
</span><span style="color: #800080;">$this</span>->rootStartBlock = GetInt4d(<span style="color: #800080;">$this</span>->data,<span style="color: #000000;"> ROOT_START_BLOCK_POS); 
</span><span style="color: #800080;">$this</span>->extensionBlock = GetInt4d(<span style="color: #800080;">$this</span>->data,<span style="color: #000000;"> EXTENSION_BLOCK_POS); 
</span><span style="color: #800080;">$this</span>->numExtensionBlocks = GetInt4d(<span style="color: #800080;">$this</span>->data,<span style="color: #000000;"> NUM_EXTENSION_BLOCK_POS); 
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 
echo $this->numBigBlockDepotBlocks." "; 
echo $this->sbdStartBlock." "; 
echo $this->rootStartBlock." "; 
echo $this->extensionBlock." "; 
echo $this->numExtensionBlocks." "; 
</span><span style="color: #008000;">*/</span> 
<span style="color: #008000;">//</span><span style="color: #008000;">echo "sbdStartBlock = $this->sbdStartBlock\n"; </span>
<span style="color: #800080;">$bigBlockDepotBlocks</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); 
</span><span style="color: #800080;">$pos</span> =<span style="color: #000000;"> BIG_BLOCK_DEPOT_BLOCKS_POS; 
</span><span style="color: #008000;">//</span><span style="color: #008000;"> echo "pos = $pos"; </span>
<span style="color: #800080;">$bbdBlocks</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">numBigBlockDepotBlocks; 
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$this</span>->numExtensionBlocks != 0<span style="color: #000000;">) { 
</span><span style="color: #800080;">$bbdBlocks</span> = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4<span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $bbdBlocks; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$bigBlockDepotBlocks</span>[<span style="color: #800080;">$i</span>] = GetInt4d(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span><span style="color: #000000;">); 
</span><span style="color: #800080;">$pos</span> += 4<span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$j</span> = 0; <span style="color: #800080;">$j</span> $this->numExtensionBlocks; <span style="color: #800080;">$j</span>++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$pos</span> = (<span style="color: #800080;">$this</span>->extensionBlock + 1) *<span style="color: #000000;"> BIG_BLOCK_SIZE; 
</span><span style="color: #800080;">$blocksToRead</span> = <span style="color: #008080;">min</span>(<span style="color: #800080;">$this</span>->numBigBlockDepotBlocks - <span style="color: #800080;">$bbdBlocks</span>, BIG_BLOCK_SIZE / 4 - 1<span style="color: #000000;">); 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = <span style="color: #800080;">$bbdBlocks</span>; <span style="color: #800080;">$i</span> $bbdBlocks + <span style="color: #800080;">$blocksToRead</span>; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$bigBlockDepotBlocks</span>[<span style="color: #800080;">$i</span>] = GetInt4d(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span><span style="color: #000000;">); 
</span><span style="color: #800080;">$pos</span> += 4<span style="color: #000000;">; 
} 
</span><span style="color: #800080;">$bbdBlocks</span> += <span style="color: #800080;">$blocksToRead</span><span style="color: #000000;">; 
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$bbdBlocks</span> $this-><span style="color: #000000;">numBigBlockDepotBlocks) { 
</span><span style="color: #800080;">$this</span>->extensionBlock = GetInt4d(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span><span style="color: #000000;">); 
} 
} </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 何问起 hovertree.com </span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> var_dump($bigBlockDepotBlocks); 
// readBigBlockDepot </span>
<span style="color: #800080;">$pos</span> = 0<span style="color: #000000;">; 
</span><span style="color: #800080;">$index</span> = 0<span style="color: #000000;">; 
</span><span style="color: #800080;">$this</span>->bigBlockChain = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $this->numBigBlockDepotBlocks; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$pos</span> = (<span style="color: #800080;">$bigBlockDepotBlocks</span>[<span style="color: #800080;">$i</span>] + 1) *<span style="color: #000000;"> BIG_BLOCK_SIZE; 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo "pos = $pos"; </span>
<span style="color: #0000ff;">for</span> (<span style="color: #800080;">$j</span> = 0 ; <span style="color: #800080;">$j</span> $j++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$this</span>->bigBlockChain[<span style="color: #800080;">$index</span>] = GetInt4d(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span><span style="color: #000000;">); 
</span><span style="color: #800080;">$pos</span> += 4<span style="color: #000000;"> ; 
</span><span style="color: #800080;">$index</span>++<span style="color: #000000;">; 
} 
} 
</span><span style="color: #008000;">//</span><span style="color: #008000;">var_dump($this->bigBlockChain); 
//echo '=====2'; 
// readSmallBlockDepot(); </span>
<span style="color: #800080;">$pos</span> = 0<span style="color: #000000;">; 
</span><span style="color: #800080;">$index</span> = 0<span style="color: #000000;">; 
</span><span style="color: #800080;">$sbdBlock</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">sbdStartBlock; 
</span><span style="color: #800080;">$this</span>->smallBlockChain = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); 
</span><span style="color: #0000ff;">while</span> (<span style="color: #800080;">$sbdBlock</span> != -2<span style="color: #000000;">) { 
</span><span style="color: #800080;">$pos</span> = (<span style="color: #800080;">$sbdBlock</span> + 1) *<span style="color: #000000;"> BIG_BLOCK_SIZE; 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$j</span> = 0; <span style="color: #800080;">$j</span> $j++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$this</span>->smallBlockChain[<span style="color: #800080;">$index</span>] = GetInt4d(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span><span style="color: #000000;">); 
</span><span style="color: #800080;">$pos</span> += 4<span style="color: #000000;">; 
</span><span style="color: #800080;">$index</span>++<span style="color: #000000;">; 
} 
</span><span style="color: #800080;">$sbdBlock</span> = <span style="color: #800080;">$this</span>->bigBlockChain[<span style="color: #800080;">$sbdBlock</span><span style="color: #000000;">]; 
} 
</span><span style="color: #008000;">//</span><span style="color: #008000;"> readData(rootStartBlock) </span>
<span style="color: #800080;">$block</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">rootStartBlock; 
</span><span style="color: #800080;">$pos</span> = 0<span style="color: #000000;">; 
</span><span style="color: #800080;">$this</span>->entry = <span style="color: #800080;">$this</span>->__readData(<span style="color: #800080;">$block</span><span style="color: #000000;">); 
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 
while ($block != -2) { 
$pos = ($block + 1) * BIG_BLOCK_SIZE; 
$this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE); 
$block = $this->bigBlockChain[$block]; 
} 
</span><span style="color: #008000;">*/</span> 
<span style="color: #008000;">//</span><span style="color: #008000;">echo '==='.$this->entry."==="; </span>
<span style="color: #800080;">$this</span>-><span style="color: #000000;">__readPropertySets(); 
} 
</span><span style="color: #0000ff;">function</span> __readData(<span style="color: #800080;">$bl</span><span style="color: #000000;">) { 
</span><span style="color: #800080;">$block</span> = <span style="color: #800080;">$bl</span><span style="color: #000000;">; 
</span><span style="color: #800080;">$pos</span> = 0<span style="color: #000000;">; 
</span><span style="color: #800080;">$data</span> = ''<span style="color: #000000;">; 
</span><span style="color: #0000ff;">while</span> (<span style="color: #800080;">$block</span> != -2<span style="color: #000000;">) { 
</span><span style="color: #800080;">$pos</span> = (<span style="color: #800080;">$block</span> + 1) *<span style="color: #000000;"> BIG_BLOCK_SIZE; 
</span><span style="color: #800080;">$data</span> = <span style="color: #800080;">$data</span>.<span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span>,<span style="color: #000000;"> BIG_BLOCK_SIZE); 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo "pos = $pos data=$data\n"; </span>
<span style="color: #800080;">$block</span> = <span style="color: #800080;">$this</span>->bigBlockChain[<span style="color: #800080;">$block</span><span style="color: #000000;">]; 
} 
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> __readPropertySets(){ 
</span><span style="color: #800080;">$offset</span> = 0<span style="color: #000000;">; 
</span><span style="color: #008000;">//</span><span style="color: #008000;">var_dump($this->entry); </span>
<span style="color: #0000ff;">while</span> (<span style="color: #800080;">$offset</span> strlen(<span style="color: #800080;">$this</span>-><span style="color: #000000;">entry)) { 
</span><span style="color: #800080;">$d</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->entry, <span style="color: #800080;">$offset</span>,<span style="color: #000000;"> PROPERTY_STORAGE_BLOCK_SIZE); 
</span><span style="color: #800080;">$nameSize</span> = <span style="color: #008080;">ord</span>(<span style="color: #800080;">$d</span>[SIZE_OF_NAME_POS]) | (<span style="color: #008080;">ord</span>(<span style="color: #800080;">$d</span>[SIZE_OF_NAME_POS+1]) ); 
<span style="color: #800080;">$type</span> = <span style="color: #008080;">ord</span>(<span style="color: #800080;">$d</span><span style="color: #000000;">[TYPE_POS]); 
</span><span style="color: #008000;">//</span><span style="color: #008000;">$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1; </span>
<span style="color: #800080;">$startBlock</span> = GetInt4d(<span style="color: #800080;">$d</span>,<span style="color: #000000;"> START_BLOCK_POS); 
</span><span style="color: #800080;">$size</span> = GetInt4d(<span style="color: #800080;">$d</span>,<span style="color: #000000;"> SIZE_POS); 
</span><span style="color: #800080;">$name</span> = ''<span style="color: #000000;">; 
</span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $nameSize ; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { 
</span><span style="color: #800080;">$name</span> .= <span style="color: #800080;">$d</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]; 
} 
</span><span style="color: #800080;">$name</span> = <span style="color: #008080;">str_replace</span>("\x00", "", <span style="color: #800080;">$name</span><span style="color: #000000;">); 
</span><span style="color: #800080;">$this</span>->props[] = <span style="color: #0000ff;">array</span><span style="color: #000000;"> ( 
</span>'name' => <span style="color: #800080;">$name</span>, 
'type' => <span style="color: #800080;">$type</span>, 
'startBlock' => <span style="color: #800080;">$startBlock</span>, 
'size' => <span style="color: #800080;">$size</span><span style="color: #000000;">); 
</span><span style="color: #0000ff;">if</span> ((<span style="color: #800080;">$name</span> == "Workbook") || (<span style="color: #800080;">$name</span> == "Book"<span style="color: #000000;">)) { 
</span><span style="color: #800080;">$this</span>->wrkbook = <span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>->props) - 1<span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$name</span> == "Root Entry"<span style="color: #000000;">) { 
</span><span style="color: #800080;">$this</span>->rootentry = <span style="color: #008080;">count</span>(<span style="color: #800080;">$this</span>->props) - 1<span style="color: #000000;">; 
} 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo "name ==$name=\n"; </span>
<span style="color: #800080;">$offset</span> +=<span style="color: #000000;"> PROPERTY_STORAGE_BLOCK_SIZE; 
} 
} 
</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> getWorkBook(){ 
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$this</span>->props[<span style="color: #800080;">$this</span>->wrkbook]['size']  SMALL_BLOCK_THRESHOLD){ 
<span style="color: #008000;">//</span><span style="color: #008000;"> getSmallBlockStream(PropertyStorage ps) </span>
<span style="color: #800080;">$rootdata</span> = <span style="color: #800080;">$this</span>->__readData(<span style="color: #800080;">$this</span>->props[<span style="color: #800080;">$this</span>->rootentry]['startBlock'<span style="color: #000000;">]); 
</span><span style="color: #800080;">$streamData</span> = ''<span style="color: #000000;">; 
</span><span style="color: #800080;">$block</span> = <span style="color: #800080;">$this</span>->props[<span style="color: #800080;">$this</span>->wrkbook]['startBlock'<span style="color: #000000;">]; 
</span><span style="color: #008000;">//</span><span style="color: #008000;">$count = 0; </span>
<span style="color: #800080;">$pos</span> = 0<span style="color: #000000;">; 
</span><span style="color: #0000ff;">while</span> (<span style="color: #800080;">$block</span> != -2<span style="color: #000000;">) { 
</span><span style="color: #800080;">$pos</span> = <span style="color: #800080;">$block</span> *<span style="color: #000000;"> SMALL_BLOCK_SIZE; 
</span><span style="color: #800080;">$streamData</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$rootdata</span>, <span style="color: #800080;">$pos</span>,<span style="color: #000000;"> SMALL_BLOCK_SIZE); 
</span><span style="color: #800080;">$block</span> = <span style="color: #800080;">$this</span>->smallBlockChain[<span style="color: #800080;">$block</span><span style="color: #000000;">]; 
} 
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$streamData</span><span style="color: #000000;">; 
}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ 
</span><span style="color: #800080;">$numBlocks</span> = <span style="color: #800080;">$this</span>->props[<span style="color: #800080;">$this</span>->wrkbook]['size'] /<span style="color: #000000;"> BIG_BLOCK_SIZE; 
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$this</span>->props[<span style="color: #800080;">$this</span>->wrkbook]['size'] % BIG_BLOCK_SIZE != 0<span style="color: #000000;">) { 
</span><span style="color: #800080;">$numBlocks</span>++<span style="color: #000000;">; 
} 
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$numBlocks</span> == 0) <span style="color: #0000ff;">return</span> ''<span style="color: #000000;">; 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo "numBlocks = $numBlocks\n"; 
//byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE]; 
//print_r($this->wrkbook); </span>
<span style="color: #800080;">$streamData</span> = ''<span style="color: #000000;">; 
</span><span style="color: #800080;">$block</span> = <span style="color: #800080;">$this</span>->props[<span style="color: #800080;">$this</span>->wrkbook]['startBlock'<span style="color: #000000;">]; 
</span><span style="color: #008000;">//</span><span style="color: #008000;">$count = 0; </span>
<span style="color: #800080;">$pos</span> = 0<span style="color: #000000;">; 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo "block = $block"; </span>
<span style="color: #0000ff;">while</span> (<span style="color: #800080;">$block</span> != -2<span style="color: #000000;">) { 
</span><span style="color: #800080;">$pos</span> = (<span style="color: #800080;">$block</span> + 1) *<span style="color: #000000;"> BIG_BLOCK_SIZE; 
</span><span style="color: #800080;">$streamData</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$this</span>->data, <span style="color: #800080;">$pos</span>,<span style="color: #000000;"> BIG_BLOCK_SIZE); 
</span><span style="color: #800080;">$block</span> = <span style="color: #800080;">$this</span>->bigBlockChain[<span style="color: #800080;">$block</span><span style="color: #000000;">]; 
} 
</span><span style="color: #008000;">//</span><span style="color: #008000;">echo 'stream'.$streamData; </span>
<span style="color: #0000ff;">return</span> <span style="color: #800080;">$streamData</span><span style="color: #000000;">; 
} 
} 
} 
</span>?>

参考:http://hovertree.com/h/bjaf/to3l3tjm.htm

http://www.cnblogs.com/roucheng/p/phpmysql.html

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn