码迷,mamicode.com
首页 > Web开发 > 详细

PHP excel 设置参数

时间:2017-10-11 20:29:44      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:link   outline   cannot   setname   enter   require   ring   shared   lap   

$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, éàèùaê?????ü???ü?)
  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, 5Created 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. ?>

PHP excel 设置参数

标签:link   outline   cannot   setname   enter   require   ring   shared   lap   

原文地址:http://www.cnblogs.com/lonnie/p/7652361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!