• PHP excel 设置参数


    $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);

    1. <?php
    2. error_reporting(E_ALL);
    3.  
    4. require_once '../Classes/PHPExcel.php';
    5.  
    6.  
    7. $objPHPExcel = new PHPExcel();
    8.  
    9. // 设置文件属性
    10. $objPHPExcel->getProperties()->setCreator("C1G")
    11.                              ->setLastModifiedBy("C1G")
    12.                              ->setTitle("phpexcel Test Document")
    13.                              ->setSubject("phpexcel Test Document")
    14.                              ->setDescription("Test document for phpexcel, generated using PHP classes.")
    15.                              ->setKeywords("office 2007 openxml php c1gstudio")
    16.                              ->setCategory("Test");
    17.  
    18. //设置当前活动的sheet
    19. $objPHPExcel->setActiveSheetIndex(0);
    20.  
    21. //设置sheet名字
    22. $objPHPExcel->getActiveSheet()->setTitle('phpexcel demo');
    23.  
    24. //设置默认行高
    25. $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
    26.  
    27. //由PHPExcel根据传入内容自动判断单元格内容类型
    28. $objPHPExcel->getActiveSheet()->setCellValue('A1', "Firstname");
    29. $objPHPExcel->getActiveSheet()->setCellValue('B1', "Lastname");
    30. $objPHPExcel->getActiveSheet()->setCellValue('C1', "Phone");
    31. $objPHPExcel->getActiveSheet()->setCellValue('D1', "Fax");
    32. $objPHPExcel->getActiveSheet()->setCellValue('E1', "Address");
    33. $objPHPExcel->getActiveSheet()->setCellValue('F1', "ZIP");
    34. $objPHPExcel->getActiveSheet()->setCellValue('G1', "DATE");
    35.  
    36.  
    37. $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 8, 'firstname');
    38. $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 8, 'lastname');
    39.  
    40.  
    41. // utf8格式字符
    42. $objPHPExcel->setActiveSheetIndex(0)
    43.             ->setCellValue('A15', 'Miscellaneous glyphs')
    44.             ->setCellValue('A16', 'éàèùâêîôûëïüÿäöüç')
    45.             ->setCellValue('A17', 'phpexcel演示');
    46.  
    47. $objPHPExcel->getActiveSheet()->setCellValue('A9', "502");
    48. $objPHPExcel->getActiveSheet()->setCellValue('B9', "99");
    49. $objPHPExcel->getActiveSheet()->setCellValue('C9', "=SUM(A9:B9)");
    50.  
    51. //设置列宽
    52. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
    53.  
    54.  
    55. // 设置行高
    56. $objPHPExcel->getActiveSheet()->getRowDimension('9')->setRowHeight(20);
    57.  
    58. // 加粗
    59. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
    60.  
    61. // 中文
    62. $objPHPExcel->getActiveSheet()->setCellValue('A2', "小风");
    63. $objPHPExcel->getActiveSheet()->setCellValue('B2', "wang");
    64.  
    65. // 设置单元格格式
    66. $objPHPExcel->getActiveSheet()->getCell('C2')->setValueExplicit('861391327543258', PHPExcel_Cell_DataType::TYPE_NUMERIC);
    67.  
    68. // 日期
    69. $objPHPExcel->getActiveSheet()->setCellValue('G2', '2008-12-31');
    70. $objPHPExcel->getActiveSheet()->getStyle('G2')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
    71.  
    72. // 时间戳
    73. $time = gmmktime(0,0,0,12,31,2008); // int(1230681600)
    74. $objPHPExcel->getActiveSheet()->setCellValue('G3', PHPExcel_Shared_Date::PHPToExcel($time));
    75. $objPHPExcel->getActiveSheet()->getStyle('G3')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
    76.  
    77. // url
    78. $objPHPExcel->getActiveSheet()->setCellValue('G11', 'blog.c1gstudio.com');
    79. $objPHPExcel->getActiveSheet()->getCell('G11')->getHyperlink()->setUrl('http://blog.c1gstudio.com');
    80.  
    81. // 另一个sheet
    82. $objPHPExcel->getActiveSheet()->setCellValue('G12', 'sheetb');
    83. $objPHPExcel->getActiveSheet()->getCell('G12')->getHyperlink()->setUrl("sheet://'sheetb'!A1");
    84.  
    85. // 水平居上
    86. $objPHPExcel->getActiveSheet()->getStyle('A9:B9')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
    87.  
    88. // 单元格换行
    89. $objPHPExcel->getActiveSheet()->getStyle('G2:G3')->getAlignment()->setWrapText(true);
    90.  
    91. // 合并
    92. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
    93.  
    94.  
    95. // 隐藏D列
    96. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);
    97.  
    98. //
    99. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
    100. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setVisible(false);
    101. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setOutlineLevel(1);
    102. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setVisible(false);
    103. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setCollapsed(true);
    104.  
    105. // 固定第一行
    106. $objPHPExcel->getActiveSheet()->freezePane('A2');
    107.  
    108. // 保护工作表
    109. $objPHPExcel->getActiveSheet()->getProtection()->setPassword('PHPExcel');
    110. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
    111. $objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
    112. $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
    113. $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
    114.  
    115. //设置边框
    116. $sharedStyle1 = new PHPExcel_Style();
    117. $sharedStyle1->applyFromArray(
    118.     array('borders' => array(
    119.                                 'left'        => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
    120.                             )
    121.          ));
    122. $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "B1:B10");
    123.  
    124. // 创建一个新的工作表
    125. $objWorksheet1 = $objPHPExcel->createSheet();
    126. $objWorksheet1->setTitle('sheetb');
    127.  
    128.  
    129. $objPHPExcel->setActiveSheetIndex(1);
    130.  
    131.  
    132. // 创建一个图片
    133. $gdImage = @imagecreatetruecolor(200, 20) or die('Cannot Initialize new GD image stream');
    134. $textColor = imagecolorallocate($gdImage, 255, 255, 255);
    135. imagestring($gdImage, 1, 5, 5'Created with PHPExcel (c1gstudio.com)', $textColor);
    136.  
    137. // 把创建的图片添加到工作表
    138. $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
    139. $objDrawing->setName('Sample image');
    140. $objDrawing->setDescription('Sample image');
    141. $objDrawing->setImageResource($gdImage);
    142. $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
    143. $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
    144. $objDrawing->setHeight(36);
    145. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    146.  
    147. $objPHPExcel->setActiveSheetIndex(0);
    148.  
    149. // 保存
    150. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    151. $objWriter->save('testexcel'.time().'.xls');
    152.  
    153. ?>
  • 相关阅读:
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 2.安装配置OpenStack基础服务
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 1.操作系统环境配置
    ssh免密登录
    PHP实现识别带emoji表情的字符串
    算法-PHP实现八大算法
    项目总结-第一次外包项目2-项目相关
    PHP.52-TP框架商城应用实例-前台4-商品详情页-面包屑导航、AJAX浏览历史
    PHP.51-TP框架商城应用实例-前台3-楼层推荐、TP框架雪崩问题
    PHP.50-TP框架商城应用实例-前台2-商品推荐
    PHP.49-TP框架商城应用实例-前台1-公共布局、制作首页
  • 原文地址:https://www.cnblogs.com/lonnie/p/7652361.html
Copyright © 2020-2023  润新知