b1gc8v 发表于 2024-10-4 16:28:52

什么是PHP,PHP spreadsheet又能干什么?


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">哈咯,下午好,又是<span style="color: black;">1星期</span>周五,这周的工作和学习怎么样呢?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">↓<span style="color: black;">↓<span style="color: black;">↓</span></span></span>是不是有人要说↓<span style="color: black;">↓<span style="color: black;">↓</span></span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/o6RTB7VwxBIo42zUQica6C2Cd5Uic7qIgALWXAH6ictn8mT3hJwchicggB3hibe4hblzibaiagoqTnd0HkkiaMp0HDzcjg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不不不,相信<span style="color: black;">大众</span>都在期待周末元旦了吧!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">什么?你<span style="color: black;">已然</span>思绪飘到远方,迫不及待了?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">别急,身为IT技术<span style="color: black;">或</span>想从事IT技术的你,先来学一下PHP吧!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">让今天充满学习能量,<span style="color: black;">而后</span>在元旦好好放松一下,工作、学习、生活三不误!<span style="color: black;">(本文纯手打,写了好几天了,代码<span style="color: black;">倘若</span>有错<span style="color: black;">能够</span>告诉我</span><span style="color: black;">)</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">今天将<span style="color: black;">重点</span>介绍PHP的<span style="color: black;">研发</span>环境、用途和<span style="color: black;">优良</span>,以及PHP Spreadsheet的用途、<span style="color: black;">运用</span>和<span style="color: black;">实质</span>操作。<strong style="color: blue;">毕竟连Facebook都在用php,你还什么理由<span style="color: black;">不消</span>?</strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/o6RTB7VwxBIo42zUQica6C2Cd5Uic7qIgAbqpic1XSibia9d627fEreV52DDL3ADib2F6eWEln5oeCjjDKic1xwicwW2iag/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.什么是<strong style="color: blue;">PHP以及PHP&nbsp;</strong>Spreadsheet</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP,全<span style="color: black;">叫作</span> Hypertext Preprocessor ,中文翻译“超文本预处理器”。<span style="color: black;">PHP内核由C<span style="color: black;">研发</span>,<span style="color: black;">因此呢</span>在语法上跟C有不少<span style="color: black;">类似</span>之处。但PHP绝对不是C。</span><span style="color: black;">PHP语法除了跟C有<span style="color: black;">类似</span>之处外,还跟Java、Perl比较流行的编程语言存在<span style="color: black;">类似</span>的<span style="color: black;">地区</span></span><span style="color: black;">。<span style="color: black;">因此呢</span>,<span style="color: black;">倘若</span>你有<span style="color: black;">以上</span>语言<span style="color: black;">基本</span>,PHP入门会更加快速。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在PHP的官网是<span style="color: black;">这般</span>介绍PHP的:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">PHP is a popular general-purpose scripting language that is especially suited to web development.</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">翻译过来,大致意思<span style="color: black;">便是</span>:</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">PHP是一种开源通用脚本语语言,并且<span style="color: black;">已然</span>被广泛应用,<span style="color: black;">尤其</span>适用于 Web <span style="color: black;">研发</span>。</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">持有</span>快速,灵活,实用的特点,PHP能做任何事,<span style="color: black;">包含</span>你的个人博客<span style="color: black;">乃至</span>是<span style="color: black;">全世界</span>性的大型站点系统。</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">PHP能干任何事!</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(1)PHP能直接嵌入HTML语言中,与HTML混编。PHP<span style="color: black;">研发</span>的目的<span style="color: black;">重点</span>是<span style="color: black;">准许</span> web <span style="color: black;">研发</span>人员快速编写动态生成的 web 页面,但 PHP 的用途远不只于此。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(2)PHP还<span style="color: black;">包括</span>了命令列执行接口(command line interface),和产生图形<span style="color: black;">运用</span>者接口(GUI)程式。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">PHP<span style="color: black;">优良</span><span style="color: black;">非常多</span>!</span></span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(1)PHP开源免费,<span style="color: black;">无</span>任何收费的项目。还<span style="color: black;">能够</span><span style="color: black;">商场</span><span style="color: black;">运用</span>(你只要改一下内核就<span style="color: black;">能够</span>商用了)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(2)PHP的<span style="color: black;">研发</span>组件非常非常非常丰富</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(3)社区<span style="color: black;">非常</span>成熟,你在社区能<span style="color: black;">处理</span>百分之85的问题,虽然还没达到<span style="color: black;">电脑关机能<span style="color: black;">处理</span>百分之99的问题,但<span style="color: black;">已然</span>很给力了。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(4)PHP学习成本非常非常低,见效快,学1个月基本能入门(<span style="color: black;">某门语言</span><span style="color: black;">要学就得三件套</span>)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(5)开源库丰富,基本所有类型均有开源<span style="color: black;">制品</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(6)PHP7.0基本不输任何一门语言,这是重点</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(7)与MySQL近乎完美的支持</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(8)找工作比较容易(这个个人有<span style="color: black;">有些</span>见解:①<span style="color: black;">没什么人学,就没什么人竞争 ②</span><span style="color: black;">由于</span>国内大厂都用了java,<span style="color: black;">因此</span><span style="color: black;">非常多</span>培训<span style="color: black;">公司</span>以此来做噱头,<span style="color: black;">引起</span>跟风的人<span style="color: black;">非常多</span>,基本全去搞java教育了&nbsp;③<span style="color: black;">亦</span>可能PHP入门很简单,没什么好割菜的,<span style="color: black;">因此</span>不教。PS:个人观点,仅供参考)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/o6RTB7VwxBIo42zUQica6C2Cd5Uic7qIgAMIt65kmCVIXQCwMuu0efBt33Zvib4kHkdIkuWiaVXM0tjRFJp7cXz91g/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而PhpSpreadsheet是一个纯PHP编写的组件库,它<span style="color: black;">运用</span>现代PHP写法,代码质量和性能比PHPExcel,高不少,完全<span style="color: black;">能够</span>替代PHPExcel(PHPExcel已<span style="color: black;">再也不</span><span style="color: black;">守护</span>)。<span style="color: black;">运用</span>PhpSpreadsheet<span style="color: black;">能够</span><span style="color: black;">容易</span>读取和写入Excel文档,支持Excel的所有操作。<span style="color: black;">因此</span>今天先挑这个来讲。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.PhpSpreadsheet特性</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">(1)</span>支持读取.xls,.xlsx,.html,.csv等格式文件,支持写入导出.xls,.xlsx,.html,.csv,.pdf格式文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">(</span><span style="color: black;">2)</span><span style="color: black;">供给</span>丰富的API,<span style="color: black;">供给</span>单元格样式设置、Excel表格属性设置、图表设置等等<span style="color: black;">许多</span>功能。<span style="color: black;">运用</span>PhpSpreadsheet完全<span style="color: black;">能够</span>生成一个外观结构都满足你的Excel表格文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(3)卓越的性能,尤其在PHP7上表现优异,比PHPExcel强大<span style="color: black;">非常多</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.安装<span style="color: black;">安排</span></span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(1)软件依赖</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要<span style="color: black;">运用</span>PhpSpreadsheet需要满足以下<span style="color: black;">要求</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">PHP5.6或更改版本,<span style="color: black;">举荐</span>PHP7</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">支持php_zip扩展</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">支持php_xml扩展</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">支持php_gd2扩展</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(2)<span style="color: black;">安排</span>composer</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Wondows平台上,<span style="color: black;">咱们</span>只需要下载Composer-Setup.exe后,一步步安装<span style="color: black;">就可</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">需要<span style="color: black;">重视</span>的是你需要开启openssl配置,<span style="color: black;">咱们</span>打开php目录下的php.ini,将extension=php_openssl.dll前面的分号去掉就<span style="color: black;">能够</span>了。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">安装成功后,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span>命令窗口(cmd)输入composer--version命令来查看<span style="color: black;">是不是</span>安装成功。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">接下来<span style="color: black;">咱们</span><span style="color: black;">能够</span>更改阿里云Composer全量镜像:</span></p><span style="color: black;"><span style="color: black;">composer&nbsp;config&nbsp;-g&nbsp;repo.packagist&nbsp;composer</span></span><span style="color: black;"><span style="color: black;">https</span>:<span style="color: black;">//mirrors.aliyun.com/composer/</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>要取消配置:</span></p><span style="color: black;">composer&nbsp;config&nbsp;-g&nbsp;--<span style="color: black;">unset</span>&nbsp;repos.packagist</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(3)安装spreadsheet</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">cmd进入自己的工作文件夹,安装Phpspreadsheet,运行命令</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行完成之后,你的文件目录就多了“vendor”文件夹以及“composer.json”、“composer.lock”文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4、<span style="color: black;">运用</span></span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">例子1.</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">试运行个例子,在工作文件根目录下创建个test.php,写入以下代码:</span></p><span style="color: black;"><span style="color: black;">&lt;?php</span></span><span style="color: black;"><span style="color: black;">//引入phpspreadsheet自动加载文件</span></span><span style="color: black;"><span style="color: black;">require</span>&nbsp;<span style="color: black;">vendor/autoload.php</span>;</span><span style="color: black;"><span style="color: black;">//引入spreadsheet和xlsx类</span></span><span style="color: black;"><span style="color: black;">use</span>&nbsp;<span style="color: black;">PhpOffice</span>\<span style="color: black;">PhpSpreadsheet</span>\<span style="color: black;">Spreadsheet</span>;</span><span style="color: black;"><span style="color: black;">use</span>&nbsp;<span style="color: black;">PhpOffice</span>\<span style="color: black;">PhpSpreadsheet</span>\<span style="color: black;">Writer</span>\<span style="color: black;">Xlsx</span>;</span><span style="color: black;">$spreadsheet&nbsp;=&nbsp;<span style="color: black;">new</span>&nbsp;Spreadsheet();<span style="color: black;">//新建个spreadsheet对象</span></span><span style="color: black;">$sheet&nbsp;=&nbsp;$spreadsheet-&gt;getActiveSheet();</span><span style="color: black;">$sheet-&gt;setCellValue(<span style="color: black;">A1</span>,&nbsp;<span style="color: black;">Welcome&nbsp;to&nbsp;Helloweba.</span>);<span style="color: black;">//设置A1表格的内容</span></span><span style="color: black;">为“Welcome&nbsp;to&nbsp;Helloweba.”</span><span style="color: black;">$writer&nbsp;=&nbsp;<span style="color: black;">new</span>Xlsx($spreadsheet);</span><span style="color: black;">$writer-&gt;save(<span style="color: black;">hello.xlsx</span>);<span style="color: black;">//<span style="color: black;">保留</span>为hello.xlsx文件</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">例子2.<span style="color: black;">运用</span>PhpSpreadsheet将Excel导入到MySQL数据库</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(1)新建个测试表格score.xlsx,内容如下:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/o6RTB7VwxBJXn1RTkrEarfSicdk9fUvBvGC579ukibv9TCgMicdNXh4fcf3YljXfqb0MZG2y4T5XcJiaaLlQcCIquA/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(2)在数据库中新建个test数据库,新建个score分数表,代码如下:</span></p><span style="color: black;"><span style="color: black;">CREATE</span>&nbsp;<span style="color: black;">TABLE</span>&nbsp;score&nbsp;(</span><span style="color: black;"><span style="color: black;">`id`</span>&nbsp;<span style="color: black;">int</span>(<span style="color: black;">11</span>)&nbsp;<span style="color: black;">unsigned</span>&nbsp;<span style="color: black;">NOT</span>&nbsp;<span style="color: black;">NULL</span>&nbsp;AUTO_INCREMENT,</span><span style="color: black;"><span style="color: black;">`name`</span>&nbsp;<span style="color: black;">varchar</span>(<span style="color: black;">32</span>)&nbsp;<span style="color: black;">NOT</span>&nbsp;<span style="color: black;">NULL</span>&nbsp;<span style="color: black;">COMMENT</span>&nbsp;<span style="color: black;">姓名</span>,</span><span style="color: black;"><span style="color: black;">`chinese`</span>&nbsp;<span style="color: black;">int</span>(<span style="color: black;">6</span>)&nbsp;<span style="color: black;">NOT</span>&nbsp;<span style="color: black;">NULL</span>&nbsp;<span style="color: black;">DEFAULT</span>&nbsp;<span style="color: black;">0</span>&nbsp;<span style="color: black;">COMMENT</span>&nbsp;<span style="color: black;">语文</span>,</span><span style="color: black;"><span style="color: black;">`maths`</span>&nbsp;<span style="color: black;">int</span>(<span style="color: black;">6</span>)&nbsp;<span style="color: black;">NOT</span>&nbsp;<span style="color: black;">NULL</span>&nbsp;<span style="color: black;">DEFAULT</span>&nbsp;<span style="color: black;">0</span>&nbsp;<span style="color: black;">COMMENT</span>&nbsp;<span style="color: black;">数学</span>,</span><span style="color: black;"><span style="color: black;">`english`</span>&nbsp;<span style="color: black;">int</span>(<span style="color: black;">6</span>)&nbsp;<span style="color: black;">NOT</span>&nbsp;<span style="color: black;">NULL</span>&nbsp;<span style="color: black;">DEFAULT</span>&nbsp;<span style="color: black;">0</span>&nbsp;<span style="color: black;">COMMENT</span>&nbsp;<span style="color: black;">外语</span>,</span><span style="color: black;">PRIMARY&nbsp;<span style="color: black;">KEY</span>&nbsp;(<span style="color: black;">`id`</span>)</span><span style="color: black;">) <span style="color: black;">ENGINE</span>=<span style="color: black;">InnoDB</span> AUTO_INCREMENT=<span style="color: black;">1</span> <span style="color: black;">DEFAULT</span> <span style="color: black;">CHARSET</span>=utf8;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(3)新建个数据库连接的设置文件conn.php,代码如下</span><span style="color: black;"><span style="color: black;">:</span></span></p><span style="color: black;"><span style="color: black;">&lt;?php</span></span><span style="color: black;">$db=mysqli_connect(<span style="color: black;">"host"</span>,<span style="color: black;">"username"</span>,<span style="color: black;">"password"</span>,<span style="color: black;">"database"</span>,port);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(4)思路:<span style="color: black;">运用</span>PhpSpreadsheet读取Excel表格中的有用信息,<span style="color: black;">而后</span>组装成sql语句,最后批量<span style="color: black;">插进</span>到MySQL表中,代码如下:</span></p><span style="color: black;"><span style="color: black;">require</span>&nbsp;<span style="color: black;">vendor/autoload.php</span>;</span><span style="color: black;"><span style="color: black;">include</span>(<span style="color: black;">conn.php</span>);&nbsp;<span style="color: black;">//连接数据库</span></span><span style="color: black;">$reader&nbsp;=&nbsp;\PhpOffice\PhpSpreadsheet\IOFactory::createReader(<span style="color: black;">Xlsx</span>);</span><span style="color: black;">$reader-&gt;setReadDataOnly(<span style="color: black;">TRUE</span>);</span><span style="color: black;">$spreadsheet&nbsp;=&nbsp;$reader-&gt;load(<span style="color: black;">score.xlsx</span>);&nbsp;<span style="color: black;">//载入excel表格</span></span><span style="color: black;">$worksheet&nbsp;=&nbsp;$spreadsheet-&gt;getActiveSheet();</span><span style="color: black;">$highestRow&nbsp;=&nbsp;$worksheet-&gt;getHighestRow();&nbsp;<span style="color: black;">//&nbsp;总行数</span></span><span style="color: black;">$highestColumn&nbsp;=&nbsp;$worksheet-&gt;getHighestColumn();<span style="color: black;">//&nbsp;总列数</span></span><span style="color: black;">$highestColumnIndex&nbsp;=</span><span style="color: black;">\PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestCol</span><span style="color: black;">umn);&nbsp;<span style="color: black;">//&nbsp;e.g.&nbsp;5</span></span><span style="color: black;">$lines&nbsp;=&nbsp;$highestRow&nbsp;-&nbsp;<span style="color: black;">2</span>;</span><span style="color: black;"><span style="color: black;">if</span>&nbsp;($lines&nbsp;&lt;=&nbsp;<span style="color: black;">0</span>)&nbsp;{</span><span style="color: black;"><span style="color: black;">exit</span>(<span style="color: black;">Excel表格中<span style="color: black;">无</span>数据</span>);</span><span style="color: black;">}</span><span style="color: black;">$sql&nbsp;=&nbsp;<span style="color: black;">"INSERT&nbsp;INTO&nbsp;score(`name`,&nbsp;`chinese`,&nbsp;`maths`,&nbsp;`english`)&nbsp;VALUES&nbsp;"</span>;</span><span style="color: black;"><span style="color: black;">for</span>&nbsp;($row&nbsp;=&nbsp;<span style="color: black;">3</span>;&nbsp;$row&nbsp;&lt;=&nbsp;$highestRow;&nbsp;++$row)&nbsp;{</span><span style="color: black;">$name&nbsp;=&nbsp;$worksheet-&gt;getCellByColumnAndRow(<span style="color: black;">1</span>,&nbsp;$row)-&gt;getValue();&nbsp;<span style="color: black;">//姓名</span></span><span style="color: black;">$chinese&nbsp;=&nbsp;$worksheet-&gt;getCellByColumnAndRow(<span style="color: black;">2</span>,&nbsp;$row)-&gt;getValue();<span style="color: black;">//语文</span></span><span style="color: black;">$maths&nbsp;=&nbsp;$worksheet-&gt;getCellByColumnAndRow(<span style="color: black;">3</span>,&nbsp;$row)-&gt;getValue();&nbsp;<span style="color: black;">//数学</span></span><span style="color: black;">$english&nbsp;=&nbsp;$worksheet-&gt;getCellByColumnAndRow(<span style="color: black;">4</span>,&nbsp;$row)-&gt;getValue();&nbsp;<span style="color: black;">//外语</span></span><span style="color: black;">$sql&nbsp;.=&nbsp;<span style="color: black;">"($name,$chinese,$maths,$english),"</span>;</span><span style="color: black;">}</span><span style="color: black;">$sql&nbsp;=&nbsp;rtrim($sql,&nbsp;<span style="color: black;">","</span>);&nbsp;<span style="color: black;">//去掉最后一个,号</span></span><span style="color: black;"><span style="color: black;">try</span>&nbsp;{</span><span style="color: black;">$db-&gt;query($sql);</span><span style="color: black;"><span style="color: black;">echo</span>&nbsp;<span style="color: black;">OK</span>;</span><span style="color: black;">}&nbsp;<span style="color: black;">catch</span>&nbsp;(<span style="color: black;">Exception</span>&nbsp;$e)&nbsp;{</span><span style="color: black;"><span style="color: black;">echo</span>&nbsp;$e-&gt;getMessage();</span><span style="color: black;">}</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">(5)<span style="color: black;">这</span><span style="color: black;">条指令</span><span style="color: black;"><span style="color: black;">能够</span>获取表格中任意单元格数据内容,$col<span style="color: black;">暗示</span>单元格所在的列,以数字<span style="color: black;">暗示</span>,A列<span style="color: black;">暗示</span><span style="color: black;">第1</span>列,$row<span style="color: black;">暗示</span>所在的行。</span></span></p><span style="color: black;">$worksheet-&gt;getCellByColumnAndRow($col,$row)-&gt;getValue()</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">例子3.PhpSpreadsheet 读取</span></strong><strong style="color: blue;"><span style="color: black;">excel文件</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码如下:</span></p><span style="color: black;"><span style="color: black;">&lt;?php</span></span><span style="color: black;">&nbsp;<span style="color: black;">require</span>&nbsp;<span style="color: black;">vendor/autoload.php</span>;</span><span style="color: black;">&nbsp;<span style="color: black;">use</span>&nbsp;\<span style="color: black;">PhpOffice</span>\<span style="color: black;">PhpSpreadsheet</span>\<span style="color: black;">IOFactory</span>;</span><span style="color: black;"> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Vcard</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">index</span><span style="color: black;">()</span></span></span><span style="color: black;">{</span><span style="color: black;">&nbsp;$file_name&nbsp;=&nbsp;<span style="color: black;">"1"</span>;</span><span style="color: black;">&nbsp;$uploadwork&nbsp;=&nbsp;<span style="color: black;">"./"</span>;</span><span style="color: black;">$uploadfile&nbsp;=&nbsp;$uploadwork&nbsp;.&nbsp;$file_name&nbsp;.<span style="color: black;">.xlsx</span>;</span><span style="color: black;">&nbsp;<span style="color: black;">//&nbsp;<span style="color: black;">按照</span>表格格式<span style="color: black;">选取</span>Xlsx、Xls、Xml、Ods、Slk、Gnumeric、Html、Csv</span></span><span style="color: black;">$reader&nbsp;=&nbsp;IOFactory::createReader(<span style="color: black;">Xlsx</span>);&nbsp;<span style="color: black;">//按文件格式,填入<span style="color: black;">区别</span>的格式字</span></span><span style="color: black;">段,<span style="color: black;">重视</span>区分<span style="color: black;">体积</span>写</span><span style="color: black;">$PHPExcel&nbsp;=&nbsp;$reader-&gt;load($uploadfile);&nbsp;<span style="color: black;">//&nbsp;载入excel文件</span></span><span style="color: black;">$sheet&nbsp;=&nbsp;$PHPExcel-&gt;getSheet(<span style="color: black;">0</span>);&nbsp;<span style="color: black;">//&nbsp;读取<span style="color: black;">第1</span>個工作表</span></span><span style="color: black;">$highestRow&nbsp;=&nbsp;$sheet-&gt;getHighestRow();&nbsp;<span style="color: black;">//&nbsp;取得总行数</span></span><span style="color: black;">$highestColumm&nbsp;=&nbsp;$sheet-&gt;getHighestColumn();<span style="color: black;">//&nbsp;取得总列数</span></span><span style="color: black;">$data&nbsp;=&nbsp;[];</span><span style="color: black;"><span style="color: black;">for</span>&nbsp;($row&nbsp;=&nbsp;<span style="color: black;">5</span>;&nbsp;$row&nbsp;&lt;=&nbsp;$highestRow;&nbsp;$row++)&nbsp;<span style="color: black;">//行号从1<span style="color: black;">起始</span></span></span><span style="color: black;">{</span><span style="color: black;"><span style="color: black;">for</span>&nbsp;($column&nbsp;=&nbsp;<span style="color: black;">A</span>;&nbsp;$column&nbsp;&lt;=&nbsp;$highestColumm;&nbsp;$column++)<span style="color: black;">//列数</span></span><span style="color: black;">是以A列<span style="color: black;">起始</span></span><span style="color: black;">{</span><span style="color: black;"><span style="color: black;">if</span>&nbsp;(<span style="color: black;">empty</span>($sheet-&gt;getCell($column&nbsp;.&nbsp;$row)-&gt;getValue())&nbsp;==</span><span style="color: black;"><span style="color: black;">false</span>)&nbsp;{</span><span style="color: black;"><span style="color: black;">if</span>&nbsp;(<span style="color: black;">empty</span>($data[$row])&nbsp;==&nbsp;<span style="color: black;">false</span>)&nbsp;{</span><span style="color: black;">$str&nbsp;=&nbsp;$sheet-&gt;getCell($column&nbsp;.&nbsp;$row)-&gt;getValue();</span><span style="color: black;"><span style="color: black;">if</span>&nbsp;(strlen($str)&nbsp;&gt;&nbsp;<span style="color: black;">11</span>)&nbsp;{</span><span style="color: black;">$pieces&nbsp;=&nbsp;explode(<span style="color: black;">"|"</span>,&nbsp;$str);</span><span style="color: black;">$data[$row][<span style="color: black;">mobile</span>]&nbsp;=&nbsp;$pieces[<span style="color: black;">0</span>];</span><span style="color: black;">}&nbsp;<span style="color: black;">else</span>&nbsp;{</span><span style="color: black;">$data[$row][<span style="color: black;">mobile</span>]&nbsp;=&nbsp;$str;</span><span style="color: black;">}</span><span style="color: black;">}&nbsp;<span style="color: black;">else</span>&nbsp;{</span><span style="color: black;">$data[$row][<span style="color: black;">name</span>]&nbsp;=&nbsp;$sheet-&gt;getCell($column&nbsp;.</span><span style="color: black;">$row)-&gt;getValue();</span><span style="color: black;">}</span><span style="color: black;">}</span><span style="color: black;">}</span><span style="color: black;">}</span><span style="color: black;"><span style="color: black;">//&nbsp;dump($data);</span></span><span style="color: black;">$myfile&nbsp;=&nbsp;fopen($uploadwork&nbsp;.&nbsp;$file_name&nbsp;.&nbsp;<span style="color: black;">".vcf"</span>,&nbsp;<span style="color: black;">"w"</span>)&nbsp;<span style="color: black;">or</span></span><span style="color: black;"><span style="color: black;">die</span>(<span style="color: black;">"Unable&nbsp;to&nbsp;open&nbsp;file!"</span>);</span><span style="color: black;"><span style="color: black;">foreach</span>&nbsp;($data&nbsp;as&nbsp;$key&nbsp;=&gt;&nbsp;$value)&nbsp;{</span><span style="color: black;">$all_str&nbsp;=&nbsp;<span style="color: black;">$this</span>-&gt;make_vcard($value[<span style="color: black;">name</span>],&nbsp;$value[<span style="color: black;">mobile</span>]);</span><span style="color: black;">fwrite($myfile,&nbsp;$all_str);</span><span style="color: black;">}</span><span style="color: black;">fclose($myfile);</span><span style="color: black;">}</span><span style="color: black;">}</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本期技术分享结束啦!<img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/Expression/Expression_64@2x.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下方投票是意见收集投票,<span style="color: black;">能够</span><span style="color: black;">帮忙</span><span style="color: black;">咱们</span>更好的改进分享内容,<span style="color: black;">期盼</span>能得到你的认可和意见,<span style="color: black;">感谢你的参与!<img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/newemoji/Party.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/Expression/Expression_78@2x.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/newemoji/2_09.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/newemoji/2_16.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/newemoji/2_15.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.3.10/assets/newemoji/Fireworks.png?tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">童鞋们记得关注<span style="color: black;">咱们</span>的招聘信息、行业咨询哦<span style="color: black;">,顺便</span><span style="color: black;">记得三连!</span>↓↓↓</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文谨<span style="color: black;">表率</span>个人观点,如有<span style="color: black;">触及</span>侵权,请联系<span style="color: black;">咱们</span>,<span style="color: black;">咱们</span>会<span style="color: black;">即时</span>删除。</p>




4zhvml8 发表于 2024-10-14 06:15:28

我完全同意你的观点,说得太对了。

qzmjef 发表于 2024-10-23 18:32:11

我深感你的理解与共鸣,愿对话长流。

nykek5i 发表于 2024-10-30 06:27:09

楼主节操掉了,还不快捡起来!

1fy07h 发表于 2024-11-8 09:00:13

你的话深深触动了我,仿佛说出了我心里的声音。
页: [1]
查看完整版本: 什么是PHP,PHP spreadsheet又能干什么?