php 导入导出excel 需要加载excel 类库
ps :如果导出文件打不开时,要检查一下php.ini 中
在php.ini文件中,将extension=php_zip.dll前面的分号“;”去除,然后同样在php.ini文件中,将 zlib.output_compression =
Off 改为 zlib.output_compression = On ;
public function outexecl()
{
$_POST = $_GET;
$_POST = str_replace('\\','',$_POST) ;
$result = array(
0=>array('类别0','代码0','姓名0','名称0','缴纳地0') ,
1=>array('类别1','代码1','姓名1','名称1','缴纳地1') ,
2=>array('类别2','代码2','姓名2','名称2','缴纳地2') ,
3=>array('类别3','代码3','姓名3','名称3','缴纳地3') ,
4=>array('类别4','代码4','姓名4','名称4','缴纳地4') ,
5=>array('类别5','代码5','姓名5','名称5','缴纳地5') ,
);
$header = array('职员类别','职员代码','职员姓名','部门名称','社保缴纳地',);
array_unshift($result, $header);
$excelFileName=date('Y-m-d').'-ttt'; //文件名
$sheetTitle=date('Y-m-d').'-社保计算表'; //文件内部title
self::excel_export($result, $excelFileName, $sheetTitle);
}
function excel_export($data,$excelFileName,$sheetTitle){
/*
* excel导出函数
* $data为从数据库中获取到的数据
* $excelFileName下载的excel的文件名称
* $sheetTitle第一个工作区的名称
*/
/* 包含进phpexcel文件 */
$path=APPPATH.'/libraries/';
require_once $path.'PHPExcel.php';
require_once $path.'PHPExcel/Writer/Excel2007.php';
/* 实例化类 */
$objPHPExcel = new PHPExcel();
/* 设置输出的excel文件为2007兼容格式 */
//$objWriter=new PHPExcel_Writer_Excel5($objPHPExcel);//非2007格式
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
/* 设置当前的sheet */
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet();
/* sheet标题 */
$objActSheet->setTitle($sheetTitle);
if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 9.0")||strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0")||strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 7.0")||strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0")){
$excelFileName = urlencode($excelFileName);
}
$i = 1;
foreach($data as $value)
{
/* excel文件内容 */
$j = 'A';
foreach($value as $value2)
{
$objActSheet->setCellValue($j.$i,$value2);
$j++;
}
$i++;
}
/* 生成文件 */
/* $putPutFileName = "test.xlsx";
$objWriter->save($putPutFileName); */
/* 生成到浏览器,提供下载 */
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename={$excelFileName}.xlsx");
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
}
//读excel 文件
function readexcel(){
$path=APPPATH.'/libraries/';
require_once $path.'PHPExcel.php';
require_once $path.'PHPExcel/Writer/Excel2007.php';
/* 实例化类 */
$objPHPExcel = new PHPExcel();
/**默认用excel2007读取excel,若格式不对,则用之前的版本进行读取*/
$filePath = $path.'test.xlsx';
if(file_exists($filePath)){
//echo 1;
}else{
//echo 2;
}
$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);
//var_dump($PHPExcel);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
/**从第A列开始输出*/
for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()将字符转为十进制数*/
if($currentColumn == 'A')
{
//echo GetData($val)."\t";
echo $val."\t";
}else{
//echo $val;
/**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
//echo iconv('utf-8','gb2312', $val)."\t";
echo $val;
}
}
echo "";
}
echo "\n";
}