• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHPexcel设置参数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
$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. ?>

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php+openresty部署安装发布时间:2022-07-10
下一篇:
PHP中通过preg_match_all函数获取页面信息并过滤变更为数组存储模式 ...发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap