时间:2021-07-01 10:21:17 帮助过:16人阅读
/**
* 用于生成excel文件的函数
* author:walker
* @param $data 生成excel的数据(二维数组形式)
* @param null $savefile 生成excel的文件名(保不指定,则为当前时间戳)
* @param null $title 生成excel的表头(一维数组形式)
* @param string $sheetname 生成excel的sheet名称(缺省为sheet1)
*/
function exportExcel($data,$savefile=null,$title=null,$sheetname='sheet1'){
//若没有指定文件名则为当前时间戳
if(is_null($savefile)){
$savefile=time();
}
//若指字了excel表头,则把表单追加到正文内容前面去
if(is_array($title)){
array_unshift($data,$title);
}
import('Org.Util.PHPExcel');
import('Org.Util.PHPExcel.IOFactory');
import('PHPExcel.Util.PHPExcel.Reader.Excel5');
$objPHPExcel = new \PHPExcel();
//Excel内容
foreach($data as $k => $v){
$obj=$objPHPExcel->setActiveSheetIndex(0);
$row=$k+1;//行
$nn=0;
foreach($v as $vv){
$col=chr(65+$nn);//列
$vv = iconv("UTF-8", "GB2312//IGNORE",$vv);
$obj->setCellValue($col.$row,$vv);//列,行,值
$nn++;
}
}
$objPHPExcel->getActiveSheet()->setTitle($sheetname);
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$savefile.'.xls"');
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
/**
*
* 导出Excel
*/
function expUser(){//导出Excel
$xlstitle = array('id','商品编码','模板编号','商品名称','总库存','当前库存','供应商');
$xlssql = M('erp_inventory');
$xlsData = $xlssql->getField('id,p_code,mid,pname,banlance,cur_banlance,factory');
// show_bug($xlsData);die;
$this->exportExcel($xlsData,time(),$xlstitle);
}
include 'Plugin/PHPExcel/Classes/PHPExcel.php';
// import('Org.Util.PHPExcel.IOFactory');
// import('PHPExcel.Util.PHPExcel.Reader.Excel5');