Home php教程 php手册 PHPExcel常用方法汇总

PHPExcel常用方法汇总

Jun 13, 2016 am 10:44 AM
include p phpexcel set Commonly used method Summary of Class library set up

 

  1.   
  2. //设置PHPExcel类库的include path   
  3. set_include_path('.'. PATH_SEPARATOR .   
  4. 'D:\Zeal\PHP_LIBS' . PATH_SEPARATOR .   
  5. get_include_path());   
  6. /** www.2cto.com
  7. * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要  
  8. * 打开对应行的注释。  
  9. * 如果使用 Excel5 ,输出的内容应该是GBK编码。  
  10. */  
  11. require_once 'PHPExcel.php';   
  12. // uncomment   
  13. ////require_once 'PHPExcel/Writer/Excel5.php';    // 用于其他低版本xls   
  14. // or   
  15. ////require_once 'PHPExcel/Writer/Excel2007.php'; // 用于 excel-2007 格式   
  16. // 创建一个处理对象实例   
  17. $objExcel = new PHPExcel();   
  18. // 创建文件格式写入对象实例, uncomment   
  19. ////$objWriter = new PHPExcel_Writer_Excel5($objExcel);    // 用于其他版本格式   
  20. // or   
  21. ////$objWriter = new PHPExcel_Writer_Excel2007($objExcel); // 用于 2007 格式   
  22. //$objWriter->setOffice2003Compatibility(true);   
  23. //*************************************   
  24. //设置文档基本属性   
  25. $objProps = $objExcel->getProperties();   
  26. $objProps->setCreator("Zeal Li");   
  27. $objProps->setLastModifiedBy("Zeal Li");   
  28. $objProps->setTitle("Office XLS Test Document");   
  29. $objProps->setSubject("Office XLS Test Document, Demo");   
  30. $objProps->setDescription("Test document, generated by PHPExcel.");   
  31. $objProps->setKeywords("office excel PHPExcel");   
  32. $objProps->setCategory("Test");   
  33. //*************************************   
  34. //设置当前的sheet索引,用于后续的内容操作。   
  35. //一般只有在使用多个sheet的时候才需要显示调用。   
  36. //缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0   
  37. $objExcel->setActiveSheetIndex(0);   
  38. $objActSheet = $objExcel->getActiveSheet();   
  39. //设置当前活动sheet的名称   
  40. $objActSheet->setTitle('测试Sheet');   
  41. //*************************************   
  42. //设置单元格内容   
  43. //   
  44. //由PHPExcel根据传入内容自动判断单元格内容类型   
  45. $objActSheet->setCellValue('A1''字符串内容');  // 字符串内容   
  46. $objActSheet->setCellValue('A2', 26);            // 数值   
  47. $objActSheet->setCellValue('A3', true);          // 布尔值   
  48. $objActSheet->setCellValue('A4''=SUM(A2:A2)'); // 公式   
  49. //显式指定内容类型   
  50. $objActSheet->setCellValueExplicit('A5''847475847857487584',   
  51. PHPExcel_Cell_DataType::TYPE_STRING);   
  52. //合并单元格   
  53. $objActSheet->mergeCells('B1:C22');   
  54. //分离单元格   
  55. $objActSheet->unmergeCells('B1:C22');   
  56. //*************************************   
  57. //设置单元格样式   
  58. //   
  59. //设置宽度   
  60. $objActSheet->getColumnDimension('B')->setAutoSize(true);   
  61. $objActSheet->getColumnDimension('A')->setWidth(30);   
  62. $objStyleA5 = $objActSheet->getStyle('A5');   
  63. //设置单元格内容的数字格式。   
  64. //   
  65. //如果使用了 PHPExcel_Writer_Excel5 来生成内容的话,   
  66. //这里需要注意,在 PHPExcel_Style_NumberFormat 类的 const 变量定义的   
  67. //各种自定义格式化方式中,其它类型都可以正常使用,但当setFormatCode   
  68. //为 FORMAT_NUMBER 的时候,实际出来的效果被没有把格式设置为"0"。需要   
  69. //修改 PHPExcel_Writer_Excel5_Format 类源代码中的 getXf($style) 方法,   
  70. //在 if ($this->_BIFF_version == 0x0500) { (第363行附近)前面增加一   
  71. //行代码:   
  72. //if($ifmt === '0') $ifmt = 1;   
  73. //   
  74. //设置格式为PHPExcel_Style_NumberFormat::FORMAT_NUMBER,避免某些大数字   
  75. //被使用科学记数方式显示,配合下面的 setAutoSize 方法可以让每一行的内容   
  76. //都按原始内容全部显示出来。   
  77. $objStyleA5  
  78. ->getNumberFormat()   
  79. ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);   
  80. //设置字体   
  81. $objFontA5 = $objStyleA5->getFont();   
  82. $objFontA5->setName('Courier New');   
  83. $objFontA5->setSize(10);   
  84. $objFontA5->setBold(true);   
  85. $objFontA5->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);   
  86. $objFontA5->getColor()->setARGB('FF999999');   
  87. //设置对齐方式   
  88. $objAlignA5 = $objStyleA5->getAlignment();   
  89. $objAlignA5->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);   
  90. $objAlignA5->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);   
  91. //设置边框   
  92. $objBorderA5 = $objStyleA5->getBorders();   
  93. $objBorderA5->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);   
  94. $objBorderA5->getTop()->getColor()->setARGB('FFFF0000'); // color   
  95. $objBorderA5->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);   
  96. $objBorderA5->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);   
  97. $objBorderA5->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);   
  98. //设置填充颜色   
  99. $objFillA5 = $objStyleA5->getFill();   
  100. $objFillA5->setFillType(PHPExcel_Style_Fill::FILL_SOLID);   
  101. $objFillA5->getStartColor()->setARGB('FFEEEEEE');   
  102. //从指定的单元格复制样式信息.   
  103. $objActSheet->duplicateStyle($objStyleA5'B1:C22');   
  104. //*************************************   
  105. //添加图片   
  106. $objDrawing = new PHPExcel_Worksheet_Drawing();   
  107. $objDrawing->setName('ZealImg');   
  108. $objDrawing->setDescription('Image inserted by Zeal');   
  109. $objDrawing->setPath('./zeali.net.logo.gif');   
  110. $objDrawing->setHeight(36);   
  111. $objDrawing->setCoordinates('C23');   
  112. $objDrawing->setOffsetX(10);   
  113. $objDrawing->setRotation(15);   
  114. $objDrawing->getShadow()->setVisible(true);   
  115. $objDrawing->getShadow()->setDirection(36);   
  116. $objDrawing->setWorksheet($objActSheet);   
  117. //添加一个新的worksheet   
  118. $objExcel->createSheet();   
  119. $objExcel->getSheet(1)->setTitle('测试2');   
  120. //保护单元格   
  121. $objExcel->getSheet(1)->getProtection()->setSheet(true);   
  122. $objExcel->getSheet(1)->protectCells('A1:C22''PHPExcel');   
  123. //*************************************   
  124. //输出内容   
  125. //   
  126. $outputFileName = "output.xls";   
  127. //到文件   
  128. ////$objWriter->save($outputFileName);   
  129. //or   
  130. //到浏览器   
  131. ////header("Content-Type: application/force-download");   
  132. ////header("Content-Type: application/octet-stream");   
  133. ////header("Content-Type: application/download");   
  134. ////header('Content-Disposition:inline;filename="'.$outputFileName.'"');   
  135. ////header("Content-Transfer-Encoding: binary");   
  136. ////header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   
  137. ////header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");   
  138. ////header("Cache-Control: must-revalidate, post-check=0, pre-check=0");   
  139. ////header("Pragma: no-cache");   
  140. ////$objWriter->save('php://output');   
  141. ?>  
  142. from:zeroplace.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

