陈涉世家第三段概括:总结 php导出Excel php
来源:百度文库 编辑:中财网 时间:2024/04/28 12:07:21
总结 php导出Excel php
3人收藏此文章, 收藏此文章 发表于9个月前 , 已有973次阅读 共0个评论 3人收藏此文章方法一:特点,简单,省心, 只适用windows服务器
以下为引用的内容:
01
02
header(
"Content-type:application/vnd.ms-excel"
);
03
header(
"Content-Disposition:attachment;filename=test_data.xls"
);
04
$tx
=
'表头'
;
05
echo
$tx
.
"\n\n"
;
06
//输出内容如下:
07
echo
"姓名"
.
"\t"
;
08
echo
"年龄"
.
"\t"
;
09
echo
"学历"
.
"\t"
;
10
echo
"\n"
;
11
echo
"张三"
.
"\t"
;
12
echo
"25"
.
"\t"
;
13
echo
"本科"
.
"\t"
;
14
?>
方法二: 引用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
?>