导出excel的方法output_excel需要依赖PHPExcel
导出csv的方法csv_export不需要
<?php
/**
* @author ttt
*/
class ExcelComponent extends Component
{
public $arr= array(
0=>'A',
1=>'B',
2=>'C',
3=>'D',
4=>'E',
5=>'F',
6=>'G',
7=>'H',
8=>'I',
9=>'J',
10=>'K',
11=>'L',
12=>'M',
13=>'N',
14=>'O',
15=>'P',
16=>'Q',
17=>"R",
18=>'S',
19=>'T',
20=>'U',
21=>'V',
22=>'W',
23=>'X',
24=>'Y',
25=>'Z',
26=>'AA',
27=>'AB',
28=>'AC',
29=>'AD',
30=>'AE',
31=>'AF',
32=>'AG',
33=>'AH',
34=>'AI',
35=>'AJ',
36=>'AK',
37=>'AL',
38=>'AM',
39=>'AN',
40=>'AO',
41=>'AP',
42=>'AQ',
43=>"AR",
44=>'AS',
45=>'AT',
46=>'AU',
47=>'AV',
48=>'AW',
49=>'AX',
50=>'AY',
51=>'AZ'
); /**
* 输出excel
* @author 唐婷婷
* @param $title 标题栏
* @param $list 数据
* @param $fileName 文件名
*/
public function output_excel($title,$list,$fileName)
{
set_time_limit(0);
require_once(APP . 'Vendor' . DS . 'PHPExcel.php');
$cacheMethod =PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize'=>'128MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); // 创建一个处理对象实例
$objExcel = new PHPExcel();
// 创建文件格式写入对象实例, uncomment
//$objWriter = new PHPExcel_Writer_Excel5($objExcel); // 用于其他版本格式
//or
$objWriter = new PHPExcel_Writer_Excel2007($objExcel); // 用于 2007 格式
//$objWriter->setOffice2003Compatibility(true); //设置文档基本属性
$objProps = $objExcel->getProperties();
$objProps->setCreator("Youjuke统计后台");
$objProps->setLastModifiedBy("Youjuke统计后台");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test"); //设置当前的sheet索引,用于后续的内容操作。
//一般只有在使用多个sheet的时候才需要显示调用。
//缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
//设置当前活动sheet的名称
$objActSheet->setTitle('yjk统计后台自动导出'); $arr = $this->arr; foreach($title as $key=>$val){
$objActSheet->setCellValue($arr[$key].'1', $val); // 字符串内容
}
//设置标题栏样式
$objStyleA1 = $objActSheet ->getStyle('A1');
$objStyleA1 ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
$objFillA1 = $objStyleA1->getFill();
$objFillA1->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objFillA1->getStartColor()->setARGB('85C25D');
$objActSheet->duplicateStyle($objStyleA1, "A1:$arr[$key]1"); //设置统一格式 $i=2;
foreach($list as $key=>$val){
$j=0;
foreach($val as $v){
$gezi = $arr[$j].$i;
$objActSheet->setCellValue($gezi, $v); // 字符串内容
$j++;
}
$i++;// 下一行
}
//输出内容
$outputFileName = $fileName.'.xlsx'; //output.xls //$outputFileName = 'output.xls';
//到文件
////$objWriter->save($outputFileName);
//or
//到浏览器
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("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');
} /**
* 导出excel(csv)
* @data 导出数据
* @headlist 第一行,列名
* @fileName 输出Excel文件名
*/
function csv_export($data = array(), $headlist = array(), $fileName)
{
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
header('Cache-Control: max-age=0');
//打开PHP文件句柄,php://output 表示直接输出到浏览器
$fp = fopen('php://output', 'a');
//输出Excel列名信息
foreach ($headlist as $key => $value) {
//CSV的Excel支持GBK编码,一定要转换,否则乱码
$headlist[$key] = iconv('utf-8', 'gbk', $value);
}
//将数据通过fputcsv写到文件句柄
fputcsv($fp, $headlist);
//计数器
$num = 0;
//每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
$limit = 100000;
//逐行取出数据,不浪费内存
$count = count($data);
for ($i = 0; $i < $count; $i++) {
$num++;
//刷新一下输出buffer,防止由于数据过多造成问题
if ($limit == $num) {
ob_flush();
flush();
$num = 0;
}
$row = $data[$i];
foreach ($row as $key => $value) {
$row[$key] = iconv('utf-8', 'gbk', $value);
}
fputcsv($fp, $row);
}
} }
?>
利用js导出excel
<script src="/js/excel/xlsx.core.min.js"></script>
<script src="/js/excel/blob.js"></script>
<script src="/js/excel/FileSaver.min.js"></script>
<script src="/js/excel/dist/js/tableexport.js"></script>