欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

phpExcel类的使用方法分享

程序员文章站 2022-04-08 17:11:29
...
  1. include ‘PHPExcel.php';
  2. include ‘PHPExcel/Writer/Excel2007.php';
  3. //或者include ‘PHPExcel/Writer/Excel5.php'; 用于输出.xls的
  4. 创建一个excel
  5. $objPHPExcel = new PHPExcel();
  6. 保存excel—2007格式
  7. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  8. //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式
  9. $objWriter->save("xxx.xlsx");
复制代码

2,直接输出到浏览器

  1. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

  2. header("Pragma: public");
  3. header("Expires: 0″);
  4. header("Cache-Control:must-revalidate, post-check=0, pre-check=0″);
  5. header("Content-Type:application/force-download");
  6. header("Content-Type:application/vnd.ms-execl");
  7. header("Content-Type:application/octet-stream");
  8. header("Content-Type:application/download");;
  9. header('Content-Disposition:attachment;filename="resume.xls"‘);
  10. header("Content-Transfer-Encoding:binary");
  11. $objWriter->save('php://output');
  12. 3,设置excel的属性:

  13. 创建人
  14. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  15. 最后修改人
  16. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
  17. 标题
  18. $objPHPExcel->getProperties()->setTitle("office 2007 XLSX Test Document");
  19. 题目
  20. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
  21. 描述
  22. $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  23. 关键字
  24. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  25. 种类
  26. $objPHPExcel->getProperties()->setCategory("Test result file");
复制代码

4,其它

  1. 设置当前的sheet
  2. $objPHPExcel->setActiveSheetIndex(0);
  3. 设置sheet的name
  4. $objPHPExcel->getActiveSheet()->setTitle('Simple');
  5. 设置单元格的值
  6. $objPHPExcel->getActiveSheet()->setCellValue('A1′, ‘String');
  7. $objPHPExcel->getActiveSheet()->setCellValue('A2′, 12);
  8. $objPHPExcel->getActiveSheet()->setCellValue('A3′, true);
  9. $objPHPExcel->getActiveSheet()->setCellValue('C5′, ‘=SUM(C2:C4)');
  10. $objPHPExcel->getActiveSheet()->setCellValue('B8′, ‘=MIN(B2:C5)');
  11. 合并单元格
  12. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22′);
  13. 分离单元格
  14. $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28′);
  15. 保护cell
  16. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
  17. $objPHPExcel->getActiveSheet()->protectCells('A3:E13′, ‘PHPExcel');
  18. 设置格式
  19. // Set cell number formats
  20. echo date('H:i:s') . " Set cell number formatsn";
  21. $objPHPExcel->getActiveSheet()->getStyle('E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  22. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4′), ‘E5:E13′ );
  23. 设置宽width
  24. // Set column widths
  25. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  26. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  27. 设置font
  28. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setName('Candara');
  29. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setSize(20);
  30. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setBold(true);
  31. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  32. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  33. $objPHPExcel->getActiveSheet()->getStyle('E1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  34. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getFont()->setBold(true);
  35. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getFont()->setBold(true);
  36. 设置align
  37. $objPHPExcel->getActiveSheet()->getStyle('D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  38. $objPHPExcel->getActiveSheet()->getStyle('D12′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  39. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  40. $objPHPExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  41. //垂直居中
  42. $objPHPExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  43. 设置column的border
  44. $objPHPExcel->getActiveSheet()->getStyle('A4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  45. $objPHPExcel->getActiveSheet()->getStyle('B4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  46. $objPHPExcel->getActiveSheet()->getStyle('C4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  47. $objPHPExcel->getActiveSheet()->getStyle('D4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  48. $objPHPExcel->getActiveSheet()->getStyle('E4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  49. 设置border的color
  50. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getLeft()->getColor()->setARGB('FF993300′);
  51. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getTop()->getColor()->setARGB('FF993300′);
  52. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getBottom()->getColor()->setARGB('FF993300′);
  53. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getTop()->getColor()->setARGB('FF993300′);
  54. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getBottom()->getColor()->setARGB('FF993300′);
  55. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getRight()->getColor()->setARGB('FF993300′);
  56. 设置填充颜色
  57. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  58. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->getStartColor()->setARGB('FF808080′);
  59. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  60. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->getStartColor()->setARGB('FF808080′);
  61. 加图片 //bbs.it-home.org
  62. $objDrawing = new PHPExcel_Worksheet_Drawing();
  63. $objDrawing->setName('Logo');
  64. $objDrawing->setDescription('Logo');
  65. $objDrawing->setPath('./images/officelogo.jpg');
  66. $objDrawing->setHeight(36);
  67. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  68. $objDrawing = new PHPExcel_Worksheet_Drawing();
  69. $objDrawing->setName('Paid');
  70. $objDrawing->setDescription('Paid');
  71. $objDrawing->setPath('./images/paid.png');
  72. $objDrawing->setCoordinates('B15′);
  73. $objDrawing->setOffsetX(110);
  74. $objDrawing->setRotation(25);
  75. $objDrawing->getShadow()->setVisible(true);
  76. $objDrawing->getShadow()->setDirection(45);
  77. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  78. 在默认sheet后,创建一个worksheet
  79. echo date('H:i:s') . " Create new Worksheet objectn";
  80. $objPHPExcel->createSheet();
  81. $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
  82. $objWriter-save('php://output');
复制代码