Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ExcelCsvWriter
- {
- /**
- * @var resource
- */
- private $resultStream;
- public function __construct($stream)
- {
- $meta = stream_get_meta_data($stream);
- if ($meta['mode'] === 'r') {
- throw new \Exception('Can\'t write to read-only stream');
- }
- $this->resultStream = $stream;
- fwrite($this->resultStream, pack('C*', 0xFF, 0xFE)); // UTF16-LE BOM
- stream_filter_append($this->resultStream, 'convert.iconv.UTF-8/UTF-16LE', STREAM_FILTER_WRITE);
- }
- /**
- * @param array $row
- */
- public function writeRow($row)
- {
- array_walk($row, function(&$value) {
- if (is_float($value)) {
- $value = '="' . $value . '"';
- }
- });
- fputcsv($this->resultStream, $row, "\t", '"');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement