辰云博客

phpexcel使用说明4

<div class="postBody">
            <div id="cnblogs_post_body"><p>PHPExcel可是个好东东,功能强大,下面这是一个phpExcel简易中文帮助手册,列举了各种属性,以及常用的操作方法,是每一个都用实例加以说明,希望对大家有所帮助。</p>
<p><strong>引用PHPExcel</strong></p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">include</span> 'PHPExcel.php'<span style="color: #000000;">;
</span><span style="color: #0000ff;">include</span> 'PHPExcel/Writer/Excel2007.php'<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的<br></span><span style="color: #800080;">$objPHPExcel</span> = <span style="color: #0000ff;">new</span> PHPExcel(); <span style="color: #008000;">//</span><span style="color: #008000;">创建一个实例</span></pre>
</div>
<p><strong>设置excel的属性</strong></p>
<div class="cnblogs_code"><div>按 Ctrl+C 复制代码</div><textarea style="width: 836px; height: 316.8px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea><div>按 Ctrl+C 复制代码</div></div>
<p>也可用下面这种方式</p>
<div class="cnblogs_code"><div>按 Ctrl+C 复制代码</div><textarea style="width: 836px; height: 195.2px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea><div>按 Ctrl+C 复制代码</div></div>
<p>设置当前的sheet</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->setActiveSheetIndex(0);</pre>
</div>
<p>设置sheet的标题</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->setTitle('Simple');</pre>
</div>
<p><span style="line-height: 1.5;">设置单元格宽度</span></p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getColumnDimension('A')->setWidth(20);</pre>
</div>
<p>设置单元格高度</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getRowDimension(<span style="color: #800080;">$i</span>)->setRowHeight(40);</pre>
</div>
<p>合并单元格</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->mergeCells('A18:E22');</pre>
</div>
<p>拆分单元格</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->unmergeCells('A28:B28');</pre>
</div>
<p>设置保护cell,保护工作表</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getProtection()->setSheet(<span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');</pre>
</div>
<p>设置格式</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::<span style="color: #000000;">FORMAT_CURRENCY_EUR_SIMPLE);
</span><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->duplicateStyle( <span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getStyle('E4'), 'E5:E13' );</pre>
</div>
<p>设置加粗</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getStyle('B1')->getFont()->setBold(<span style="color: #0000ff;">true</span>);</pre>
</div>
<p>设置水平对齐方式(HORIZONTAL_RIGHT,HORIZONTAL_LEFT,HORIZONTAL_CENTER,HORIZONTAL_JUSTIFY)</p>
<div class="cnblogs_code">
<pre>$objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);</pre>
</div>
<p>设置垂直居中</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);</pre>
</div>
<p><span style="line-height: 1.5;">设置字号</span></p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);</pre>
</div>
<p>设置边框</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet()->getStyle('A1:I20')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN); </pre>
</div>
<p>设置边框颜色</p>
<div class="cnblogs_code"><div>按 Ctrl+C 复制代码</div><textarea style="width: 836px; height: 178.4px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea><div>按 Ctrl+C 复制代码</div></div>
<p> </p>
<p>插入图像</p>
<div class="cnblogs_code"><div>按 Ctrl+C 复制代码</div><textarea style="width: 836px; height: 316.8px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea><div>按 Ctrl+C 复制代码</div></div>
<p>设置单元格背景色</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet(0)->getStyle('A1')->getFill()->setFillType(\PHPExcel_Style_Fill::<span style="color: #000000;">FILL_SOLID);
</span><span style="color: #800080;">$objPHPExcel</span>->getActiveSheet(0)->getStyle('A1')->getFill()->getStartColor()->setARGB('FFCAE8EA');</pre>
</div>
<p>最后输入浏览器,导出Excel</p>
<div class="cnblogs_code"><div>按 Ctrl+C 复制代码</div><textarea style="width: 836px; height: 299.2px; font-family: Courier New; font-size: 12px; line-height: 1.5;"></textarea><div>按 Ctrl+C 复制代码</div></div>
<p> </p></div><div style="display: block;" id="MySignature"><strong>广告区——博主提供付费服务,网站建设、web开发、微信开发,尤其欢迎长期外包合作,联系QQ:404658186,电话:18716186715(于).</strong></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory">分类: <a href="http://www.cnblogs.com/manongxiaobing/category/672527.html">PHPExcel</a></div>
<div id="EntryTag">标签: <a href="http://www.cnblogs.com/manongxiaobing/tag/PHPExcel/">PHPExcel</a>, <a href="http://www.cnblogs.com/manongxiaobing/tag/%E6%89%8B%E5%86%8C/">手册</a>, <a href="http://www.cnblogs.com/manongxiaobing/tag/PHP%E5%AF%BC%E5%87%BAExcel/">PHP导出Excel</a></div>
<div id="blog_post_info"><div id="green_channel">
<a href="javascript:void(0);" id="green_channel_digg" onclick="DiggIt(4378492,cb_blogId,1);green_channel_success(this,'谢谢推荐!');">好文要顶</a>
<a id="green_channel_follow" onclick="c_follow();" href="javascript:void(0);">关注我</a>
<a id="green_channel_favorite" onclick="AddToWz(cb_entryId);return false;" href="javascript:void(0);">收藏该文</a><a id="green_channel_contact" href="//msg.cnblogs.com/send/%E7%A0%81%E5%86%9C%E5%B0%8F%E5%85%B5" target="_blank">联系我</a>
<a id="green_channel_weibo" href="javascript:void(0);" title="分享至新浪微博" onclick="ShareToTsina()"><img src="//common.cnblogs.com/images/icon_weibo_24.png" alt=""></a>
<a id="green_channel_wechat" href="javascript:void(0);" title="分享至微信" onclick="shareOnWechat()"><img src="//common.cnblogs.com/images/wechat.png" alt=""></a>
</div>
<div id="author_profile">
<div id="author_profile_info" class="author_profile_info">
<a href="http://home.cnblogs.com/u/manongxiaobing/" target="_blank"><img src="http://pic.cnblogs.com/face/sample_face.gif" class="author_avatar" alt=""></a>
<div id="author_profile_detail" class="author_profile_info">
<a href="http://home.cnblogs.com/u/manongxiaobing/">码农小兵</a><br>
<a href="http://home.cnblogs.com/u/manongxiaobing/followees">关注 - 0</a><br>
<a href="http://home.cnblogs.com/u/manongxiaobing/followers">粉丝 - 11</a>
</div>
</div>
<div class="clear"></div>
<div id="author_profile_honor"></div>
<div id="author_profile_follow">
    <a href="javascript:void(0);" onclick="c_follow();return false;">+加关注</a>
</div>
</div>
<div id="div_digg">                                      
    <div class="diggit" onclick="votePost(4378492,'Digg')">
        <span class="diggnum" id="digg_count">2</span>
    </div>
    <div class="buryit" onclick="votePost(4378492,'Bury')">
        <span class="burynum" id="bury_count">0</span>
    </div>
    <div class="clear"></div>  
    <div class="diggword" id="digg_tips">
    (请您对文章做出评价)
    </div>   
</div>
</div>
<div class="clear"></div>
<div id="post_next_prev"><a href="http://www.cnblogs.com/manongxiaobing/p/4378292.html" class="p_n_p_prefix">« </a> 上一篇:<a href="http://www.cnblogs.com/manongxiaobing/p/4378292.html" title="发布于2015-03-30 15:58">微信开发之——Php批量生成带参数的二维码</a><br><a href="http://www.cnblogs.com/manongxiaobing/p/4378530.html" class="p_n_p_prefix">» </a> 下一篇:<a href="http://www.cnblogs.com/manongxiaobing/p/4378530.html" title="发布于2015-03-30 16:57">关于PHPExcel导出Excel时身份证,数字会导出为科学计数的处理方法</a><br></div>
</div>
 
 
        </div>

 

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注