Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97464
  • 博文数量: 28
  • 博客积分: 310
  • 博客等级: 二等列兵
  • 技术积分: 680
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-24 09:57
文章存档

2013年(10)

2012年(18)

分类: PHP

2013-02-06 12:02:44

1.首先php借助于一个类,这个类的名字是PHPEXECL,可以到相应的网站上去下载:

2.

require_once 'Classes/PHPExcel.php'; //下载下来的php的目录

function GetData($val){

    $jd = GregorianToJD(1, 1, 1970);

    $gregorian = JDToGregorian($jd+intval($val)-25569);

    return $gregorian;/**显示格式为  “月/日/年”  */

}
$filePath = './uploads/list.xlsx'; //execl文件的路径

$PHPExcel = new PHPExcel();

 

/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/

$PHPReader = new PHPExcel_Reader_Excel2007();

if(!$PHPReader->canRead($filePath)){

    $PHPReader = new PHPExcel_Reader_Excel5();

    if(!$PHPReader->canRead($filePath)){

        echo 'no Excel';

        return ;

    }

}


$PHPExcel = $PHPReader->load($filePath);

/**读取excel文件中的第一个工作表*/

$currentSheet = $PHPExcel->getSheet(0);

/**取得最大的列号*/

$allColumn = $currentSheet->getHighestColumn();
$array=array();
/**取得一共有多少行*/

$allRow = $currentSheet->getHighestRow();

$_SESSION['row']= $allRow;
/**从第二行开始输出,因为excel表中第一行为列名*/

for($currentRow =0;$currentRow <= $allRow;$currentRow++){

    /**从第A列开始输出*/

    for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){

        $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()将字符转为十进制数*/

            /**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
       $char=iconv("utf-8","gb2312",$val);
       //$char=getUnicode($val);
       // echo $char."\t";
       $array[$currentRow][$currentColumn]=$char; //把取出的execl表里的内容赋给一个数组
    }
// echo "
";

}
?>

这样你就可以操作数组里的数据,等于变相的操作execl的数据。。。。。

阅读(2209) | 评论(0) | 转发(0) |
0

上一篇:js强制设置html页面头部

下一篇:没有了

给主人留下些什么吧!~~