一生吉祥 發表於 2019-10-11 11:28:00

php读取Excel文件数据

<p><span style="font-size: 14pt">首先先下载 PHPExcel</span></p>
<pre><span style="color: rgba(255, 0, 0, 1)">脚本之家下载地址:</span>https://www.jb51.net/codes/194070.html</pre>
<p><span style="font-size: 14pt">然后把文件丢到php脚本同级目录里面</span></p>
<p><span style="font-size: 18.6667px">在php文件里面写下以下代码即可</span></p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">引入类库</span>
<span style="color: rgba(0, 0, 255, 1)">include</span> "./PHPExcel-1.8/Classes/PHPExcel/IOFactory.php"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">elsx文件路径</span>
<span style="color: rgba(128, 0, 128, 1)">$inputFileName</span> = "./1111.xlsx"<span style="color: rgba(0, 0, 0, 1)">;

date_default_timezone_set(</span>'PRC'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 读取excel文件</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(128, 0, 128, 1)">$inputFileType</span> = PHPExcel_IOFactory::identify(<span style="color: rgba(128, 0, 128, 1)">$inputFileName</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(128, 0, 128, 1)">$objReader</span> = PHPExcel_IOFactory::createReader(<span style="color: rgba(128, 0, 128, 1)">$inputFileType</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(128, 0, 128, 1)">$objPHPExcel</span> = <span style="color: rgba(128, 0, 128, 1)">$objReader</span>-&gt;load(<span style="color: rgba(128, 0, 128, 1)">$inputFileName</span><span style="color: rgba(0, 0, 0, 1)">);
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span>(<span style="color: rgba(0, 0, 255, 1)">Exception</span> <span style="color: rgba(128, 0, 128, 1)">$e</span><span style="color: rgba(0, 0, 0, 1)">) {

}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧</span>
<span style="color: rgba(128, 0, 128, 1)">$sheet</span> = <span style="color: rgba(128, 0, 128, 1)">$objPHPExcel</span>-&gt;getSheet(0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$highestRow</span> = <span style="color: rgba(128, 0, 128, 1)">$sheet</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">getHighestRow();
</span><span style="color: rgba(128, 0, 128, 1)">$highestColumn</span> = <span style="color: rgba(128, 0, 128, 1)">$sheet</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">getHighestColumn();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取excel文件的数据,$row=2代表从第二行开始获取数据</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(128, 0, 128, 1)">$row</span> = 2; <span style="color: rgba(128, 0, 128, 1)">$row</span> &lt;= <span style="color: rgba(128, 0, 128, 1)">$highestRow</span>; <span style="color: rgba(128, 0, 128, 1)">$row</span>++<span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Read a row of data into an array</span>
    <span style="color: rgba(128, 0, 128, 1)">$rowData</span> = <span style="color: rgba(128, 0, 128, 1)">$sheet</span>-&gt;rangeToArray('A' . <span style="color: rgba(128, 0, 128, 1)">$row</span> . ':' . <span style="color: rgba(128, 0, 128, 1)">$highestColumn</span> . <span style="color: rgba(128, 0, 128, 1)">$row</span>, <span style="color: rgba(0, 0, 255, 1)">NULL</span>, <span style="color: rgba(0, 0, 255, 1)">TRUE</span>, <span style="color: rgba(0, 0, 255, 1)">FALSE</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里得到的rowData都是一行的数据,得到数据后自行处理,我们这里只打出来看看效果</span>
    <span style="color: rgba(0, 0, 255, 1)">echo</span> '&lt;pre&gt;'<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 128, 1)">var_dump</span>(<span style="color: rgba(128, 0, 128, 1)">$rowData</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">echo</span> "&lt;br&gt;"<span style="color: rgba(0, 0, 0, 1)">;

}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>原文路径:https://blog.csdn.net/Developersq/article/details/84852854</p>
<pre><span style="color: rgba(255, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><br><br></span></span></span></pre><br><br>
来源:https://www.cnblogs.com/junyi-bk/p/11653103.html
頁: [1]
查看完整版本: php读取Excel文件数据