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

------------------------------------------
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的读取类下载可用,取出的内容是"&#20840"这种编码方式,为了将其转换成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})| 视实际情况 有无分号 来确定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值