PHP code example of exinfinite / excel
1. Go to this page and download the library: Download exinfinite/excel library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
exinfinite / excel example snippets
composer
use Exinfinite\Excel;
$excel = new Excel();
/**
* @param Exinfinite\Excel $excel
* @param array $cols 每個直欄的設定及標題
* @param array $data_array 每列資料
* @param [type] $filename 檔名(不含副檔名)
* @return void
* 參數範例
* $cols = [
'A' => ['width' => 10, 'title' => '標題1'],
'B' => ['width' => 25, 'title' => '標題2'],
'C' => ['width' => 25, 'title' => '標題3'],
];
$data_array = [
['資料1', '資料2', '資料3'],
['資料1', '資料2', '資料3']
];
*/
function excel(Exinfinite\Excel $excel, $cols = [], $data_array = [], $filename) {
array_unshift($data_array, array_column($cols, 'title'));
$excel->write($data_array, "A1");
$excel->set_active_sheet(0);
$act_sheet = $excel->objPHPExcel->getActiveSheet();
foreach (array_combine(array_keys($cols), array_column($cols, 'width')) as $col => $w) {
$act_sheet->getColumnDimension($col)->setWidth($w)->setAutoSize(false);
}
$highestCol = $act_sheet->getHighestDataColumn();
$highestRow = $act_sheet->getHighestDataRow();
$excel->style([
'borders' => [
'allborders' => [
'style' => PHPExcel_Style_Border::BORDER_THIN,
],
],
'alignment' => [
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
'wrap' => true,
],
], "A1", "{$highestCol}{$highestRow}");
$excel->style([
'fill' => [
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => ['argb' => 'D1EEEE'],
],
], "A1", "{$highestCol}1");
header("Content-Type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename={$filename}.xls");
header("Cache-Control:max-age=0");
return $excel->export();
}