335 lines
13 KiB
PHP
335 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: linfan
|
|
* Date: 2018-11-28
|
|
* Time: 16:49
|
|
*/
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
require_once COMMPATH . 'third_party/PHPExcel.php';
|
|
|
|
class Excel extends PHPExcel
|
|
{
|
|
|
|
/*
|
|
* 导出Excel
|
|
* @param array $data 数据数组
|
|
* @param array $indexs 设置要导出的字段 array('key1' => '中文名称1', 'key2' => '中文名称2', ...)
|
|
* @param string $filename 导出文件名称
|
|
*/
|
|
public function out($data, $indexs = array(), $filename = '')
|
|
{
|
|
if ($data) {
|
|
$excel = new PHPExcel();
|
|
$sheet = $excel->getActiveSheet();
|
|
$cells = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
|
$startRow = 1;
|
|
|
|
$indexs_key = $indexs ? array_keys($indexs) : array_keys($data);
|
|
$indexs_name = $indexs ? array_values($indexs) : array_keys($data);
|
|
|
|
foreach ($data as $row) {
|
|
foreach ($indexs_key as $key => $value) {
|
|
$startRow == 1 ? $sheet->setCellValue($cells[$key] . $startRow, $indexs_name[$key]) : $sheet->setCellValue($cells[$key] . $startRow, $row[$value]);
|
|
}
|
|
|
|
$startRow++;
|
|
}
|
|
|
|
!$filename && $filename = date('YmdHis');
|
|
|
|
header("Pragma: public");
|
|
header("Expires: 0");
|
|
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
|
|
header("Content-Type:application/force-download");
|
|
header("Content-Type:application/vnd.ms-execl");
|
|
header("Content-Type:application/octet-stream");
|
|
header("Content-Type:application/download");;
|
|
header('Content-Disposition:attachment;filename=' . $filename . '.xlsx');
|
|
header("Content-Transfer-Encoding:binary");
|
|
|
|
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
|
|
$writer->save('php://output');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 导出 Excel支持多个 sheet
|
|
* @param array $content 内容
|
|
* @param string $fileName 文件名
|
|
* @throws PHPExcel_Exception
|
|
* @throws PHPExcel_Reader_Exception
|
|
* @throws PHPExcel_Writer_Exception
|
|
*/
|
|
public function exportExcel($content = [], $fileName = '')
|
|
{
|
|
if (empty($content)) die('内容不能为空!');
|
|
if (empty($fileName)) die('文件名不能为空!');
|
|
// 设置页面等待时间
|
|
set_time_limit(0);
|
|
// 不限制内存
|
|
ini_set('memory_limit', -1);
|
|
$objPHPExcel = new PHPExcel();
|
|
// 所有单元格居中
|
|
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
$i = 0;
|
|
// 外循环产生每一项 Sheet
|
|
foreach ($content as $key => $value) {
|
|
// 创建新的工作空间 sheet
|
|
if ($i > 0) $objPHPExcel->createSheet();
|
|
$objPHPExcel->setActiveSheetIndex($i);
|
|
// 给 Sheet 设置名字
|
|
$objPHPExcel->getActiveSheet()->setTitle($key);
|
|
// 内容
|
|
$j = 1;
|
|
// 外循环产生每一列
|
|
foreach ($value as $val) {
|
|
// 内循环产生每一行
|
|
foreach ($val as $k => $v) {
|
|
// 数字列转换为字母列 如:27变为AA
|
|
$objPHPExcel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($k) . $j, $v);
|
|
}
|
|
$j++;
|
|
}
|
|
$i++;
|
|
}
|
|
$filename = iconv('UTF-8', 'UTF-8', $fileName) . '.xls';
|
|
header('Pragma: public');
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Content-Type: application/force-download');
|
|
header('Content-Type: application/vnd.ms-execl');
|
|
header('Content-Type: application/octet-stream');
|
|
header('Content-Type: application/download');
|
|
header("Content-Disposition: attachment; filename=$filename");
|
|
header('Content-Transfer-Encoding: binary');
|
|
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
$objWriter->save('php://output');
|
|
}
|
|
|
|
/**
|
|
* cvs格式导出
|
|
* @param $data
|
|
* @param array $indexs
|
|
* @param string $filename
|
|
* @return bool
|
|
*/
|
|
public function out_csv($data, $indexs = array(), $filename = '')
|
|
{
|
|
header("Pragma: public");
|
|
header("Expires: 0");
|
|
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
|
|
header("Content-Type:application/force-download");
|
|
header("Content-Type:application/vnd.ms-execl");
|
|
header("Content-Type:application/octet-stream");
|
|
header("Content-Type:application/download");;
|
|
header('Content-Disposition:attachment;filename=' . $filename . '.csv');
|
|
header("Content-Transfer-Encoding:binary");
|
|
|
|
|
|
$fp = fopen('php://output', 'a');
|
|
|
|
$indexs_key = $indexs ? array_keys($indexs) : array_keys($data);
|
|
|
|
//每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
|
|
$limit = 100000;
|
|
$num = 0;
|
|
foreach ($data as $k => $v) {
|
|
$num++;
|
|
//刷新一下输出buffer,防止由于数据过多造成问题
|
|
if ($limit == $num) {
|
|
ob_flush();
|
|
flush();
|
|
$num = 0;
|
|
}
|
|
|
|
$row = array();
|
|
foreach ($indexs_key as $col) {
|
|
if (in_array($col, array('sid', 'refund_no', 'id_card')) && is_numeric($v[$col])) {
|
|
$row[] = iconv('utf-8', 'GBK//IGNORE', "=\"{$v[$col]}\"");
|
|
} else {
|
|
$row[] = iconv('utf-8', 'GBK//IGNORE', $v[$col]);
|
|
}
|
|
}
|
|
//将数据通过fputcsv写到文件句柄
|
|
fputcsv($fp, $row);
|
|
}
|
|
|
|
return fclose($fp);
|
|
}
|
|
|
|
/**
|
|
* @param $data "数据数组"
|
|
* @param $indexs "数据字段对应的列"
|
|
* @param $file "文件路径"
|
|
* @param $mode "a追加"
|
|
* @return bool
|
|
*/
|
|
public function save($data, $indexs, $file, $mode = '')
|
|
{
|
|
$dir = substr($file, 0, strrpos($file, '/'));
|
|
if (!file_exists($dir) && !mkdir($dir, 0777, true)) {//创建文件夹失败
|
|
return false;
|
|
}
|
|
if ('a' == $mode && file_exists($file)) {
|
|
$excel = PHPExcel_IOFactory::load($file);
|
|
$sheet = $excel->getActiveSheet();
|
|
$startRow = $sheet->getHighestRow() + 1;
|
|
} else {
|
|
$excel = new PHPExcel();
|
|
$sheet = $excel->getActiveSheet();
|
|
$startRow = 1;
|
|
}
|
|
$cells = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
|
|
|
|
foreach ($data as $row) {
|
|
foreach ($indexs as $key => $value) {
|
|
$sheet->setCellValue($cells[$key] . $startRow, $row[$value]);
|
|
}
|
|
|
|
$startRow++;
|
|
}
|
|
|
|
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
|
|
$writer->save($file);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 使用csv方式保存
|
|
* @param $data
|
|
* @param $indexs
|
|
* @param $file
|
|
* @param string $mode
|
|
* @return bool
|
|
*/
|
|
function csv($data, $indexs, $file, $mode = 'a')
|
|
{
|
|
$dir = substr($file, 0, strrpos($file, '/'));
|
|
if (!file_exists($dir) && !mkdir($dir, 0777, true)) {//创建文件夹失败
|
|
return false;
|
|
}
|
|
|
|
$fp = fopen($file, $mode);
|
|
|
|
//每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
|
|
$limit = 100000;
|
|
$num = 0;
|
|
foreach ($data as $k => $v) {
|
|
$num++;
|
|
//刷新一下输出buffer,防止由于数据过多造成问题
|
|
if ($limit == $num) {
|
|
ob_flush();
|
|
flush();
|
|
$num = 0;
|
|
}
|
|
|
|
$row = array();
|
|
foreach ($indexs as $col) {
|
|
if (in_array($col, array('sid', 'refund_no')) && is_numeric($v[$col])) {
|
|
$row[] = iconv('utf-8', 'GBK//IGNORE', "=\"{$v[$col]}\"");
|
|
} else {
|
|
$row[] = iconv('utf-8', 'GBK//IGNORE', $v[$col]);
|
|
}
|
|
}
|
|
//将数据通过fputcsv写到文件句柄
|
|
fputcsv($fp, $row);
|
|
}
|
|
|
|
return fclose($fp);
|
|
}
|
|
|
|
/**
|
|
* 导出excel,第一行支持多列表头合并
|
|
* @param $data
|
|
* @param $indexs
|
|
* @param $headers
|
|
* @param $sheet_title
|
|
* @param $filename
|
|
* @return bool
|
|
*/
|
|
public function excel2($data, $indexs, $headers=array(), $sheet_title='sheet1', $filename=''){
|
|
!$sheet_title && $sheet_title = 'sheet1';
|
|
!$filename && $filename = 'excel_' . date('YmdHis');
|
|
set_time_limit(0); // 设置页面等待时间
|
|
ini_set('memory_limit', -1); // 不限制内存
|
|
|
|
$objPHPExcel = new PHPExcel();
|
|
$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); // 单元格整体居右
|
|
$objPHPExcel->setActiveSheetIndex(0); // 选中默认的第一个sheet
|
|
$objPHPExcel->getActiveSheet()->setTitle($sheet_title); // 给 Sheet 设置名字
|
|
|
|
# 先生成表头
|
|
$i = 0;
|
|
$column_num = 0;
|
|
foreach ($indexs as $k => $v){
|
|
$columnkey = PHPExcel_Cell::stringFromColumnIndex($i);
|
|
if ($column_num || in_array($k, array_keys($headers))){
|
|
if (in_array($k, array_keys($headers)) && !$headers[$k]['is_create']){
|
|
$column_num = $headers[$k]['column_num'];
|
|
$title = $headers[$k]['title'];
|
|
$headers[$k]['is_create'] = 1;
|
|
# 第一行需合并多列的列
|
|
$columnkey2 = PHPExcel_Cell::stringFromColumnIndex($i + $column_num - 1);
|
|
$pRange = $columnkey.'1:'.$columnkey2.'1';
|
|
$pCoordinate = $columnkey.'1';
|
|
$objPHPExcel->getActiveSheet()->mergeCells($pRange);
|
|
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $title);
|
|
|
|
# 水平居中
|
|
$objStyle = $objPHPExcel->getActiveSheet()->getStyle($pCoordinate); //获取要设置单元格的样式,括号里的内容也可是:('A1:E1')
|
|
$objAlign = $objStyle->getAlignment(); //用来设置对齐属性和单元格内文本换行的一个变量
|
|
$objAlign->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
}
|
|
|
|
# 第二行的列
|
|
$pCoordinate = $columnkey.'2';
|
|
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $v);
|
|
|
|
$column_num--;
|
|
}
|
|
else{
|
|
# 第一二行需合并的列
|
|
$pRange = $columnkey.'1:'.$columnkey.'2';
|
|
$pCoordinate = $columnkey.'1';
|
|
$objPHPExcel->getActiveSheet()->mergeCells($pRange);
|
|
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $v);
|
|
|
|
# 垂直居中
|
|
$objStyle = $objPHPExcel->getActiveSheet()->getStyle($pCoordinate);
|
|
$objAlign = $objStyle->getAlignment();
|
|
$objAlign->setVertical(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
}
|
|
$i++;
|
|
}
|
|
|
|
# 再生成数据
|
|
$data = array_slice($data, 1);
|
|
$j = 3;
|
|
foreach ($data as $val) {
|
|
$i = 0;
|
|
// 内循环产生每一行
|
|
foreach ($indexs as $k => $v){
|
|
// 数字列转换为字母列 如:27变为AA
|
|
$objPHPExcel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($i) . $j, $val[$k]);
|
|
$i++;
|
|
}
|
|
$j++;
|
|
}
|
|
|
|
$filename = iconv('UTF-8', 'UTF-8', $filename) . '.xls';
|
|
header('Pragma: public');
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Content-Type: application/force-download');
|
|
header('Content-Type: application/vnd.ms-execl');
|
|
header('Content-Type: application/octet-stream');
|
|
header('Content-Type: application/download');
|
|
header("Content-Disposition: attachment; filename=$filename");
|
|
header('Content-Transfer-Encoding: binary');
|
|
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
return $objWriter->save('php://output');
|
|
}
|
|
} |