123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\common\util;
- use PhpOffice\PhpSpreadsheet\Exception;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Style\Border;
- use PhpOffice\PhpSpreadsheet\Style\Color;
- use PhpOffice\PhpSpreadsheet\Style\Fill;
- class PhpSpreadsheetExport
- {
- public function index()
- {
- $data = [
- ['title1' => '111', 'title2' => '111', 'title3' => 666],
- ['title1' => '222', 'title2' => '222'],
- ['title1' => '333', 'title2' => '333']
- ];
- $tableHeader = [
- ['第一行标题', '第一行标题'],
- ['第二行标题', '第二行标题']
- ];
- $mergeCells = [
- ['A1:B1' => '第一行标题', 'C1:F1' => '第一111行标题'],
- ['A2:B2' => '第一行标题', 'C2:E2' => '第一222行标题'],
- ];
- $fileName = "8888.xlsx";
- $this->saveFile($data, $fileName, $tableHeader);
- }
- public static array $excelCol = ['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'];
- public static bool $setBold = false;
- public static string $setName = '宋体';
- public static string $setSize = '12';
- public static string $setBgRGB = 'FFFF00';
- public static string $setFontRGB = 'FF000000';
- public static array $styleArray = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'vertical' => Alignment::VERTICAL_CENTER
- ],
- 'borders' => [
- 'allBorders' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => '000000'],
- ],
- ],
- ];
-
- public function saveFile($data, $fileName, array $tableHeader = [], array $mergeCells = [], string $suffix = 'xlsx'): bool
- {
- try {
- $spreadsheet = self::write($data, $tableHeader, $mergeCells);
- $writer = IOFactory::createWriter($spreadsheet, ucwords($suffix));
- $writer->save($fileName, true);
- return true;
- } catch (\Exception) {
- return false;
- }
- }
-
- public static function write($data, $tableHeader, $mergeCells): Spreadsheet
- {
-
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $totalCol = 0;
-
- foreach ($mergeCells as $row => $rows) {
- $i = 0;
- foreach ($rows as $col => $colValue) {
-
- $sheet->mergeCells($col);
-
- self::setStyle($sheet, $i, $totalCol, $row);
-
- $sheet->setCellValue(substr($col, 0, strpos($col, ":")), $colValue);
- $i++;
- }
- }
- $totalCol = count($mergeCells);
-
- foreach ($tableHeader as $row => $rows) {
- $headerRowDatas = array_values($rows);
- foreach ($headerRowDatas as $col => $colValue) {
-
- self::setStyle($sheet, $col, $totalCol, $row);
-
- $sheet->setCellValue(self::$excelCol[$col] . ($totalCol + $row + 1), $colValue);
- }
- }
- $totalCol += count($tableHeader);
-
- foreach ($data as $row => $rows) {
- $rowDatas = array_values($rows);
- foreach ($rowDatas as $col => $colValue) {
-
- $sheet->setCellValue(self::$excelCol[$col] . ($totalCol + $row + 1), $colValue);
- }
- }
- return $spreadsheet;
- }
-
- public static function setStyle($sheet, $col, $totalCol, $row): void
- {
-
- $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))->applyFromArray(self::$styleArray);
-
- $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
- ->getFill()
- ->setFillType(Fill::FILL_SOLID)
- ->getStartColor()
- ->setRGB(self::$setBgRGB);
-
- $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
- ->getFont()
- ->setBold(self::$setBold)
- ->setName(self::$setName)
- ->setSize(self::$setSize);
-
- $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
- ->getFont()
- ->getColor()->setRGB(self::$setFontRGB);
- }
- }
|