欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

使用 PHPExcel 导出 Excel

shiping1 的头像

使用 PHPExcel 导出 Excel

PHPExcel是一个PHP类库,功能强大,操作excel方便,尤其是可以方便地插入jpg、gif、png格式图片,支持win Excel2003和Win Excel2007。Drupal 有一个封装了这个类库的模块 PHPExcel,使用起来更加方便,但在使用时,导出过程日志提示有两个严重错误,估计是模块不完善导致的。于是就抛弃了这个模块,直接使用这个类库,记录一下:

code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//设置宽度 
$objActSheet->getColumnDimension('B')->setAutoSize(true); 
$objActSheet->getColumnDimension('A')->setWidth(30); //单位是厘米
//设置格式为PHPExcel_Style_NumberFormat::FORMAT_NUMBER,避免某些大数字 
 
//被使用科学记数方式显示,配合下面的 setAutoSize 方法可以让每一行的内容 
 
//都按原始内容全部显示出来。 
 
$objStyleA5
           ->getNumberFormat()
           ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); 
 
//************************************* 
 
//输出内容
 
 $outputFileName = "output.xls"
 
// 到文件 
 
 // $objWriter->save($outputFileName); 
 
 
 
 // 到浏览器 
 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download"); 
header('Content-Disposition:inline;filename="'.$outputFileName.'"');
header("Content-Transfer-Encoding: binary"); 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache"); 
$objWriter->save('php://output');
更多设置可以查看 PHPExcel 类库的文档 参考文章:总结 php导出Excel php

来自 http://www.cuitu.net/content/shi-yong-phpexcel-dao-chu-excel
普通分类: