You are here

public function PHPExcel_Writer_CSV::save in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/CSV.php \PHPExcel_Writer_CSV::save()

* Save PHPExcel to file * *

Parameters

string $pFilename: * @throws PHPExcel_Writer_Exception

Overrides PHPExcel_Writer_IWriter::save

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/CSV.php, line 101

Class

PHPExcel_Writer_CSV
PHPExcel_Writer_CSV

Code

public function save($pFilename = null) {

  // Fetch sheet
  $sheet = $this->_phpExcel
    ->getSheet($this->_sheetIndex);
  $saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)
    ->getDebugLog()
    ->getWriteDebugLog();
  PHPExcel_Calculation::getInstance($this->_phpExcel)
    ->getDebugLog()
    ->setWriteDebugLog(FALSE);
  $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);

  // Open file
  $fileHandle = fopen($pFilename, 'wb+');
  if ($fileHandle === false) {
    throw new PHPExcel_Writer_Exception("Could not open file {$pFilename} for writing.");
  }
  if ($this->_excelCompatibility) {
    fwrite($fileHandle, "");

    //	Enforce UTF-8 BOM Header
    $this
      ->setEnclosure('"');

    //	Set enclosure to "
    $this
      ->setDelimiter(";");

    //	Set delimiter to a semi-colon
    $this
      ->setLineEnding("\r\n");
    fwrite($fileHandle, 'sep=' . $this
      ->getDelimiter() . $this->_lineEnding);
  }
  elseif ($this->_useBOM) {

    // Write the UTF-8 BOM code if required
    fwrite($fileHandle, "");
  }

  //	Identify the range that we need to extract from the worksheet
  $maxCol = $sheet
    ->getHighestDataColumn();
  $maxRow = $sheet
    ->getHighestDataRow();

  // Write rows to file
  for ($row = 1; $row <= $maxRow; ++$row) {

    // Convert the row to an array...
    $cellsArray = $sheet
      ->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->_preCalculateFormulas);

    // ... and write to the file
    $this
      ->_writeLine($fileHandle, $cellsArray[0]);
  }

  // Close file
  fclose($fileHandle);
  PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  PHPExcel_Calculation::getInstance($this->_phpExcel)
    ->getDebugLog()
    ->setWriteDebugLog($saveDebugLog);
}