前提:要下载PHPEXCEL库文件
情形一:对于将数据写入EXCEL表中的用法
header("content-type:text/html;charset=utf-8");
require "PHPEXCEL/Classes/PHPExcel.php";
require "PHPEXCEL/Classes/PHPExcel/IOFactory.php";
require "PHPEXCEL/Classes/PHPExcel/Reader/Excel5.php";
require "PHPEXCEL/Classes/PHPExcel/Reader/Excel2007.php";
require "PHPEXCEL/Classes/PHPExcel/Reader/Excel2003XML.php";
require "PHPEXCEL/Classes/PHPExcel/Writer/Excel2007.php";
require "PHPEXCEL/Classes/PHPExcel/Writer/Excel5.php";
$phpexcel = new PHPExcel();
###### 其中包含了 ‘读’与‘写’类########
$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true); //该语句用于换行,但必须与下面配套使用 $phpexcel->getActiveSheet() ->setCellValue('A1',"Tilel \n ID"); //请注意这里的 '\n' ,这与上面的呼应使用的
$phpexcel->getActiveSheet()->setTitle($code_title); //设置比标题
$phpexcel->getActiveSheet()->getStyle('A1:L1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//设置垂直居中
$phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(2.8); //设置列的宽度
$phpexcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);////设置水平居中
$phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(8);//设置字体大小
$phpexcel->getActiveSheet() ->setCellValue('A'.$i,$row['id']);//写入数据,其中$i,是变量.如$i = 1,2,3,4,5...
$phpexcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$phpexcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$phpexcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$phpexcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//设置水平居中,居左/右
$obj_Writer = PHPExcel_IOFactory::createWriter($phpexcel,'Excel5');
$filename = $code_title.".xls";//文件名
//设置header
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$obj_Writer->save('php://output');//输出
die();//种植执行
情形二:对于将EXCEL表中数据读取的用法
第一步:引用核心库文件,上面已有描述。
$phpexcel = new PHPExcel(); //同上
$filePath = "/data/home/cn/newadmin/product/uploads/cusabio_common.xls"; //引用一个文件路径,并做判断
if(file_exists($filePath)){
。。。。。。。。。。。。。。。
}
$PHPReader = new PHPExcel_Reader_Excel5(); ### 1
// $PHPReader = new PHPExcel_Reader_Excel2007(); ### 2
### 其中 1和2 的区别,与文件的后缀名有关。如:xls或者xlsx等
// var_dump($PHPReader);
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5(); //如果不成功的时候用以前的版本来读取
if(!$PHPReader->canRead($filePath)){
echo 'no Excel';
return ;
}
}
$PHPExcel=$PHPReader->load($filePath);
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
$currentSheet=$PHPExcel->getSheet(0);
//获取总列数
$allColumn=$currentSheet->getHighestColumn();
//获取总行数
$allRow=$currentSheet->getHighestRow();
// var_dump($allColumn, $allRow);
// exit;
for($currentRow=1;$currentRow<=$allRow;$currentRow++){
//从哪列开始,A表示第一列
for($currentColumn='A';$currentColumn != 'AT';$currentColumn++){
//数据坐标
$address=$currentColumn.$currentRow;
//读取到的数据,保存到数组$arr中
$data[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue(); // var_dump($data[$currentRow][$currentColumn]);
} if($data[1]['A'] != "product_name_cn" && $data[1]['B'] != "product_name_en" && $data[1]['C'] != "product_code"){ echo "<script>alert('sorry,该文件读取信息有误。请使用系统提供的模板文件!');</script>";
echo "<script>window.history.back();</script>";
exit;
}
//判断数组原始是否为空。如果为空,跳出循环
if(empty($data[$currentRow]['A']) && empty($data[$currentRow]['B']) && empty($data[$currentRow]['C'])){
unset($data[$currentRow]);
break;
} }
最后得到的二位数组:$data,就是excel中的数据,剩下的如何调用各种方法。