<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Yo;
use Lu;

class ExcelController extends Controller
{
  public function excel_test()
  {
    $spreadsheet = new Spreadsheet();
    $worksheet = $spreadsheet->getActiveSheet();
    $worksheet->setCellValue('A1', 'AAA');
    $worksheet->setCellValue('B1', Lu::date());
    $worksheet->setCellValue('C1', 'CCC');
    $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(20);
    $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(120);
    $spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(50);
    for ($i = 2; $i <= 11; $i++) {
      $worksheet->setCellValueExplicit('A' . $i, 'A' . $i, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
      $worksheet->setCellValueExplicit('B' . $i, 'B' . $i, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
      $worksheet->setCellValueExplicit('C' . $i, 'C' . $i, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
    }
    Yo::echo();
    $writer = new Xlsx($spreadsheet);
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="导出数据.xlsx"');
    header('Cache-Control: max-age=0');
    $writer->save('php://output');
  }
}
