Home Backend Development PHP Tutorial PHPExcel read and write encapsulation class_PHP tutorial

PHPExcel read and write encapsulation class_PHP tutorial

Jul 20, 2016 am 11:15 AM
excel phpexcel Can encapsulation us data document generate kind Read and write read

For generating excel files and reading data, we can use the PHPExcel plug-in. Address: http://phpexcel.codeplex.com/releases/view/107442, choose the first one to download. Then the unzipped file directory is as follows:

Beginners should look at the examples first, the code is not difficult to understand. The names of methods are based on their names and their uses. After reading some examples, I thought of encapsulating it for convenience. Since I am just getting started. So I wrote a relatively entry-level package.

<span   1</span> <form  method="post" enctype="multipart/form-data" >
<span   2</span> 
<span   3</span> 文件:<input type="file" name="files" /></li>
<span   4</span> <input type="submit" value="提交" />
<span   5</span> </form>
<span   6</span> 
<span   7</span> <?<span php
</span><span   8</span> 
<span   9</span> <span //</span><span set_include_path(get_include_path() . PATH_SEPARATOR . 'Excel/Classes/');</span>
<span  10</span> 
<span  11</span> <span require_once</span> 'Excel/Classes/PHPExcel.php'<span ;
</span><span  12</span> 
<span  13</span> <span class</span> myExcel <span extends</span><span  PHPExcel{
</span><span  14</span>     
<span  15</span>     <span private</span> <span $file</span><span ;
</span><span  16</span>     <span private</span> <span $fileType</span><span ;
</span><span  17</span> 
<span  18</span>     <span private</span> <span $objProperty</span>; <span //</span><span 文档属性对象 </span>
<span  19</span>     <span private</span> <span $objReader</span><span ;
</span><span  20</span>     <span private</span> <span $objWriter</span><span ;
</span><span  21</span>     
<span  22</span>     <span public</span> <span function</span><span  __construct(){
</span><span  23</span>         parent::<span __construct();
</span><span  24</span> 
<span  25</span> <span     }
</span><span  26</span>     
<span  27</span>     <span /*</span> 
<span  28</span> <span      * 设置文档属性
</span><span  29</span> <span      * $property = array('title'=>'标题', 'creator' => '作者'); 
</span><span  30</span>     <span */</span>
<span  31</span>     <span public</span> <span function</span> setProperty(<span $property</span><span ){
</span><span  32</span>         <span $this</span>->objProperty = <span $this</span>-><span getProperties();
</span><span  33</span>         <span if</span>(!<span empty</span>(<span $property</span>['creator']))<span $this</span>->objProperty->setCreator(<span $property</span>['creator'<span ]);
</span><span  34</span>         <span if</span>(!<span empty</span>(<span $property</span>['title'])) <span $this</span>->objProperty->setTitle(<span $property</span>['title'<span ]);
</span><span  35</span>         <span if</span>(!<span empty</span>(<span $property</span>['subject']))<span $this</span>->objProperty->setSubject(<span $property</span>['subject'<span ]);
</span><span  36</span>         <span if</span>(!<span empty</span>(<span $property</span>['laster']))<span $this</span>->objProperty->setLastModifiedBy(<span $property</span>['laster'<span ]);
</span><span  37</span>         <span if</span>(!<span empty</span>(<span $property</span>['description']))<span $this</span>->objProperty->setDescription(<span $property</span>['description'<span ]);
</span><span  38</span>         <span if</span>(!<span empty</span>(<span $property</span>['keyword']))<span $this</span>->objProperty->setKeywords(<span $property</span>['keyword'<span ]);
</span><span  39</span>         <span if</span>(!<span empty</span>(<span $property</span>['category']))<span $this</span>->objProperty->setCategory(<span $property</span>['category'<span ]);
</span><span  40</span> <span     }
</span><span  41</span>     
<span  42</span>     <span /*</span> 
<span  43</span> <span      * 添加数据
</span><span  44</span> <span      * $data = array( 'a1'=>'a111', 'b1'=>'b222', 'c1'=>'c111',); 
</span><span  45</span> <span      * </span><span */</span>
<span  46</span>     <span public</span> <span function</span> addData(<span $data</span>, <span $index</span> = <span null</span><span ){
</span><span  47</span>         <span $objAdd</span> = (<span $index</span>)? <span $this</span>->setActiveSheetIndex(<span $index</span>) : <span $this</span>-><span getActiveSheet();
</span><span  48</span>         <span foreach</span>(<span $data</span> <span as</span> <span $key</span> => <span $val</span><span ){
</span><span  49</span>             <span if</span>(<span empty</span>(<span $key</span>) || <span empty</span>(<span $val</span>)) <span continue</span><span ;
</span><span  50</span>             <span $objAdd</span>->setCellValue(<span $key</span>, <span $val</span><span );
</span><span  51</span> <span         }
</span><span  52</span> <span     }
</span><span  53</span>     
<span  54</span>     <span //</span><span 生成文件</span>
<span  55</span>     <span public</span> <span function</span> saveFile(<span $file</span>, <span $path</span> = <span null</span>, <span $type</span> = 'Excel5'<span ){
</span><span  56</span>         <span $this</span>->objWriter = PHPExcel_IOFactory::createWriter(<span $this</span>, <span $type</span><span );
</span><span  57</span>         
<span  58</span>         <span $filePath</span> = <span $path</span>.<span $file</span><span ;
</span><span  59</span>         <span $this</span>->objWriter->save(<span $filePath</span><span );
</span><span  60</span> <span     }
</span><span  61</span>     
<span  62</span> <span /*</span><span  -------------------------------------- 读取文件 ---------------------------- </span><span */</span>
<span  63</span>     
<span  64</span>     <span //</span><span 设置读对象</span>
<span  65</span>     <span private</span> <span function</span> setReader(<span $file</span>, <span $type</span> = <span null</span><span ){
</span><span  66</span>         <span $this</span>-><span file</span> = <span $file</span><span ;
</span><span  67</span>         <span if</span>(<span $type</span><span ){
</span><span  68</span>             <span $this</span>-><span fileType</span> = <span $type</span><span ;
</span><span  69</span>             <span $this</span>->objReader = PHPExcel_IOFactory::createReader(<span $type</span>)->load(<span $file</span><span );
</span><span  70</span>         }<span else</span><span {
</span><span  71</span>             <span $this</span>-><span fileType</span> = PHPExcel_IOFactory::identify(<span $file</span><span );
</span><span  72</span>             <span $this</span>->objReader = PHPExcel_IOFactory::load(<span $file</span><span );
</span><span  73</span> <span         }
</span><span  74</span> <span     }
</span><span  75</span>     
<span  76</span>     
<span  77</span>     <span //</span><span 加载文件</span>
<span  78</span>     <span public</span> <span function</span> loadFile(<span $file</span>, <span $type</span> = <span null</span><span ){
</span><span  79</span>         <span $this</span>->setReader(<span $file</span>, <span $type</span><span );
</span><span  80</span>         <span $this</span>->sheetData = <span $this</span>->objReader->getActiveSheet()->toArray(<span null</span>,<span true</span>,<span true</span>,<span true</span><span );
</span><span  81</span> <span     }
</span><span  82</span>     
<span  83</span>     <span //</span><span 返回需要的数据</span>
<span  84</span>     <span public</span> <span function</span> dataFormat(<span $meed</span>, <span $start</span> = 1, <span $end</span> = <span null</span><span ){
</span><span  85</span>         <span foreach</span>(<span $this</span>->sheetData <span as</span> <span $line</span> => <span $row</span><span ){
</span><span  86</span>             <span if</span>(<span $start</span> && <span $line</span> < <span $start</span>) <span continue</span><span ;
</span><span  87</span>             <span if</span>(<span $end</span> && <span $line</span> > <span $end</span>) <span break</span><span ;
</span><span  88</span>             <span foreach</span>(<span $row</span> <span as</span> <span $key</span> => <span $val</span><span ){
</span><span  89</span>                 <span if</span>(<span array_key_exists</span>(<span $key</span>, <span $meed</span><span )){
</span><span  90</span>                     <span $data</span>[<span $line</span>][<span $meed</span>[<span $key</span>]] = <span $val</span><span ;
</span><span  91</span> <span                 }
</span><span  92</span> <span             }
</span><span  93</span> <span         }
</span><span  94</span>         <span return</span> <span array_merge</span>(<span $data</span><span );
</span><span  95</span> <span     }
</span><span  96</span>     
<span  97</span>     <span //</span><span 工作表信息</span>
<span  98</span>     <span public</span> <span function</span> sheetInfo(<span $file</span> = <span null</span><span ){
</span><span  99</span>         (<span $file</span>)? <span null</span> : <span $file</span> = <span $this</span>-><span file</span><span ;
</span><span 100</span>         <span $info</span> = <span $this</span>->objReader->listWorksheetInfo(<span $file</span><span );
</span><span 101</span>         <span return</span> <span $info</span><span ;
</span><span 102</span> <span     }
</span><span 103</span>     
<span 104</span> <span }
</span><span 105</span> 
<span 106</span> 
<span 107</span> <span if</span>(<span $_FILES</span><span ){
</span><span 108</span> <span $upDir</span> = './upImg/'<span ;
</span><span 109</span> <span $path</span> = <span $upDir</span>.'aaa.xls'<span ;
</span><span 110</span> <span if</span>(!<span is_dir</span>(<span $upDir</span>)) <span mkdir</span>(<span $upDir</span>, 0777, <span true</span>) or <span exit</span>('上传目录创建失败!'<span );
</span><span 111</span> <span $temp</span> = <span $_FILES</span>['files']['tmp_name'<span ];
</span><span 112</span> <span move_uploaded_file</span>(<span $temp</span>, <span $path</span>);<span //</span><span 移到指定目录</span>
<span 113</span> 
<span 114</span> <span $property</span> = <span array</span><span (
</span><span 115</span>         'title'=>'the title',
<span 116</span>         'creator' => '作者'
<span 117</span> <span );
</span><span 118</span> <span $data</span> = <span array</span><span (
</span><span 119</span>         'A1'=>'a1111',
<span 120</span>         'A2'=>'a222',
<span 121</span>         'B1'=>'b111',
<span 122</span>         'E1'=>'e111',
<span 123</span>         'f2'=>'f22222'
<span 124</span> <span );
</span><span 125</span> 
<span 126</span> <span $data2</span> = <span array</span><span (
</span><span 127</span> 
<span 128</span>         'g2'=>'f22222',
<span 129</span>         'h1'=>'hhh',
<span 130</span>         'j1'=>'jjjjj'
<span 131</span> <span );
</span><span 132</span> 
<span 133</span> <span $test</span> = <span new</span><span  myExcel();
</span><span 134</span> <span //</span><span $test->setProperty($property);
</span><span 135</span> <span //$test->addData($data, 0);
</span><span 136</span> <span //$test->addData($data2);
</span><span 137</span> <span //$test->saveFile('aaa.xls');</span>
<span 138</span> 
<span 139</span> <span $test</span>->loadFile('upImg/aaa.xls'<span );
</span><span 140</span> <span $meed</span> = <span array</span>('A'=>'name','E'=>'age','F'=>'address'<span );
</span><span 141</span> <span $data</span> = <span $test</span>->dataFormat(<span $meed</span>, <span null</span>, 5<span );
</span><span 142</span> <span var_dump</span>(<span $data</span><span );
</span><span 143</span> <span }
</span><span 144</span> ?>
Copy after login

