php thinkphp3.2 框架的详细excel导出
程序员文章站
2022-06-25 12:14:06
...
只需要这两个放到Org下的Util里面,把PHPExcel.php改成PHPExcel.class.php,然后在前段写一个可以传表名到后端的标签 a也行 input也行;
//导出
public function table_export(){
$t_name = I('get.C_cate');
$obj = M("t_数据库总结构2");
$tName = $obj->where(" C_分类 = "."'".$t_name."'") ->select();
$tobj = M('t_'.$tName[0]["c_表名称"]);
$data = $tobj->select();//查出要导出的单表数据
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Writer.Excel5");
import("Org.Util.PHPExcel.IOFactory.php");
$filename = '租赁费'; //导出的文件名
$headarr = array("编号","日期","租赁费"); //文件内标题
$this->getExcel($filename,$headarr,$data);
}
public function getExcel( $filename,$headarr,$data){
//对数据进行检验
if(empty($data) || !is_array($data)){
die("数据必须是数组");
}
$date = date("Y-m-d",time());
$filename .= "_{$date}.xls";
//创建PHPExcel对象
$excelobj = new \PHPExcel();
$proobj = $excelobj ->getProperties();
//设置excel表头
$key = ord("A");
foreach($headarr as $k=>$v){
$colum = chr($key);
$excelobj -> setActiveSheetIndex(0) ->setCellValue($colum.'1',$v);
$key += 1;
}
$column = 2;
$actsheetobj = $excelobj ->getActiveSheet();
foreach($data as $key=>$rows){
$span = ord("A");
foreach($rows as $keyname=>$value){
$j = chr($span);
$actsheetobj->setCellValue($j.$column,$value);
$span++;
}
$column++;
}
$filename = iconv("utf-8","gb2312",$filename);
$excelobj -> setActiveSheetIndex(0);
ob_get_clean();
ob_start();
header('Content-Type: application/ynd.ms-excel');
header("Content-Disposition: attachment;filename=\"$filename\"");
header('Cache-Control: max-age=0');
$writerobj = \PHPExcel_IOFactory::createWriter($excelobj,'Excel5');
$writerobj->save('php://output');
exit;
}