用php读取excel数据~~

liccajing 2009-07-13 05:19:42
我要用php读取门禁系统导出的excel~关于签到时间的数据~~
然后拼成sql~再插入到数据库中~~

为什么用excel里的时间字段~写到sql里就会报错~~
遇到中文也会报错~~
怎么能让字符串统一~~~


谢谢拉~~
...全文
2861 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
fenyao 2009-07-14
  • 打赏
  • 举报
回复
有个phpExcelReader很好用,google一下吧
lzz0098 2009-07-14
  • 打赏
  • 举报
回复
我的那个excel_class.php类是改过的,你下来试一下,之前我也是和你出现的情况一样的
liccajing 2009-07-14
  • 打赏
  • 举报
回复
我还真就是用的excel_class.php这个~~

但就是从excel导出的时间~~拼成sql ~~mysql_query不能执行~~但是在phpadmin里直接用sql也没有问题~
lzz0098 2009-07-14
  • 打赏
  • 举报
回复
你用一下excel_class.php类导出看一下,下面是导出的代码,excel_class.php你到http://download.csdn.net/source/1487342下载,


<?
require "excel_class.php";
Read_Excel_File("Book1.xls",$return);
for ($i=0;$i<count($return[Sheet1]);$i++)
{
$login=GetGB2312String($return[Sheet1][$i][1]);
$name=GetGB2312String($return[Sheet1][$i][0]);
...
}


function GetGB2312String($name)
{
$tostr = "";
for($i=0;$i<strlen($name);$i++)
{
$curbin = ord(substr($name,$i,1));
if($curbin < 0x80)
{
$tostr .= substr($name,$i,1);
}elseif($curbin < bindec("11000000")){
$str = substr($name,$i,1);
$tostr .= "&#".ord($str).";";
}elseif($curbin < bindec("11100000")){
$str = substr($name,$i,2);
$tostr .= "&#".GetUnicodeChar($str).";";
$i += 1;
}elseif($curbin < bindec("11110000")){
$str = substr($name,$i,3);
$gstr= iconv("UTF-8","GB2312",$str);
if(!$gstr)
{
$tostr .= "&#".GetUnicodeChar($str).";";
}else{
$tostr .= $gstr;
}

$i += 2;
}elseif($curbin < bindec("11111000")){
$str = substr($name,$i,4);
$tostr .= "&#".GetUnicodeChar($str).";";

$i += 3;
}elseif($curbin < bindec("11111100")){
$str = substr($name,$i,5);
$tostr .= "&#".GetUnicodeChar($str).";";

$i += 4;
}else{
$str = substr($name,$i,6);
$tostr .= "&#".GetUnicodeChar($str).";";

$i += 5;
}
}

return $tostr;
}

function GetUnicodeChar($str)
{
$temp = "";
for($i=0;$i<strlen($str);$i++)
{
$x = decbin(ord(substr($str,$i,1)));
if($i == 0)
{
$s = strlen($str)+1;
$temp .= substr($x,$s,8-$s);
}else{
$temp .= substr($x,2,6);
}
}

return bindec($temp);
}
?>
liccajing 2009-07-14
  • 打赏
  • 举报
回复
还有谁知道啊?
zhuddan 2009-07-14
  • 打赏
  • 举报
回复
导出时间的时候字段前加个'符号,把时间完全导出 不用excel进行转换格式 这样试试看。
  • 打赏
  • 举报
回复
可能是字符集问题,sql语句在phpmyadmin可以执行,而通过mysql_query(sql)报错的话,可能是连接字符集设置有错,总共好像有四个字符集。
lzz0098 2009-07-14
  • 打赏
  • 举报
回复
有可能,估计你要导出时间要进行处理一下,还有,你导出后用echo将时间的变量打印出来看他的格式是怎样的,
yctin 2009-07-14
  • 打赏
  • 举报
回复
门禁系统一般都能导出CSV格式, 为什么不用?
liccajing 2009-07-14
  • 打赏
  • 举报
回复
不行啊~~时间格式还是不对~不执行sql~~我现在主要针对时间拼成sql~~~

是不是从excel导出的时间就不对啊~~
lzz0098 2009-07-14
  • 打赏
  • 举报
回复
http://download.csdn.net/source/1487342

我的也是下载的,但我下载后对里面的一些编码转换进行了修改了,你下后再综合我上面给的代码试一下看行不行
liccajing 2009-07-14
  • 打赏
  • 举报
回复
啊~~你不是也是下载的吗~~~
你的excel_class.php在哪的啊?
ljf_ljf 2009-07-13
  • 打赏
  • 举报
回复
理论上以PHP 调用 VBA接口应该可以完成你要求,具体没有实现过。
你可以查查 Excel 相关资料。

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