- 首页 > it技术 > >
phpspread使用完整示例
//导出
public function export(Request $request)
{
$data = https://www.it610.com/article/$request->all();
$pageName = $data['pageName'] ?? '';
$options['webConfigKV'] = WebConfigService::S()->getWebConfigKV();
$tpl = $data['tpl'] ?? Project::$tplInfo;
$filename = $options['webConfigKV']['tpl'][$tpl] . '_' . date('Y-m-d');
if (Str::startsWith($pageName, 'check')) {
$header = [
'id' => 'id',
'control' => '控制项',
'desc' => '描述',
'purpose' => '检查目的',
'tab' => '类别',
'tabCn' => '类别中文',
'law' => '法律依据',
'standard' => '标准依据',
'disk' => '判断规则',
'dangerConditions' => '风险条件',
'source' => '判断依据',
'conditions' => '条件',
];
$columnWidth = [
'B' => 100,
'C' => 100,
'D' => 100,
'G' => 100,
'H' => 100,
'I' => 100,
];
} elseif (Str::startsWith($pageName, 'question')) {
$header = [
'id' => 'id',
'title' => '问题',
'type' => '类型',
'typeCn' => '类型中文',
'parentId' => '父id',
'conditions' => '存在条件',
'field' => '字段',
'tab' => '类别',
'tabCn' => '类别中文',
'replace' => '标题替换',
'level' => '级别',
'isRequired' => '必填',
'isInline' => '单行',
];
$columnWidth = [
'B' => 120,
];
}
switch ($pageName) {
case 'checkList':
$filename = '数据盘点检查库-' . $filename;
LibraryCheckListService::S()->exportCheck(Project::$processList, $tpl, $filename, $header, $columnWidth);
break;
case 'checkSystem':
$filename = '体系评估检查库-' . $filename;
LibraryCheckSystemService::S()->exportCheck(Project::$processSystem, $tpl, $filename, $header, $columnWidth);
break;
case 'questionList':
$filename = '数据盘点问题库-' . $filename;
LibraryQuestionListService::S()->exportQuestion(Project::$processList, $tpl, $filename, $header, $columnWidth);
break;
case 'questionSystem':
$filename = '体系评估问题库-' . $filename;
LibraryQuestionSystemService::S()->exportQuestion(Project::$processSystem, $tpl, $filename, $header, $columnWidth);
break;
}
return;
}
public function exportQuestion($theType, $tpl, $filename, $header, $columnWidth)
{
$data = https://www.it610.com/article/[];
$headerFields = array_keys($header);
$headerTitles = array_values($header);
$questions = $this->questionModel::where('isRemoved', 0)
->where('tpl', $tpl)
->get();
$options = ProjectProcessList::getMap($tpl)
+ ProjectProcessSystem::getMap($tpl)
+ LibraryQuestion::getMap();
$i = 0;
foreach ($questions as &$question) {
$question->unpack();
$type = $question->type;
$question->typeCn = $options['answer'][$type];
$question->tabCn = $options[$theType . 'Tab'][$question->tab] ?? $question->tab;
foreach ($headerFields as $field) {
if ($field == 'conditions') {
$data[$i][$field] = \Util::libraryJson2Str($question->$field);
} else if (in_array($field, ['replace'])) {
$data[$i][$field] = implode(",", $question->$field);
} else {
$data[$i][$field] = strval($question->$field);
}
}
if (in_array($type, [LibraryQuestion::$answerTable
, LibraryQuestion::$answerTableDisplay
, LibraryQuestion::$answerCheckbox
, LibraryQuestion::$answerRadio])) {
foreach ($question->answer as $answer) {
$i++;
foreach ($headerFields as $field) {
if($field == 'title') {
$data[$i][$field] = ('[' . $answer['key'] . ']' . $answer['val']);
} elseif($field == 'type' && isset($answer['type'])) {
$data[$i][$field] = $answer['type'];
} elseif($field == 'field' && isset($answer['field'])) {
$data[$i][$field] = $answer['field'];
} elseif($field == 'isRequired' && isset($answer['isRequired'])) {
$data[$i][$field] = strval($answer['isRequired']);
} else {
$data[$i][$field] = '';
}
}
}
}
$i++;
}
return \Util::export($headerTitles, $data, $filename, $columnWidth);
}
public static function export($headerTitles, $data, $filename, $columnWidth = [])
{
$spreadsheet = new Spreadsheet();
$titleArr[] = $headerTitles;
$result = array_merge($titleArr, $data);
$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);
//默认列宽
if ($columnWidth) {
foreach ($columnWidth as $character => $width) {
$spreadsheet->getActiveSheet()->getColumnDimension($character)->setWidth($width);
}
}
$spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
//默认行高
$spreadsheet->getActiveSheet()->fromArray($result);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;
filename="' . $filename . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
ob_end_clean();
$writer->save('php://output');
/* 释放内存 */
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
return;
}
推荐阅读