您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 将excel导入mysql_PHP教程

2024/2/24 23:35:54发布22次查看
这里介绍一个直接将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
复制代码 代码如下:
require_once './includes/reader.php';
// excelfile($filename, $encoding);
$data = new spreadsheet_excel_reader();
// set output encoding.
$data->setoutputencoding('gbk');
//”data.xls”是指要导入到mysql中的excel文件
$data->read('date.xls');
@ $db = mysql_connect('localhost', 'root', '1234') or
die(could not connect to database.);//连接数据库
mysql_query(set names 'gbk');//输出中文
mysql_select_db('wenhuaedu'); //选择数据库
error_reporting(e_all ^ e_notice);
for ($i = 1; $i sheets[0]['numrows']; $i++) {
//以下注释的for循环打印excel表数据
/*
for ($j = 1; $j sheets[0]['numcols']; $j++) {
echo .$data->sheets[0]['cells'][$i][$j].,;
}
echo n;
*/
//以下代码是将excel表数据【3个字段】插入到mysql中,根据你的excel表字段的多少,改写以下代码吧!
$sql = insert into test values('.
$data->sheets[0]['cells'][$i][1].','.
$data->sheets[0]['cells'][$i][2].','.
$data->sheets[0]['cells'][$i][3].');
echo $sql.'
';
$res = mysql_query($sql);
贴出其中包含的文件
oleread.php
复制代码 代码如下:
=4294967294)
{
$value=-2;
}
return $value;
}
class oleread {
var $data = '';
function oleread(){
}
function read($sfilename){
// check if file exist and is readable (darko miljanovic)
if(!is_readable($sfilename)) {
$this->error = 1;
return false;
}
$this->data = @file_get_contents($sfilename);
if (!$this->data) {
$this->error = 1;
return false;
}
//echo identifier_ole;
//echo 'start';
if (substr($this->data, 0, 8) != identifier_ole) {
$this->error = 1;
return false;
}
$this->numbigblockdepotblocks = getint4d($this->data, num_big_block_depot_blocks_pos);
$this->sbdstartblock = getint4d($this->data, small_block_depot_block_pos);
$this->rootstartblock = getint4d($this->data, root_start_block_pos);
$this->extensionblock = getint4d($this->data, extension_block_pos);
$this->numextensionblocks = getint4d($this->data, num_extension_block_pos);
/*
echo $this->numbigblockdepotblocks. ;
echo $this->sbdstartblock. ;
echo $this->rootstartblock. ;
echo $this->extensionblock. ;
echo $this->numextensionblocks. ;
*/
//echo sbdstartblock = $this->sbdstartblock\n;
$bigblockdepotblocks = array();
$pos = big_block_depot_blocks_pos;
// echo pos = $pos;
$bbdblocks = $this->numbigblockdepotblocks;
if ($this->numextensionblocks != 0) {
$bbdblocks = (big_block_size - big_block_depot_blocks_pos)/4;
}
for ($i = 0; $i $bigblockdepotblocks[$i] = getint4d($this->data, $pos);
$pos += 4;
}
for ($j = 0; $j numextensionblocks; $j++) {
$pos = ($this->extensionblock + 1) * big_block_size;
$blockstoread = min($this->numbigblockdepotblocks - $bbdblocks, big_block_size / 4 - 1);
for ($i = $bbdblocks; $i $bigblockdepotblocks[$i] = getint4d($this->data, $pos);
$pos += 4;
}
$bbdblocks += $blockstoread;
if ($bbdblocks numbigblockdepotblocks) {
$this->extensionblock = getint4d($this->data, $pos);
}
}
// var_dump($bigblockdepotblocks);
// readbigblockdepot
$pos = 0;
$index = 0;
$this->bigblockchain = array();
for ($i = 0; $i numbigblockdepotblocks; $i++) {
$pos = ($bigblockdepotblocks[$i] + 1) * big_block_size;
//echo pos = $pos;
for ($j = 0 ; $j $this->bigblockchain[$index] = getint4d($this->data, $pos);
$pos += 4 ;
$index++;
}
}
//var_dump($this->bigblockchain);
//echo '=====2';
// readsmallblockdepot();
$pos = 0;
$index = 0;
$sbdblock = $this->sbdstartblock;
$this->smallblockchain = array();
while ($sbdblock != -2) {
$pos = ($sbdblock + 1) * big_block_size;
for ($j = 0; $j $this->smallblockchain[$index] = getint4d($this->data, $pos);
$pos += 4;
$index++;
}
$sbdblock = $this->bigblockchain[$sbdblock];
}
// readdata(rootstartblock)
$block = $this->rootstartblock;
$pos = 0;
$this->entry = $this->__readdata($block);
/*
while ($block != -2) {
$pos = ($block + 1) * big_block_size;
$this->entry = $this->entry.substr($this->data, $pos, big_block_size);
$block = $this->bigblockchain[$block];
}
*/
//echo '==='.$this->entry.===;
$this->__readpropertysets();
}
function __readdata($bl) {
$block = $bl;
$pos = 0;
$data = '';
while ($block != -2) {
$pos = ($block + 1) * big_block_size;
$data = $data.substr($this->data, $pos, big_block_size);
//echo pos = $pos data=$data\n;
$block = $this->bigblockchain[$block];
}
return $data;
}
function __readpropertysets(){
$offset = 0;
//var_dump($this->entry);
while ($offset entry)) {
$d = substr($this->entry, $offset, property_storage_block_size);
$namesize = ord($d[size_of_name_pos]) | (ord($d[size_of_name_pos+1]) $type = ord($d[type_pos]);
//$maxblock = strlen($d) / big_block_size - 1;
$startblock = getint4d($d, start_block_pos);
$size = getint4d($d, size_pos);
$name = '';
for ($i = 0; $i $name .= $d[$i];
}
$name = str_replace(\x00, , $name);
$this->props[] = array (
'name' => $name,
'type' => $type,
'startblock' => $startblock,
'size' => $size);
if (($name == workbook) || ($name == book)) {
$this->wrkbook = count($this->props) - 1;
}
if ($name == root entry) {
$this->rootentry = count($this->props) - 1;
}
//echo name ==$name=\n;
$offset += property_storage_block_size;
}
}
function getworkbook(){
if ($this->props[$this->wrkbook]['size'] // getsmallblockstream(propertystorage ps)
$rootdata = $this->__readdata($this->props[$this->rootentry]['startblock']);
$streamdata = '';
$block = $this->props[$this->wrkbook]['startblock'];
//$count = 0;
$pos = 0;
while ($block != -2) {
$pos = $block * small_block_size;
$streamdata .= substr($rootdata, $pos, small_block_size);
$block = $this->smallblockchain[$block];
}
return $streamdata;
}else{
$numblocks = $this->props[$this->wrkbook]['size'] / big_block_size;
if ($this->props[$this->wrkbook]['size'] % big_block_size != 0) {
$numblocks++;
}
if ($numblocks == 0) return '';
//echo numblocks = $numblocks\n;
//byte[] streamdata = new byte[numblocks * big_block_size];
//print_r($this->wrkbook);
$streamdata = '';
$block = $this->props[$this->wrkbook]['startblock'];
//$count = 0;
$pos = 0;
//echo block = $block;
while ($block != -2) {
$pos = ($block + 1) * big_block_size;
$streamdata .= substr($this->data, $pos, big_block_size);
$block = $this->bigblockchain[$block];
}
//echo 'stream'.$streamdata;
return $streamdata;
}
}
}
?>
还有一个
reader.php
复制代码 代码如下:
'cells' --> row --> column --> interpreted value
* --> 'cellsinfo' --> row --> column --> 'type' - can be 'date', 'number', or 'unknown'
* --> 'raw' - the raw data that excel stores for that data cell
*
* @var array
* @access public
*/
var $sheets = array();
/**
* the data returned by ole
*
* @var string
* @access public
*/
var $data;
/**
* ole object for reading the file
*
* @var ole object
* @access private
*/
var $_ole;
/**
* default encoding
*
* @var string
* @access private
*/
var $_defaultencoding;
/**
* default number format
*
* @var integer
* @access private
*/
var $_defaultformat = spreadsheet_excel_reader_def_num_format;
/**
* todo
* list of formats to use for each column
*
* @var array
* @access private
*/
var $_columnsformat = array();
/**
* todo
*
* @var integer
* @access private
*/
var $_rowoffset = 1;
/**
* todo
*
* @var integer
* @access private
*/
var $_coloffset = 1;
/**
* list of default date formats used by excel
*
* @var array
* @access public
*/
var $dateformats = array (
0xe => d/m/y,
0xf => d-m-y,
0x10 => d-m,
0x11 => m-y,
0x12 => h:i a,
0x13 => h:i:s a,
0x14 => h:i,
0x15 => h:i:s,
0x16 => d/m/y h:i,
0x2d => i:s,
0x2e => h:i:s,
0x2f => i:s.s);
/**
* default number formats used by excel
*
* @var array
* @access public
*/
var $numberformats = array(
0x1 => %1.0f, // 0
0x2 => %1.2f, // 0.00,
0x3 => %1.0f, //#,##0,
0x4 => %1.2f, //#,##0.00,
0x5 => %1.0f, /*$#,##0;($#,##0),*/
0x6 => '$%1.0f', /*$#,##0;($#,##0),*/
0x7 => '$%1.2f', //$#,##0.00;($#,##0.00),
0x8 => '$%1.2f', //$#,##0.00;($#,##0.00),
0x9 => '%1.0f%%', // 0%
0xa => '%1.2f%%', // 0.00%
0xb => '%1.2f', // 0.00e00,
0x25 => '%1.0f', // #,##0;(#,##0),
0x26 => '%1.0f', //#,##0;(#,##0),
0x27 => '%1.2f', //#,##0.00;(#,##0.00),
0x28 => '%1.2f', //#,##0.00;(#,##0.00),
0x29 => '%1.0f', //#,##0;(#,##0),
0x2a => '$%1.0f', //$#,##0;($#,##0),
0x2b => '%1.2f', //#,##0.00;(#,##0.00),
0x2c => '$%1.2f', //$#,##0.00;($#,##0.00),
0x30 => '%1.0f'); //##0.0e0;
// }}}
// {{{ spreadsheet_excel_reader()
/**
* constructor
*
* some basic initialisation
*/
function spreadsheet_excel_reader()
{
@$this->_ole =& new oleread();
@$this->setutfencoder('iconv');
}
// }}}
// {{{ setoutputencoding()
/**
* set the encoding method
*
* @param string encoding to use
* @access public
*/
function setoutputencoding($encoding)
{
$this->_defaultencoding = $encoding;
}
// }}}
// {{{ setutfencoder()
/**
* $encoder = 'iconv' or 'mb'
* set iconv if you would like use 'iconv' for encode utf-16le to your encoding
* set mb if you would like use 'mb_convert_encoding' for encode utf-16le to your encoding
*
* @access public
* @param string encoding type to use. either 'iconv' or 'mb'
*/
function setutfencoder($encoder = 'iconv')
{
$this->_encoderfunction = '';
if ($encoder == 'iconv') {
$this->_encoderfunction = function_exists('iconv') ? 'iconv' : '';
} elseif ($encoder == 'mb') {
$this->_encoderfunction = function_exists('mb_convert_encoding') ?
'mb_convert_encoding' :
'';
}
}
// }}}
// {{{ setrowcoloffset()
/**
* todo
*
* @access public
* @param offset
*/
function setrowcoloffset($ioffset)
{
$this->_rowoffset = $ioffset;
$this->_coloffset = $ioffset;
}
// }}}
// {{{ setdefaultformat()
/**
* set the default number format
*
* @access public
* @param default format
*/
function setdefaultformat($sformat)
{
$this->_defaultformat = $sformat;
}
// }}}
// {{{ setcolumnformat()
/**
* force a column to use a certain format
*
* @access public
* @param integer column number
* @param string format
*/
function setcolumnformat($column, $sformat)
{
$this->_columnsformat[$column] = $sformat;
}
// }}}
// {{{ read()
/**
* read the spreadsheet file using ole, then parse
*
* @access public
* @param filename
* @todo return a valid value
*/
function read($sfilename)
{
/*
require_once 'ole.php';
$ole = new ole();
$ole->read($sfilename);
foreach ($ole->_list as $i => $pps) {
if (($pps->name == 'workbook' || $pps->name == 'book') &&
$pps->size >= small_block_threshold) {
$this->data = $ole->getdata($i, 0, $ole->getdatalength($i));
} elseif ($pps->name == 'root entry') {
$this->data = $ole->getdata($i, 0, $ole->getdatalength($i));
}
//var_dump(strlen($ole->getdata($i, 0, $ole->getdatalength($i))), $pps->name, md5($this->data), $ole->getdatalength($i));
}
//exit;
$this->_parse();
return sizeof($this->sheets) > 0;
*/
$res = $this->_ole->read($sfilename);
// oops, something goes wrong (darko miljanovic)
if($res === false) {
// check error code
if($this->_ole->error == 1) {
// bad file
die('the filename ' . $sfilename . ' is not readable');
}
// check other error codes here (eg bad fileformat, etc)
}
$this->data = $this->_ole->getworkbook();
/*
$res = $this->_ole->read($sfilename);
if ($this->iserror($res)) {
// var_dump($res);
return $this->raiseerror($res);
}
$total = $this->_ole->ppstotal();
for ($i = 0; $i if ($this->_ole->isfile($i)) {
$type = unpack(v, $this->_ole->getdata($i, 0, 2));
if ($type[''] == 0x0809) { // check if it's a biff stream
$this->_index = $i;
$this->data = $this->_ole->getdata($i, 0, $this->_ole->getdatalength($i));
break;
}
}
}
if ($this->_index === null) {
return $this->raiseerror($file doesn't seem to be an excel file);
}
*/
//echo data =.$this->data;
//$this->readrecords();
$this->_parse();
}
// }}}
// {{{ _parse()
/**
* parse a workbook
*
* @access private
* @return bool
*/
function _parse()
{
$pos = 0;
$code = ord($this->data[$pos]) | ord($this->data[$pos+1])$length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])$version = ord($this->data[$pos + 4]) | ord($this->data[$pos + 5])$substreamtype = ord($this->data[$pos + 6]) | ord($this->data[$pos + 7])//echo start parse code=.base_convert($code,10,16). version=.base_convert($version,10,16). substreamtype=.base_convert($substreamtype,10,16)..\n;
if (($version != spreadsheet_excel_reader_biff8) &&
($version != spreadsheet_excel_reader_biff7)) {
return false;
}
if ($substreamtype != spreadsheet_excel_reader_workbookglobals){
return false;
}
//print_r($rec);
$pos += $length + 4;
$code = ord($this->data[$pos]) | ord($this->data[$pos+1])$length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])while ($code != spreadsheet_excel_reader_type_eof) {
switch ($code) {
case spreadsheet_excel_reader_type_sst:
//echo type_sst\n;
$spos = $pos + 4;
$limitpos = $spos + $length;
$uniquestrings = $this->_getint4d($this->data, $spos+4);
$spos += 8;
for ($i = 0; $i // read in the number of characters
if ($spos == $limitpos) {
$opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])$conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])if ($opcode != 0x3c) {
return -1;
}
$spos += 4;
$limitpos = $spos + $conlength;
}
$numchars = ord($this->data[$spos]) | (ord($this->data[$spos+1]) //echo i = $i pos = $pos numchars = $numchars ;
$spos += 2;
$optionflags = ord($this->data[$spos]);
$spos++;
$asciiencoding = (($optionflags & 0x01) == 0) ;
$extendedstring = ( ($optionflags & 0x04) != 0);
// see if string contains formatting information
$richstring = ( ($optionflags & 0x08) != 0);
if ($richstring) {
// read in the crun
$formattingruns = ord($this->data[$spos]) | (ord($this->data[$spos+1]) $spos += 2;
}
if ($extendedstring) {
// read in cchextrst
$extendedrunlength = $this->_getint4d($this->data, $spos);
$spos += 4;
}
$len = ($asciiencoding)? $numchars : $numchars*2;
if ($spos + $len $retstr = substr($this->data, $spos, $len);
$spos += $len;
}else{
// found countinue
$retstr = substr($this->data, $spos, $limitpos - $spos);
$bytesread = $limitpos - $spos;
$charsleft = $numchars - (($asciiencoding) ? $bytesread : ($bytesread / 2));
$spos = $limitpos;
while ($charsleft > 0){
$opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])$conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])if ($opcode != 0x3c) {
return -1;
}
$spos += 4;
$limitpos = $spos + $conlength;
$option = ord($this->data[$spos]);
$spos += 1;
if ($asciiencoding && ($option == 0)) {
$len = min($charsleft, $limitpos - $spos); // min($charsleft, $conlength);
$retstr .= substr($this->data, $spos, $len);
$charsleft -= $len;
$asciiencoding = true;
}elseif (!$asciiencoding && ($option != 0)){
$len = min($charsleft * 2, $limitpos - $spos); // min($charsleft, $conlength);
$retstr .= substr($this->data, $spos, $len);
$charsleft -= $len/2;
$asciiencoding = false;
}elseif (!$asciiencoding && ($option == 0)) {
// bummer - the string starts off as unicode, but after the
// continuation it is in straightforward ascii encoding
$len = min($charsleft, $limitpos - $spos); // min($charsleft, $conlength);
for ($j = 0; $j $retstr .= $this->data[$spos + $j].chr(0);
}
$charsleft -= $len;
$asciiencoding = false;
}else{
$newstr = '';
for ($j = 0; $j $newstr = $retstr[$j].chr(0);
}
$retstr = $newstr;
$len = min($charsleft * 2, $limitpos - $spos); // min($charsleft, $conlength);
$retstr .= substr($this->data, $spos, $len);
$charsleft -= $len/2;
$asciiencoding = false;
//echo izavrat\n;
}
$spos += $len;
}
}
$retstr = ($asciiencoding) ? $retstr : $this->_encodeutf16($retstr);
// echo str $i = $retstr\n;
if ($richstring){
$spos += 4 * $formattingruns;
}
// for extended strings, skip over the extended string data
if ($extendedstring) {
$spos += $extendedrunlength;
}
//if ($retstr == 'derby'){
// echo bb\n;
//}
$this->sst[]=$retstr;
}
/*$continuerecords = array();
while ($this->getnextcode() == type_continue) {
$continuerecords[] = &$this->nextrecord();
}
//echo 1 type_sst\n;
$this->sharestrings = new sstrecord($r, $continuerecords);
//print_r($this->sharestrings->strings);
*/
// echo 'sst read: '.($time_end-$time_start).\n;
break;
case spreadsheet_excel_reader_type_filepass:
return false;
break;
case spreadsheet_excel_reader_type_name:
//echo type_name\n;
break;
case spreadsheet_excel_reader_type_format:
$indexcode = ord($this->data[$pos+4]) | ord($this->data[$pos+5]) if ($version == spreadsheet_excel_reader_biff8) {
$numchars = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) if (ord($this->data[$pos+8]) == 0){
$formatstring = substr($this->data, $pos+9, $numchars);
} else {
$formatstring = substr($this->data, $pos+9, $numchars*2);
}
} else {
$numchars = ord($this->data[$pos+6]);
$formatstring = substr($this->data, $pos+7, $numchars*2);
}
$this->formatrecords[$indexcode] = $formatstring;
// echo type.format\n;
break;
case spreadsheet_excel_reader_type_xf:
//global $dateformats, $numberformats;
$indexcode = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) //echo \ntype.xf .count($this->formatrecords['xfrecords']). $indexcode ;
if (array_key_exists($indexcode, $this->dateformats)) {
//echo isdate .$dateformats[$indexcode];
$this->formatrecords['xfrecords'][] = array(
'type' => 'date',
'format' => $this->dateformats[$indexcode]
);
}elseif (array_key_exists($indexcode, $this->numberformats)) {
//echo isnumber .$this->numberformats[$indexcode];
$this->formatrecords['xfrecords'][] = array(
'type' => 'number',
'format' => $this->numberformats[$indexcode]
);
}else{
$isdate = false;
if ($indexcode > 0){
if (isset($this->formatrecords[$indexcode]))
$formatstr = $this->formatrecords[$indexcode];
//echo '.other.';
//echo \ndate-time=$formatstr=\n;
if ($formatstr)
if (preg_match(/[^hmsday\/\-:\s]/i, $formatstr) == 0) { // found day and time format
$isdate = true;
$formatstr = str_replace('mm', 'i', $formatstr);
$formatstr = str_replace('h', 'h', $formatstr);
//echo \ndate-time $formatstr \n;
}
}
if ($isdate){
$this->formatrecords['xfrecords'][] = array(
'type' => 'date',
'format' => $formatstr,
);
}else{
$this->formatrecords['xfrecords'][] = array(
'type' => 'other',
'format' => '',
'code' => $indexcode
);
}
}
//echo \n;
break;
case spreadsheet_excel_reader_type_nineteenfour:
//echo type.nineteenfour\n;
$this->nineteenfour = (ord($this->data[$pos+4]) == 1);
break;
case spreadsheet_excel_reader_type_boundsheet:
//echo type.boundsheet\n;
$rec_offset = $this->_getint4d($this->data, $pos+4);
$rec_typeflag = ord($this->data[$pos+8]);
$rec_visibilityflag = ord($this->data[$pos+9]);
$rec_length = ord($this->data[$pos+10]);
if ($version == spreadsheet_excel_reader_biff8){
$chartype = ord($this->data[$pos+11]);
if ($chartype == 0){
$rec_name = substr($this->data, $pos+12, $rec_length);
} else {
$rec_name = $this->_encodeutf16(substr($this->data, $pos+12, $rec_length*2));
}
}elseif ($version == spreadsheet_excel_reader_biff7){
$rec_name = substr($this->data, $pos+11, $rec_length);
}
$this->boundsheets[] = array('name'=>$rec_name,
'offset'=>$rec_offset);
break;
}
//echo code = .base_convert($r['code'],10,16).\n;
$pos += $length + 4;
$code = ord($this->data[$pos]) | ord($this->data[$pos+1])$length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])//$r = &$this->nextrecord();
//echo 1 code = .base_convert($r['code'],10,16).\n;
}
foreach ($this->boundsheets as $key=>$val){
$this->sn = $key;
$this->_parsesheet($val['offset']);
}
return true;
}
/**
* parse a worksheet
*
* @access private
* @param todo
* @todo fix return codes
*/
function _parsesheet($spos)
{
$cont = true;
// read bof
$code = ord($this->data[$spos]) | ord($this->data[$spos+1])$length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$version = ord($this->data[$spos + 4]) | ord($this->data[$spos + 5])$substreamtype = ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])if (($version != spreadsheet_excel_reader_biff8) && ($version != spreadsheet_excel_reader_biff7)) {
return -1;
}
if ($substreamtype != spreadsheet_excel_reader_worksheet){
return -2;
}
//echo start parse code=.base_convert($code,10,16). version=.base_convert($version,10,16). substreamtype=.base_convert($substreamtype,10,16)..\n;
$spos += $length + 4;
//var_dump($this->formatrecords);
//echo code $code $length;
while($cont) {
//echo mem= .memory_get_usage().\n;
// $r = &$this->file->nextrecord();
$lowcode = ord($this->data[$spos]);
if ($lowcode == spreadsheet_excel_reader_type_eof) break;
$code = $lowcode | ord($this->data[$spos+1])$length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$spos += 4;
$this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
$this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
//echo code=.base_convert($code,10,16). $code\n;
unset($this->rectype);
$this->multiplier = 1; // need for format with %
switch ($code) {
case spreadsheet_excel_reader_type_dimension:
//echo 'type_dimension ';
if (!isset($this->numrows)) {
if (($length == 10) || ($version == spreadsheet_excel_reader_biff7)){
$this->sheets[$this->sn]['numrows'] = ord($this->data[$spos+2]) | ord($this->data[$spos+3]) $this->sheets[$this->sn]['numcols'] = ord($this->data[$spos+6]) | ord($this->data[$spos+7]) } else {
$this->sheets[$this->sn]['numrows'] = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) $this->sheets[$this->sn]['numcols'] = ord($this->data[$spos+10]) | ord($this->data[$spos+11]) }
}
//echo 'numrows '.$this->numrows.' '.$this->numcols.\n;
break;
case spreadsheet_excel_reader_type_mergedcells:
$cellranges = ord($this->data[$spos]) | ord($this->data[$spos+1])for ($i = 0; $i $fr = ord($this->data[$spos + 8*$i + 2]) | ord($this->data[$spos + 8*$i + 3])$lr = ord($this->data[$spos + 8*$i + 4]) | ord($this->data[$spos + 8*$i + 5])$fc = ord($this->data[$spos + 8*$i + 6]) | ord($this->data[$spos + 8*$i + 7])$lc = ord($this->data[$spos + 8*$i + 8]) | ord($this->data[$spos + 8*$i + 9])//$this->sheets[$this->sn]['mergedcells'][] = array($fr + 1, $fc + 1, $lr + 1, $lc + 1);
if ($lr - $fr > 0) {
$this->sheets[$this->sn]['cellsinfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
}
if ($lc - $fc > 0) {
$this->sheets[$this->sn]['cellsinfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
}
}
//echo merged cells $cellranges $lr $fr $lc $fc\n;
break;
case spreadsheet_excel_reader_type_rk:
case spreadsheet_excel_reader_type_rk2:
//echo 'spreadsheet_excel_reader_type_rk'.\n;
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$rknum = $this->_getint4d($this->data, $spos + 6);
$numvalue = $this->_getieee754($rknum);
//echo $numvalue. ;
if ($this->isdate($spos)) {
list($string, $raw) = $this->createdate($numvalue);
}else{
$raw = $numvalue;
if (isset($this->_columnsformat[$column + 1])){
$this->curformat = $this->_columnsformat[$column + 1];
}
$string = sprintf($this->curformat, $numvalue * $this->multiplier);
//$this->addcell(rkrecord($r));
}
$this->addcell($row, $column, $string, $raw);
//echo type_rk $row $column $string $raw {$this->curformat}\n;
break;
case spreadsheet_excel_reader_type_labelsst:
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5])$index = $this->_getint4d($this->data, $spos + 6);
//var_dump($this->sst);
$this->addcell($row, $column, $this->sst[$index]);
//echo labelsst $row $column $string\n;
break;
case spreadsheet_excel_reader_type_mulrk:
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$colfirst = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$collast = ord($this->data[$spos + $length - 2]) | ord($this->data[$spos + $length - 1])$columns = $collast - $colfirst + 1;
$tmppos = $spos+4;
for ($i = 0; $i $numvalue = $this->_getieee754($this->_getint4d($this->data, $tmppos + 2));
if ($this->isdate($tmppos-4)) {
list($string, $raw) = $this->createdate($numvalue);
}else{
$raw = $numvalue;
if (isset($this->_columnsformat[$colfirst + $i + 1])){
$this->curformat = $this->_columnsformat[$colfirst + $i + 1];
}
$string = sprintf($this->curformat, $numvalue * $this->multiplier);
}
//$rec['rknumbers'][$i]['xfindex'] = ord($rec['data'][$pos]) | ord($rec['data'][$pos+1]) $tmppos += 6;
$this->addcell($row, $colfirst + $i, $string, $raw);
//echo mulrk $row .($colfirst + $i). $string\n;
}
//mulrkrecord($r);
// get the individual cell records from the multiple record
//$num = ;
break;
case spreadsheet_excel_reader_type_number:
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$tmp = unpack(ddouble, substr($this->data, $spos + 6, 8)); // it machine machine dependent
if ($this->isdate($spos)) {
list($string, $raw) = $this->createdate($tmp['double']);
// $this->addcell(daterecord($r, 1));
}else{
//$raw = $tmp[''];
if (isset($this->_columnsformat[$column + 1])){
$this->curformat = $this->_columnsformat[$column + 1];
}
$raw = $this->createnumber($spos);
$string = sprintf($this->curformat, $raw * $this->multiplier);
// $this->addcell(numberrecord($r));
}
$this->addcell($row, $column, $string, $raw);
//echo number $row $column $string\n;
break;
case spreadsheet_excel_reader_type_formula:
case spreadsheet_excel_reader_type_formula2:
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])if ((ord($this->data[$spos+6])==0) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
//string formula. result follows in a string record
//echo formula $row $column formula with a string
\n;
} elseif ((ord($this->data[$spos+6])==1) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
//boolean formula. result is in +2; 0=false,1=true
} elseif ((ord($this->data[$spos+6])==2) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
//error formula. error code is in +2;
} elseif ((ord($this->data[$spos+6])==3) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
//formula result is a null string.
} else {
// result is a number, so first 14 bytes are just like a _number record
$tmp = unpack(ddouble, substr($this->data, $spos + 6, 8)); // it machine machine dependent
if ($this->isdate($spos)) {
list($string, $raw) = $this->createdate($tmp['double']);
// $this->addcell(daterecord($r, 1));
}else{
//$raw = $tmp[''];
if (isset($this->_columnsformat[$column + 1])){
$this->curformat = $this->_columnsformat[$column + 1];
}
$raw = $this->createnumber($spos);
$string = sprintf($this->curformat, $raw * $this->multiplier);
// $this->addcell(numberrecord($r));
}
$this->addcell($row, $column, $string, $raw);
//echo number $row $column $string\n;
}
break;
case spreadsheet_excel_reader_type_boolerr:
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$string = ord($this->data[$spos+6]);
$this->addcell($row, $column, $string);
//echo 'type_boolerr '.\n;
break;
case spreadsheet_excel_reader_type_row:
case spreadsheet_excel_reader_type_dbcell:
case spreadsheet_excel_reader_type_mulblank:
break;
case spreadsheet_excel_reader_type_label:
$row = ord($this->data[$spos]) | ord($this->data[$spos+1])$column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])$this->addcell($row, $column, substr($this->data, $spos + 8, ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])// $this->addcell(labelrecord($r));
break;
case spreadsheet_excel_reader_type_eof:
$cont = false;
break;
default:
//echo ' unknown :'.base_convert($r['code'],10,16).\n;
break;
}
$spos += $length;
}
if (!isset($this->sheets[$this->sn]['numrows']))
$this->sheets[$this->sn]['numrows'] = $this->sheets[$this->sn]['maxrow'];
if (!isset($this->sheets[$this->sn]['numcols']))
$this->sheets[$this->sn]['numcols'] = $this->sheets[$this->sn]['maxcol'];
}
/**
* check whether the current record read is a date
*
* @param todo
* @return boolean true if date, false otherwise
*/
function isdate($spos)
{
//$xfindex = getint2d(, 4);
$xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) //echo 'check is date '.$xfindex.' '.$this->formatrecords['xfrecords'][$xfindex]['type'].\n;
//var_dump($this->formatrecords['xfrecords'][$xfindex]);
if ($this->formatrecords['xfrecords'][$xfindex]['type'] == 'date') {
$this->curformat = $this->formatrecords['xfrecords'][$xfindex]['format'];
$this->rectype = 'date';
return true;
} else {
if ($this->formatrecords['xfrecords'][$xfindex]['type'] == 'number') {
$this->curformat = $this->formatrecords['xfrecords'][$xfindex]['format'];
$this->rectype = 'number';
if (($xfindex == 0x9) || ($xfindex == 0xa)){
$this->multiplier = 100;
}
}else{
$this->curformat = $this->_defaultformat;
$this->rectype = 'unknown';
}
return false;
}
}
//}}}
//{{{ createdate()
/**
* convert the raw excel date into a human readable format
*
* dates in excel are stored as number of seconds from an epoch. on
* windows, the epoch is 30/12/1899 and on mac it's 01/01/1904
*
* @access private
* @param integer the raw excel value to convert
* @return array first element is the converted date, the second element is number a unix timestamp
*/
function createdate($numvalue)
{
if ($numvalue > 1) {
$utcdays = $numvalue - ($this->nineteenfour ? spreadsheet_excel_reader_utcoffsetdays1904 : spreadsheet_excel_reader_utcoffsetdays);
$utcvalue = round(($utcdays+1) * spreadsheet_excel_reader_msinaday);
$string = date ($this->curformat, $utcvalue);
$raw = $utcvalue;
} else {
$raw = $numvalue;
$hours = floor($numvalue * 24);
$mins = floor($numvalue * 24 * 60) - $hours * 60;
$secs = floor($numvalue * spreadsheet_excel_reader_msinaday) - $hours * 60 * 60 - $mins * 60;
$string = date ($this->curformat, mktime($hours, $mins, $secs));
}
return array($string, $raw);
}
function createnumber($spos)
{
$rknumhigh = $this->_getint4d($this->data, $spos + 10);
$rknumlow = $this->_getint4d($this->data, $spos + 6);
//for ($i=0; $idata[$i+$spos+6]) . ; } echo
;
$sign = ($rknumhigh & 0x80000000) >> 31;
$exp = ($rknumhigh & 0x7ff00000) >> 20;
$mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
$mantissalow1 = ($rknumlow & 0x80000000) >> 31;
$mantissalow2 = ($rknumlow & 0x7fffffff);
$value = $mantissa / pow( 2 , (20- ($exp - 1023)));
if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023)));
$value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
//echo sign = $sign, exp = $exp, mantissahighx = $mantissa, mantissalow1 = $mantissalow1, mantissalow2 = $mantissalow2
\n;
if ($sign) {$value = -1 * $value;}
return $value;
}
function addcell($row, $col, $string, $raw = '')
{
//echo add cel $row-$col $string\n;
$this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset);
$this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset);
$this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string;
if ($raw)
$this->sheets[$this->sn]['cellsinfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw;
if (isset($this->rectype))
$this->sheets[$this->sn]['cellsinfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['type'] = $this->rectype;
}
function _getieee754($rknum)
{
if (($rknum & 0x02) != 0) {
$value = $rknum >> 2;
} else {
//mmp
// first comment out the previously existing 7 lines of code here
// $tmp = unpack(d, pack(vv, 0, ($rknum & 0xfffffffc)));
// //$value = $tmp[''];
// if (array_key_exists(1, $tmp)) {
// $value = $tmp[1];
// } else {
// $value = $tmp[''];
// }
// i got my info on ieee754 encoding from
// http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
// the rk format calls for using only the most significant 30 bits of the
// 64 bit floating point value. the other 34 bits are assumed to be 0
// so, we use the upper 30 bits of $rknum as follows
$sign = ($rknum & 0x80000000) >> 31;
$exp = ($rknum & 0x7ff00000) >> 20;
$mantissa = (0x100000 | ($rknum & 0x000ffffc));
$value = $mantissa / pow( 2 , (20- ($exp - 1023)));
if ($sign) {$value = -1 * $value;}
//end of changes by mmp
}
if (($rknum & 0x01) != 0) {
$value /= 100;
}
return $value;
}
function _encodeutf16($string)
{
$result = $string;
if ($this->_defaultencoding){
switch ($this->_encoderfunction){
case 'iconv' : $result = iconv('utf-16le', $this->_defaultencoding, $string);
break;
case 'mb_convert_encoding' : $result = mb_convert_encoding($string, $this->_defaultencoding, 'utf-16le' );
break;
}
}
return $result;
}
function _getint4d($data, $pos)
{
$value = ord($data[$pos]) | (ord($data[$pos+1]) if ($value>=4294967294)
{
$value=-2;
}
return $value;
}
}
/*
* local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* end:
*/
?>
http://www.bkjia.com/phpjc/320875.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/320875.htmltecharticle这里介绍一个直接将excel文件导入mysql的例子。我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用。 php-excelreader,下载地...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product