php用PHPExcel库生成Excel文档的例子

<?php

	require_once '../libs/PHPWord/PHPWord.php';

	require_once '../libs/PHPWord/PHPWord/IOFactory.php';

	require_once '../../config.php';



	// New Word Document

	$PHPWord = new PHPWord();

	

	$objExcel->getProperties()->setCreator("office 2003 excel");

	$objExcel->getProperties()->setLastModifiedBy("office 2003 excel");

	$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");

	$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");

	$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");

	$objExcel->getProperties()->setKeywords("office 2003 openxml php");

	$objExcel->getProperties()->setCategory("Test result file");

	$objExcel->setActiveSheetIndex(0);

	$objActSheet = $objExcel->getActiveSheet();

	

	$data1 = array();//内容自己写

	

	$objActSheet->setTitle('aaa');//设置当前sheet

	$objActSheet->mergeCells('A1:D1');//合并A1到D1单元格

	$objActSheet->setCellValue('A1','硬件配置信息');

	$objActSheet->setCellValue('A2','序列号');

	$objActSheet->setCellValue('B2',$data1['serial_number']);

	$objActSheet->setCellValue('C2','采购订单号');

	$objActSheet->setCellValue('D2',$data1['purchase_number']);

	$objActSheet->setCellValue('A3','硬件厂家');

	$objActSheet->mergeCells('B3:D3');

	$objActSheet->setCellValue('B3',$data1['factory']);

	$objActSheet->setCellValue('A4','设备状态');

	$objActSheet->setCellValue('B4',$data1['status']);

	$objActSheet->setCellValue('C4','到货时间');

	$objActSheet->setCellValue('D4',$data1['reach_time']);

	$objActSheet->setCellValue('A5','硬件配置');

	$objActSheet->mergeCells('B5:D5');

	$objActSheet->setCellValue('B5',$data1['configuration']);

	$objActSheet->setCellValue('A6','备注');

	$objActSheet->mergeCells('B6:D6');

	$objActSheet->setCellValue('B6',$data1['remark']);

	//设置默认行高

	$objActSheet->getDefaultRowDimension()->setRowHeight(20); 

	//手动设置行高

	$objActSheet->getRowDimension('5')->setRowHeight(150); 

	$objActSheet->getRowDimension('6')->setRowHeight(100); 

	//自动换行

	$objActSheet->getStyle('B5')->getAlignment()->setWrapText(true);

	$objActSheet->getStyle('B6')->getAlignment()->setWrapText(true);

	//设置边框颜色

	$arrs = array('A','B','C','D');

	for($i=1;$i<30;$i++){

       for($j=0;$jgetStyle($arrs[$j].$i)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

           $objActSheet->getStyle($arrs[$j].$i)->getBorders()->getAllBorders()->getColor()->setARGB('FF00BBcc');

		   $objActSheet->getStyle($arrs[$j].$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

       }

   	}

	//设置列的宽度

	$objActSheet->getColumnDimension('A')->setWidth(15);

	$objActSheet->getColumnDimension('B')->setWidth(30);

	$objActSheet->getColumnDimension('C')->setWidth(15);

	$objActSheet->getColumnDimension('D')->setWidth(30);

	// 设置页方向和规模

	$objActSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);

	$objActSheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

	//生成EXCEL文档

	$excelName = 'aaa';

	header('Content-Type: application/vnd.ms-excel');

    header('Cache-Control: max-age=0');

    header( 'Content-Disposition: attachment; filename='.iconv("utf-8", "GBK", $excelName).'.xls');

    $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');

    $objWriter->save('php://output');

?>

  

你可能感兴趣的:(phpexcel)