php excel类 phpExcel使用方法介绍

下载地址:http://www.codeplex.com/phpExcel
  下面是总结的几个使用方法
  include ‘phpExcel.php';
  include ‘phpExcel/Writer/Excel2007.php';
  //或者include ‘phpExcel/Writer/Excel5.php'; 用于输出.xls的
  创建一个excel
  $objphpExcel = new phpExcel();
  保存excel―2007格式
  $objWriter = new phpExcel_Writer_Excel2007($objphpExcel);
  //或者$objWriter = new phpExcel_Writer_Excel5($objphpExcel); 非2007格式
  $objWriter->save("xxx.xlsx");
  直接输出到浏览器
  $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->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");
  ――――――――――――――――――――――――――――――――――――――
  设置当前的sheet
  $objphpExcel->setActiveSheetIndex(0);
  设置sheet的name
  $objphpExcel->getActiveSheet()->setTitle('Simple');
  设置单元格的值
  $objphpExcel->getActiveSheet()->setCellValue('A1′, ‘String');
  $objphpExcel->getActiveSheet()->setCellValue('A2′, 12);
  $objphpExcel->getActiveSheet()->setCellValue('A3′, true);
  $objphpExcel->getActiveSheet()->setCellValue('C5′, ‘=SUM(C2:C4)');
  $objphpExcel->getActiveSheet()->setCellValue('B8′, ‘=MIN(B2:C5)');
  合并单元格
  $objphpExcel->getActiveSheet()->mergeCells('A18:E22′);
  分离单元格
  $objphpExcel->getActiveSheet()->unmergeCells('A28:B28′);
保护cell
  $objphpExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
  $objphpExcel->getActiveSheet()->protectCells('A3:E13′, ‘phpExcel');
  设置格式
  // Set cell number formats
  echo date('H:i:s') . " Set cell number formatsn";
  $objphpExcel->getActiveSheet()->getStyle('E4′)->getNumberFormat()->setFormatCode(phpExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  $objphpExcel->getActiveSheet()->duplicateStyle( $objphpExcel->getActiveSheet()->getStyle('E4′), ‘E5:E13′ );
  设置宽width
  // Set column widths
  $objphpExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  $objphpExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  设置font
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFont()->setName('Candara');
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFont()->setSize(20);
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFont()->setBold(true);
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFont()->setUnderline(phpExcel_Style_Font::UNDERLINE_SINGLE);
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFont()->getColor()->setARGB(phpExcel_Style_Color::COLOR_WHITE);
  $objphpExcel->getActiveSheet()->getStyle('E1′)->getFont()->getColor()->setARGB(phpExcel_Style_Color::COLOR_WHITE);
  $objphpExcel->getActiveSheet()->getStyle('D13′)->getFont()->setBold(true);
  $objphpExcel->getActiveSheet()->getStyle('E13′)->getFont()->setBold(true);
  设置align
  $objphpExcel->getActiveSheet()->getStyle('D11′)->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_RIGHT);
  $objphpExcel->getActiveSheet()->getStyle('D12′)->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_RIGHT);
  $objphpExcel->getActiveSheet()->getStyle('D13′)->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_RIGHT);
  $objphpExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setHorizontal(phpExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  //垂直居中
  $objphpExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setVertical(phpExcel_Style_Alignment::VERTICAL_CENTER);
  设置column的border
  $objphpExcel->getActiveSheet()->getStyle('A4′)->getBorders()->getTop()->setBorderStyle(phpExcel_Style_Border::BORDER_THIN);
  $objphpExcel->getActiveSheet()->getStyle('B4′)->getBorders()->getTop()->setBorderStyle(phpExcel_Style_Border::BORDER_THIN);
  $objphpExcel->getActiveSheet()->getStyle('C4′)->getBorders()->getTop()->setBorderStyle(phpExcel_Style_Border::BORDER_THIN);
  $objphpExcel->getActiveSheet()->getStyle('D4′)->getBorders()->getTop()->setBorderStyle(phpExcel_Style_Border::BORDER_THIN);
  $objphpExcel->getActiveSheet()->getStyle('E4′)->getBorders()->getTop()->setBorderStyle(phpExcel_Style_Border::BORDER_THIN);
  设置border的color
  $objphpExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getLeft()->getColor()->setARGB('FF993300′);
  $objphpExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getTop()->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()->getBottom()->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′);
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFill()->setFillType(phpExcel_Style_Fill::FILL_SOLID);
  $objphpExcel->getActiveSheet()->getStyle('B1′)->getFill()->getStartColor()->setARGB('FF808080′);
  加图片
  $objDrawing = new phpExcel_Worksheet_Drawing();
  $objDrawing->setName('Logo');
  $objDrawing->setDescription('Logo');
  $objDrawing->setPath('./images/officelogo.jpg');
  $objDrawing->setHeight(36);
  $objDrawing->setWorksheet($objphpExcel->getActiveSheet());
  $objDrawing = new phpExcel_Worksheet_Drawing();
  $objDrawing->setName('Paid');
  $objDrawing->setDescription('Paid');
  $objDrawing->setPath('./images/paid.png');
  $objDrawing->setCoordinates('B15′);
  $objDrawing->setOffsetX(110);
  $objDrawing->setRotation(25);
  $objDrawing->getShadow()->setVisible(true);
  $objDrawing->getShadow()->setDirection(45);
  $objDrawing->setWorksheet($objphpExcel->getActiveSheet());
  在默认sheet后,创建一个worksheet
  echo date('H:i:s') . " Create new Worksheet objectn";
  $objphpExcel->createSheet();
  $objWriter = phpExcel_IOFactory::createWriter($objExcel, 'Excel5');
  $objWriter-save('php://output');

php技术php excel类 phpExcel使用方法介绍,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。