您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

总结 php导出Excel php

2025/3/30 12:03:24发布29次查看
方法一:特点,简单,省心,  只适用windows服务器
以下为引用的内容:
01
方法二: 引用google code中推荐的小类库(大体同方法一,比较复杂点)
下载地址: http://code.google.com/p/php-excel/downloads/list
方法三: phpexcel 类库,功能强大,操作excel很方便,尤其是可以方便的加入图片,支持jpg gif png格式,支持win excel2003 ,win excel2007.
下载地址:http://www.codeplex.com/phpexcel
下面是总结的几个使用方法
001
002 /**
003  * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要
004  * 打开对应行的注释。
005  * 如果使用 excel5 ,输出的内容应该是gbk编码。
006  */
007
008 include 'phpexcel.php';
009
010 include 'phpexcel/writer/excel2007.php';
011
012 //或者include 'phpexcel/writer/excel5.php'; 用于输出.xls的
013
014 // uncomment
015 ////require_once 'phpexcel/writer/excel5.php';    // 用于其他低版本xls
016 // or
017 ////require_once 'phpexcel/writer/excel2007.php'; // 用于 excel-2007 格式
018
019 // 创建一个处理对象实例
020 $objexcel = new phpexcel();
021
022 // 创建文件格式写入对象实例, uncomment
023 ////$objwriter = new phpexcel_writer_excel5($objexcel);    // 用于其他版本格式
024 // or
025 ////$objwriter = new phpexcel_writer_excel2007($objexcel); // 用于 2007 格式
026 //$objwriter->setoffice2003compatibility(true);
027
028 //*************************************
029 //设置文档基本属性
030 $objprops = $objexcel->getproperties();
031 $objprops->setcreator(zeal li);
032 $objprops->setlastmodifiedby(zeal li);
033 $objprops->settitle(office xls test document);
034 $objprops->setsubject(office xls test document, demo);
035 $objprops->setdescription(test document, generated by phpexcel.);
036 $objprops->setkeywords(office excel phpexcel);
037 $objprops->setcategory(test);
038
039 //*************************************
040 //设置当前的sheet索引,用于后续的内容操作。
041 //一般只有在使用多个sheet的时候才需要显示调用。
042 //缺省情况下,phpexcel会自动创建第一个sheet被设置sheetindex=0
043 $objexcel->setactivesheetindex(0);
044
045
046 $objactsheet = $objexcel->getactivesheet();
047
048 //设置当前活动sheet的名称
049 $objactsheet->settitle('测试sheet');
050
051 //*************************************
052 //设置单元格内容
053 //
054 //由phpexcel根据传入内容自动判断单元格内容类型
055 $objactsheet->setcellvalue('a1', '字符串内容');  // 字符串内容
056 $objactsheet->setcellvalue('a2', 26);            // 数值
057 $objactsheet->setcellvalue('a3', true);          // 布尔值
058 $objactsheet->setcellvalue('a4', '=sum(a2:a2)'); // 公式
059
060 //显式指定内容类型
061 $objactsheet->setcellvalueexplicit('a5', '847475847857487584',
062                                    phpexcel_cell_datatype::type_string);
063
064 //合并单元格
065 $objactsheet->mergecells('b1:c22');
066
067 //分离单元格
068 $objactsheet->unmergecells('b1:c22');
069
070 //*************************************
071 //设置单元格样式
072 //
073
074 //设置宽度
075 $objactsheet->getcolumndimension('b')->setautosize(true);
076 $objactsheet->getcolumndimension('a')->setwidth(30);
077
078 $objstylea5 = $objactsheet->getstyle('a5');
079
080 //设置单元格内容的数字格式。
081 //
082 //如果使用了 phpexcel_writer_excel5 来生成内容的话,
083 //这里需要注意,在 phpexcel_style_numberformat 类的 const 变量定义的
084 //各种自定义格式化方式中,其它类型都可以正常使用,但当setformatcode
085 //为 format_number 的时候,实际出来的效果被没有把格式设置为0。需要
086 //修改 phpexcel_writer_excel5_format 类源代码中的 getxf($style) 方法,
087 //在 if ($this->_biff_version == 0x0500) { (第363行附近)前面增加一
088 //行代码:
089 //if($ifmt === '0') $ifmt = 1;
090 //
091 //设置格式为phpexcel_style_numberformat::format_number,避免某些大数字
092 //被使用科学记数方式显示,配合下面的 setautosize 方法可以让每一行的内容
093 //都按原始内容全部显示出来。
094 $objstylea5
095     ->getnumberformat()
096     ->setformatcode(phpexcel_style_numberformat::format_number);
097
098 //设置字体
099 $objfonta5 = $objstylea5->getfont();
100 $objfonta5->setname('courier new');
101 $objfonta5->setsize(10);
102 $objfonta5->setbold(true);
103 $objfonta5->setunderline(phpexcel_style_font::underline_single);
104 $objfonta5->getcolor()->setargb('ff999999');
105
106 //设置对齐方式
107 $objaligna5 = $objstylea5->getalignment();
108 $objaligna5->sethorizontal(phpexcel_style_alignment::horizontal_right);
109 $objaligna5->setvertical(phpexcel_style_alignment::vertical_center);
110
111 //设置边框
112 $objbordera5 = $objstylea5->getborders();
113 $objbordera5->gettop()->setborderstyle(phpexcel_style_border::border_thin);
114 $objbordera5->gettop()->getcolor()->setargb('ffff0000'); // color
115 $objbordera5->getbottom()->setborderstyle(phpexcel_style_border::border_thin);
116 $objbordera5->getleft()->setborderstyle(phpexcel_style_border::border_thin);
117 $objbordera5->getright()->setborderstyle(phpexcel_style_border::border_thin);
118
119 //设置填充颜色
120 $objfilla5 = $objstylea5->getfill();
121 $objfilla5->setfilltype(phpexcel_style_fill::fill_solid);
122 $objfilla5->getstartcolor()->setargb('ffeeeeee');
123
124 //从指定的单元格复制样式信息.
125 $objactsheet->duplicatestyle($objstylea5, 'b1:c22');
126
127
128 //*************************************
129 //添加图片
130 $objdrawing = new phpexcel_worksheet_drawing();
131 $objdrawing->setname('zealimg');
132 $objdrawing->setdescription('image inserted by zeal');
133 $objdrawing->setpath('./zeali.net.logo.gif');
134 $objdrawing->setheight(36);
135 $objdrawing->setcoordinates('c23');
136 $objdrawing->setoffsetx(10);
137 $objdrawing->setrotation(15);
138 $objdrawing->getshadow()->setvisible(true);
139 $objdrawing->getshadow()->setdirection(36);
140 $objdrawing->setworksheet($objactsheet);
141
142
143 //添加一个新的worksheet
144 $objexcel->createsheet();
145 $objexcel->getsheet(1)->settitle('测试2');
146
147 //保护单元格
148 $objexcel->getsheet(1)->getprotection()->setsheet(true);
149 $objexcel->getsheet(1)->protectcells('a1:c22', 'phpexcel');
150
151
152 //*************************************
153 //输出内容
154 //
155 $outputfilename = output.xls;
156 // 到文件
157 // $objwriter->save($outputfilename);
158
159 // 到浏览器
160 header(content-type: application/force-download);
161 header(content-type: application/octet-stream);
162 header(content-type: application/download);
163 header('content-disposition:inline;filename='.$outputfilename.'');
164 header(content-transfer-encoding: binary);
165 header(expires: mon, 26 jul 1997 05:00:00 gmt);
166 header(last-modified:  . gmdate(d, d m y h:i:s) .  gmt);
167 header(cache-control: must-revalidate, post-check=0, pre-check=0);
168 header(pragma: no-cache);
169 $objwriter->save('php://output');
170
171 ?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product