The configuration of data return can be seen in the figure below:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440164.htmlTechArticleFor excel file generation and data reading, we can use the PHPExcel plug-in. Address: http://phpexcel.codeplex.com/releases/view/107442, choose the first one to download. Then unzip...
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)

What should I do if the frame line disappears when printing in Excel? What should I do if the frame line disappears when printing in Excel? Mar 21, 2024 am 09:50 AM

If when opening a file that needs to be printed, we will find that the table frame line has disappeared for some reason in the print preview. When encountering such a situation, we must deal with it in time. If this also appears in your print file If you have questions like this, then join the editor to learn the following course: What should I do if the frame line disappears when printing a table in Excel? 1. Open a file that needs to be printed, as shown in the figure below. 2. Select all required content areas, as shown in the figure below. 3. Right-click the mouse and select the &quot;Format Cells&quot; option, as shown in the figure below. 4. Click the “Border” option at the top of the window, as shown in the figure below. 5. Select the thin solid line pattern in the line style on the left, as shown in the figure below. 6. Select &quot;Outer Border&quot;

How to filter more than 3 keywords at the same time in excel How to filter more than 3 keywords at the same time in excel Mar 21, 2024 pm 03:16 PM

Excel is often used to process data in daily office work, and it is often necessary to use the &quot;filter&quot; function. When we choose to perform &quot;filtering&quot; in Excel, we can only filter up to two conditions for the same column. So, do you know how to filter more than 3 keywords at the same time in Excel? Next, let me demonstrate it to you. The first method is to gradually add the conditions to the filter. If you want to filter out three qualifying details at the same time, you first need to filter out one of them step by step. At the beginning, you can first filter out employees with the surname &quot;Wang&quot; based on the conditions. Then click [OK], and then check [Add current selection to filter] in the filter results. The steps are as follows. Similarly, perform filtering separately again

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box &quot;Error 0x80004005: Unspecified Error&quot; will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

How to insert excel icons into PPT slides How to insert excel icons into PPT slides Mar 26, 2024 pm 05:40 PM

1. Open the PPT and turn the page to the page where you need to insert the excel icon. Click the Insert tab. 2. Click [Object]. 3. The following dialog box will pop up. 4. Click [Create from file] and click [Browse]. 5. Select the excel table to be inserted. 6. Click OK and the following page will pop up. 7. Check [Show as icon]. 8. Click OK.

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

How to read excel data in html How to read excel data in html Mar 27, 2024 pm 05:11 PM

How to read excel data in html: 1. Use JavaScript library to read Excel data; 2. Use server-side programming language to read Excel data.

See all articles