How to set the scheduled time for publishing works on Douyin? How does it set the video duration? How to set the scheduled time for publishing works on Douyin? How does it set the video duration? Mar 27, 2024 pm 06:11 PM

Publishing works on Douyin can attract more attention and likes, but sometimes it may be difficult for us to publish works in real time. In this case, we can use Douyin's scheduled release function. Douyin’s scheduled release function allows users to automatically publish works at a scheduled time, which can better plan the release plan and increase the exposure and influence of the work. 1. How to set the scheduled time for publishing works on Douyin? To set a scheduled release time, first go to Douyin's personal homepage, find the "+" button in the upper right corner, and click to enter the release page. There is a clock icon in the lower right corner of the publishing page. Click to enter the scheduled publishing interface. In the interface, you can choose the type of work you want to publish, including short videos, long videos, and live broadcasts. Next, you need to set a time for your work to be published. TikTok provides

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately? Where is the Douyin tag set? How can it be tagged so that it can be pushed accurately? Mar 27, 2024 am 11:01 AM

As one of the most popular short video platforms in the world, Douyin allows everyone to become a creator and share every moment of life. For Douyin users, tags are a very important function. It can help users better classify and retrieve content, and also allows the platform to push appropriate content to users more accurately. So, where are the Douyin tags set? This article will explain in detail how to set up and use tags on Douyin. 1. Where is the Douyin tag set? Using tags on Douyin can help users better classify and label their works, making it easier for other users to find and follow them. The method to set the label is as follows: 1. Open the Douyin APP and log in to your account. 2. Click the "+" sign at the bottom of the screen and select the "Publish" button. 3.

How to set up scheduled publishing on Weibo_Tutorial on how to set up scheduled publishing on Weibo How to set up scheduled publishing on Weibo_Tutorial on how to set up scheduled publishing on Weibo Mar 29, 2024 pm 03:51 PM

1. Open the Weibo client, click the three little dots on the editing page, and then click Scheduled Post. 2. After clicking on scheduled posting, there will be a time option on the right side of the publishing time. Set the time, edit the article, and click on the yellow words in the lower right corner to schedule posting. 3. The mobile version of Weibo does not currently support scheduled publishing. This function can only be used on the PC client!

Do Not Disturb Mode Not Working in iPhone: Fix Do Not Disturb Mode Not Working in iPhone: Fix Apr 24, 2024 pm 04:50 PM

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

See all articles