Advertisement
Guest User

Untitled

a guest
May 24th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. <?php
  2.  
  3. class ExcelCsvWriter
  4. {
  5. /**
  6. * @var resource
  7. */
  8. private $resultStream;
  9.  
  10. public function __construct($stream)
  11. {
  12. $meta = stream_get_meta_data($stream);
  13. if ($meta['mode'] === 'r') {
  14. throw new \Exception('Can\'t write to read-only stream');
  15. }
  16.  
  17. $this->resultStream = $stream;
  18. fwrite($this->resultStream, pack('C*', 0xFF, 0xFE)); // UTF16-LE BOM
  19. stream_filter_append($this->resultStream, 'convert.iconv.UTF-8/UTF-16LE', STREAM_FILTER_WRITE);
  20. }
  21.  
  22. /**
  23. * @param array $row
  24. */
  25. public function writeRow($row)
  26. {
  27. array_walk($row, function(&$value) {
  28. if (is_float($value)) {
  29. $value = '="' . $value . '"';
  30. }
  31. });
  32.  
  33. fputcsv($this->resultStream, $row, "\t", '"');
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement