}> */ private array $sheets = []; public function __destruct() { foreach ($this->sheets as $sheet) { if (is_resource($sheet['handle'])) { fclose($sheet['handle']); } @unlink($sheet['path']); } } /** @param list $cells */ public function row(string $sheet, array $cells, bool $bold = false): void { $sheet = self::sheetName($sheet); if (! isset($this->sheets[$sheet])) { $path = tempnam(sys_get_temp_dir(), 'bill_sheet_'); $handle = $path === false ? false : fopen($path, 'wb'); if ($handle === false) { throw new RuntimeException('无法创建导出临时文件。'); } $this->sheets[$sheet] = ['path' => $path, 'handle' => $handle, 'row' => 0, 'widths' => []]; } $state = &$this->sheets[$sheet]; $number = ++$state['row']; $xml = ''; foreach (array_values($cells) as $index => $value) { if ($value === null || $value === '') { continue; } $reference = self::column($index + 1) . $number; $style = $bold ? ' s="1"' : ''; if (is_int($value) || is_float($value)) { $xml .= '' . self::number($value) . ''; $width = strlen(self::number($value)); } else { $text = (string) $value; $xml .= '' . self::escape($text) . ''; $width = mb_strwidth($text); } if ($number <= 200) { $state['widths'][$index] = max($state['widths'][$index] ?? 8, min($width + 2, 40)); } } fwrite($state['handle'], $xml . ''); } public function isEmpty(): bool { return $this->sheets === []; } /** @param resource $output */ public function writeTo($output): void { if ($this->sheets === []) { $this->row('Sheet1', []); } $zipPath = tempnam(sys_get_temp_dir(), 'bill_zip_'); if ($zipPath === false) { throw new RuntimeException('无法创建导出文件。'); } try { $zip = new ZipArchive(); if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { throw new RuntimeException('无法创建导出文件。'); } $names = array_keys($this->sheets); $contentTypes = ''; $workbook = ''; $relations = ''; foreach ($names as $i => $name) { $n = $i + 1; $state = $this->sheets[$name]; fclose($state['handle']); $cols = ''; foreach ($state['widths'] as $index => $width) { $cols .= ''; } $sheetPath = tempnam(sys_get_temp_dir(), 'bill_ws_'); $out = fopen((string) $sheetPath, 'wb'); $in = fopen($state['path'], 'rb'); fwrite($out, '' . '' . '' . ($cols !== '' ? '' . $cols . '' : '') . ''); stream_copy_to_stream($in, $out); fwrite($out, ''); fclose($in); fclose($out); @unlink($state['path']); $this->sheets[$name]['path'] = (string) $sheetPath; $zip->addFile((string) $sheetPath, 'xl/worksheets/sheet' . $n . '.xml'); $contentTypes .= ''; $workbook .= ''; $relations .= ''; } $styles = count($names) + 1; $zip->addFromString('[Content_Types].xml', '' . '' . '' . '' . '' . '' . $contentTypes . ''); $zip->addFromString('_rels/.rels', '' . '' . '' . ''); $zip->addFromString('xl/workbook.xml', '' . '' . '' . $workbook . ''); $zip->addFromString('xl/_rels/workbook.xml.rels', '' . '' . $relations . '' . ''); $zip->addFromString('xl/styles.xml', '' . '' . '' . '' . '' . '' . '' . '' . ''); $zip->close(); $handle = fopen($zipPath, 'rb'); stream_copy_to_stream($handle, $output); fclose($handle); } finally { @unlink($zipPath); } } private static function sheetName(string $name): string { $name = str_replace(['\\', '/', '?', '*', '[', ']', ':'], '-', $name); return mb_substr($name === '' ? 'Sheet' : $name, 0, 31); } private static function number(int|float $value): string { if (is_int($value)) { return (string) $value; } if (! is_finite($value)) { return '0'; } $text = rtrim(rtrim(sprintf('%.10F', $value), '0'), '.'); return ($text === '' || $text === '-' || $text === '-0') ? '0' : $text; } private static function escape(string $value): string { if (! mb_check_encoding($value, 'UTF-8')) { $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8'); } $value = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/u', '', $value) ?? $value; return htmlspecialchars($value, ENT_QUOTES | ENT_XML1, 'UTF-8'); } private static function column(int $index): string { $name = ''; while ($index > 0) { $index--; $name = chr(65 + ($index % 26)) . $name; $index = intdiv($index, 26); } return $name; } }