/**
* 엑셀 다운로드
* phpspreadsheet 사용
*/
function downloadXlsx($fn=null,$conf=null,$rows=null){
if(!isset($fn[0])) $fn = '엑셀다운로드';
//-- STYLE
$styleBoldRed = [
'font' => [
'bold' => true,
'color'=>[
'argb'=>PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED,
]
],
];
$styleCenterCenter = [
'alignment' => [
'horizontal' => PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
'vertical' => PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
],
];
$styleAllBorders = [
'borders' => [
'allBorders' => [
// 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => '00000000'],
],
],
];
$styleLink = [
'font' => array(
'color' => array('rgb' => '0000FF'),
'underline' => 'single',
)
];
$styleHeadFill = [
'fill'=>[
'fillType'=>\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => array('rgb' => 'C6EFCE'),
]
];
$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->getProperties()->setCreator('RAPS')
->setLastModifiedBy('RAPS')
->setTitle('RAPS')
->setSubject('RAPS')
->setDescription('RAPS')
->setKeywords('RAPS')
->setCategory('RAPS')
->setDescription(
"CREATE IN RAPS"
);
//--- 첫 시트
$sheet = $spreadsheet->getActiveSheet();
$default_width = 20;//20글자
$sheet->getDefaultColumnDimension()->setWidth($default_width); // 각 시트에 기본 너비 설정함
// $sheet->getDefaultColumnDimension()->setAutoSize(true);
// $sheet->setCellValue('A1', '테스트입니다.');
//--- col 설정
$ri = 1; //row idx
$ci = 1; //cell idx
// print_r($conf['fields']);exit;
if(isset($conf['fields'])){
foreach($conf['fields'] as $field){
// $ca = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($ci);
if(isset($field['width']) && $field['width'] !=='auto'){
if($field['width'] =='auto'){
$sheet->getColumnDimensionByColumn($ci)->setAutoSize(true);
}else{
$sheet->getColumnDimensionByColumn($ci)->setWidth($field['width']);
}
}
$cell = $sheet->getCellByColumnAndRow($ci,$ri);
$cell->setValue($field['value']);
$cell->getStyle()->applyFromArray($styleCenterCenter+$styleAllBorders+$styleHeadFill);
$ci++;
}
$ri++;
}
foreach($rows as $row){
$ci = 1; //cell idx
foreach($row as $k => $v){
$field = $conf['fields'][$k]??null;
$cell = $sheet->getCellByColumnAndRow($ci,$ri);
$cell->setValue($v);
$cell->getStyle()->applyFromArray($styleCenterCenter+$styleAllBorders);
$cell->getStyle()->getNumberFormat()
->setFormatCode(
PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT
);
$ci++;
}
$ri++;
}
// 출력부 - xlsx
$writer = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$fn.'.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
// 테스트 출력부 - HTML
// $writer = new PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
// $writer->save('php://output');
exit();
}
$fn = '테스트파일';
$conf = [
'fields'=>[
['value'=>'필드명1','width'=>10],
['value'=>'필드명2','width'=>20],
['value'=>'필드명3','width'=>30],
['value'=>'필드명4','width'=>40],
['value'=>'필드명5','width'=>'auto'],
['value'=>'필드명6'],
['value'=>'필드명7'],
['value'=>'필드명8'],
['value'=>'필드명9'],
['value'=>'필드명10'],
['value'=>'필드명11'],
]
];
$rows = [
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
['데이터1','데이터2','데이터3','데이터4','데이터5','데이터6','데이터7','데이터8','데이터9','데이터10','데이터11','데이터12','데이터13',],
];
downloadXlsx($fn,$conf,$rows);