您的位置:首页 > 编程语言 > PHP开发

php度取excel内容 并对其unicode编码转化成gb2312

2007-01-23 16:59 597 查看
------------------------------------------


Excel 类 V1.1


By Monkey


------------------------------------------




-----函数说明


读取Excel文件


function Read_Excel_File($ExcelFile,$Result)




$ExcelFile Excel文件名


$Result 返回的结果


函数返回值 正常返回0,否则返回错误信息




返回的值数组


$result[sheet名][行][列] 的值为相应Excel Cell的值




建立Excel文件


function Create_Excel_File($ExcelFile,$Data)




$ExcelFile Excel文件名


$Data Excel表格数据


请把函数写在PHP脚本的开头




例1:


<?


require "excel_class.php";




Read_Excel_File("Book1.xls",$return);




for ($i=0;$i<count($return[Sheet1]);$i++)


{


for ($j=0;$j<count($return[Sheet1][$i]);$j++)


{


echo $return[Sheet1][$i][$j]."|";


}


echo "<br>";


}


?>




例2:


<?


require "excel_class.php";




Read_Excel_File("Book1.xls",$return);


Create_Excel_File("ddd.xls",$return[Sheet1]);


?>

excel的读取类下载可用,取出的内容是"全"这种编码方式,为了将其转换成gb2312

function u2utf82gb($c){
$str="";
if ($c < 0x80) {
$str.=$c;
} else if ($c < 0x800) {
$str.=chr(0xC0 | $c>>6);
$str.=chr(0x80 | $c & 0x3F);
} else if ($c < 0x10000) {
$str.=chr(0xE0 | $c>>12);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
} else if ($c < 0x200000) {
$str.=chr(0xF0 | $c>>18);
$str.=chr(0x80 | $c>>12 & 0x3F);
$str.=chr(0x80 | $c>>6 & 0x3F);
$str.=chr(0x80 | $c & 0x3F);
}
return iconv('UTF-8', 'GB2312', $str);
}

function get_gb_str($str){
$str = preg_replace("|&#([0-9]{1,5})|", "/".u2utf82gb(//1)./"", $str);
$str = "/$str=/"$str/";";

eval($str);
return $str;

}

通过这两个函数可以达到目的, 注意:

preg_replace("|&#([0-9]{1,5})|", "/".u2utf82gb(//1)./"", $str);
此处的|&#([0-9]{1,5})| 视实际情况 有无分号 来确定
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: