<?php
/**
* Excel 助手
* sudo composer require phpoffice/phpspreadsheet
*/
namespace CommonUtil;
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;
use PhpOfficePhpSpreadsheetStyleAlignment;
use PhpOfficePhpSpreadsheetStyleColor;
class ExcelUtil extends CommonUtil
{
public function __construct()
{
parent::__construct();
}
/**
* 导出数据
*/
public function exportExcel($expTitle, $expCellName, $expTableData, $subTitle)
{
$fileName = $expTitle;
$spreadsheet = new Spreadsheet();
// 获取活动的工作空间
$worksheet = $spreadsheet->getActiveSheet();
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$cellName = 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', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
// 合并单元格
$worksheet->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');
// 设置第一格的内容
$worksheet->setCellValue('A1', $expTitle);
// 设置加粗
$worksheet->getStyle('A1')->getFont()->setBold(true);
// 设置居中
$styleArray = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
];
$worksheet->getStyle('A1')->applyFromArray($styleArray);
// 设置颜色
$worksheet->getStyle('A1')->getFont()->getColor()->setARGB(Color::COLOR_DARKRED);
// 设置标题
$worksheet->setTitle($expTitle);
if ($subTitle) {
// 合并单元格
$worksheet->mergeCells('A2:' . $cellName[$cellNum - 1] . '2');
// 设置第一格的内容
$worksheet->setCellValue('A2', $subTitle);
// 设置居中
$styleArray = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
];
$worksheet->getStyle('A2')->applyFromArray($styleArray);
// 设置颜色
$worksheet->getStyle('A2')->getFont()->getColor()->setARGB(Color::COLOR_DARKGREEN);
// 设置表格菜单
for ($i = 0; $i < $cellNum; $i++) {
$worksheet->setCellValue($cellName[$i] . (+3), $expCellName[$i][1]);
$worksheet->getStyle($cellName[$i] . (+3))->getFont()->setBold(true);
// 自动宽度
$worksheet->getColumnDimension($cellName[$i])->setAutoSize(true);
}
// 设置表格内容
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$worksheet->setCellValue($cellName[$j] . ($i + 4), $expTableData[$i][$expCellName[$j][0]]);
// 支持换行
if ($expCellName[$j][2] == 1) {
$worksheet->getStyle($cellName[$j] . ($i + 4))->getAlignment()->setWrapText(true);
}
}
}
} else {
// 设置表格菜单
for ($i = 0; $i < $cellNum; $i++) {
$worksheet->setCellValue($cellName[$i] . (+2), $expCellName[$i][1]);
$worksheet->getStyle($cellName[$i] . (+2))->getFont()->setBold(true);
// 自动宽度
$worksheet->getColumnDimension($cellName[$i])->setAutoSize(true);
}
// 设置表格内容
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$worksheet->setCellValue($cellName[$j] . ($i + 3), $expTableData[$i][$expCellName[$j][0]]);
// 支持换行
if ($expCellName[$j][2] == 1) {
$worksheet->getStyle($cellName[$j] . ($i + 3))->getAlignment()->setWrapText(true);
}
}
}
}
// 设置列宽为自动
// $spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
// $worksheet->getDefaultColumnDimension()->setAutoSize(true);
// 设置行高
$worksheet->getDefaultRowDimension()->setRowHeight(20);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //告诉浏览器输出07Excel文件
//header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"'); //告诉浏览器输出浏览器名称
header('Cache-Control: max-age=0'); //禁止缓存
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
}
}
使用
/**
* 导出数据
*/
public function export_data() {
$id = $_GET['survey_id'];
$survey = M('survey');
$survey_info = $survey->where(array('id' => $id))->find();
if (!$survey_info) {
$this->error('参数错误', 'index');
}
// 获取题目
$survey_question = M('survey_question');
$survey_question_item = M('survey_question_item');
$survey_answer = M('survey_answer');
$survey_question_list = $survey_question->where(['survey_id'=>$id,'deleted'=>0])->order('weight desc,id asc')->select();
foreach($survey_question_list as $k => &$v) {
$v['subject'] = ($k+1).'、'.$v['subject'];
if ($v['type'] == 1) {
$v['type_str'] = '单选';
// 获取选项和各选项答题数量
$item_list = $survey_question_item->where(['question_id'=>$v['id'],'deleted'=>0])->select();
foreach($item_list as $ik => &$iv) {
$count = $survey_answer->where(['question_id'=>$v['id'],'answer'=>['like','%,'.$iv['id'].',%']])->count();
if ($ik+1 == count($item_list)) {
$v['result'] .= $iv['option']."【".$count."个回答】";
} else {
$v['result'] .= $iv['option']."【".$count."个回答】
";
}
}
}
if ($v['type'] == 2) {
$v['type_str'] = '多选';
// 获取选项和各选项答题数量
$item_list = $survey_question_item->where(['question_id'=>$v['id'],'deleted'=>0])->select();
foreach($item_list as $ik => &$iv) {
$count = $survey_answer->where(['question_id'=>$v['id'],'answer'=>['like','%,'.$iv['id'].',%']])->count();
if ($ik+1 == count($item_list)) {
$v['result'] .= $iv['option']."【".$count."个回答】";
} else {
$v['result'] .= $iv['option']."【".$count."个回答】
";
}
}
}
if ($v['type'] == 3) {
$v['type_str'] = '问答';
// 获取回答结果
$answer_list = $survey_answer->where(['question_id'=>$v['id'],'deleted'=>0])->select();
foreach($answer_list as $ak => &$av) {
if ($ak+1 == count($answer_list)) {
$v['result'] .= "回答".($ak+1)."【".$av['answer']."】";
} else {
$v['result'] .= "回答".($ak+1)."【".$av['answer']."】
";
}
}
}
}
$survey_company = M('survey_company');
$survey_company_list = $survey_company->where(['survey_id' => $id, 'deleted' => 0])->select();
$company_count = count($survey_company_list);
$sub_count = 0;
foreach ($survey_company_list as $sv) {
if ($sv['is_sub']) {
$sub_count++;
}
}
$company_sub_count = $sub_count;
$subTitle = '本次分配了'.$company_count.'个企业,提交了'.$company_sub_count.'个企业';
$xlsCell = array(
array('subject', '题目名称'),
array('type_str', '题目类型'),
array('result', '答题结果'),
);
$xlsName = '调查问卷《'.$survey_info['title'].'》统计结果';
$excelUtil = new ExcelUtil();
$excelUtil->exportExcel($xlsName, $xlsCell, $survey_question_list,$subTitle);
}