php导出文件(二):使用php_excel导出excel,csv文件

上一篇记录了使用写文件的方式来导出csv文件,这一次整理一下使用php_excel插件导出excel文件,虽然插件有些笨重吃内存,但是对于小数据量还是很有用的,因为其可以支持很丰富的 excel 样式,能实现很炫酷的表格内容,这点还是让人很心动的
官方的例子还是很全面的,地址在这里https://github.com/PHPOffice/PHPExcel/tree/1.8/Examples,你想要的全都有
可以导出pdf,csv,excel,ods格式

//直接输出到浏览器

	$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
	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="resume.xls"');
	header("Content-Transfer-Encoding:binary");
	$objWriter->save('php://output');
	//创建excel方法一
    $objPHPExcel = new \PHPExcel();
    
    //创建excel方法二
    //$objPHPExcel  = new PHPExcel_Writer_Excel2007($objPHPExcel); //保存excel—2007格式

	//创建excel方法三
    //$objPHPExcel = new PHPExcel_Writer_Excel5($objPHPExcel);//保存excel—2005格式
    
    //创建excel方法四
    //$objReader = PHPExcel_IOFactory::createReader('Excel2007');
    //$objPHPExcel = $objReader->load($filepath);

	$PHPExcel->createSheet('new sheet'); //创建工作表
	$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");    //创建人
	$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");//最后修改人
	$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");//标题
	$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");//题目
	$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX,generated using PHP classes.");//描述
	$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");//关键字
	$objPHPExcel->getProperties()->setCategory("Test result file");//种类
	
	$objPHPExcel->getActiveSheet()->setTitle('Simple');//设置sheet的name
    $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//所有单元格(行)默认高度
    $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格(列)默认宽度
    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//设置行高度
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);//设置列宽度
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);//设置文字大小
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//设置是否加粗
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);// 设置文字颜色
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居左(HORIZONTAL_LEFT,默认值)中(HORIZONTAL_CENTER)右(HORIZONTAL_RIGHT)
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//设置填充颜色
    $objPHPExcel->getActiveSheet()->freezePane('A1');//冻结窗口
	$objPHPExcel->getActiveSheet()->mergeCells('A1:B1');//合并单元格,值为''
	$objPHPExcel->getActiveSheet()->unmergeCells('A1:B1')///拆分单元格,将清空合并前单元格的值并还原合并前单元格的样式

	//设置boder
	$objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

	//设置border的color
	$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
	$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
	$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
	$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300');

	//设置填充颜色
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080');
     
	$objWriter->save("xxx.xlsx");

你可能感兴趣的:(php,phpExcel,csv,excel)