Feature: Excel compatibility option added for writing CSV files

While Excel 2010 can read CSV files with a simple UTF-8 BOM, Excel2007 and earlier require UTF-16LE encoded tab-separated files.
The new setExcelCompatibility(TRUE) option for the CSV Writer will generate files with this formatting for easy import into Excel2007 and below.
This commit is contained in:
Mark Baker 2012-09-05 14:12:00 +01:00
parent a376606bd2
commit 2d6ad66f0c
3 changed files with 44 additions and 3 deletions

View File

@ -181,6 +181,7 @@ class PHPExcel_CachedObjectStorage_CacheBase {
$highestRow = max($row); $highestRow = max($row);
$highestColumn = substr(max($col),1); $highestColumn = substr(max($col),1);
} }
return array( 'row' => $highestRow, return array( 'row' => $highestRow,
'column' => $highestColumn 'column' => $highestColumn
); );

View File

@ -83,6 +83,13 @@ class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
*/ */
private $_useBOM = false; private $_useBOM = false;
/**
* Whether to write a fully Excel compatible CSV file.
*
* @var boolean
*/
private $_excelCompatibility = false;
/** /**
* Create a new PHPExcel_Writer_CSV * Create a new PHPExcel_Writer_CSV
* *
@ -113,7 +120,12 @@ class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
throw new Exception("Could not open file $pFilename for writing."); throw new Exception("Could not open file $pFilename for writing.");
} }
if ($this->_useBOM) { if ($this->_excelCompatibility) {
// Write the UTF-16LE BOM code
fwrite($fileHandle, "\xFF\xFE"); // Excel uses UTF-16LE encoding
$this->setEnclosure(); // Default enclosure is "
$this->setDelimiter("\t"); // Excel delimiter is a TAB
} elseif ($this->_useBOM) {
// Write the UTF-8 BOM code // Write the UTF-8 BOM code
fwrite($fileHandle, "\xEF\xBB\xBF"); fwrite($fileHandle, "\xEF\xBB\xBF");
} }
@ -220,6 +232,27 @@ class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
return $this; return $this;
} }
/**
* Get whether the file should be saved with full Excel Compatibility
*
* @return boolean
*/
public function getExcelCompatibility() {
return $this->_excelCompatibility;
}
/**
* Set whether the file should be saved with full Excel Compatibility
*
* @param boolean $pValue Set the file to be written as a fully Excel compatible csv file
* Note that this overrides other settings such as useBOM, enclosure and delimiter
* @return PHPExcel_Writer_CSV
*/
public function setExcelCompatibility($pValue = false) {
$this->_excelCompatibility = $pValue;
return $this;
}
/** /**
* Get sheet index * Get sheet index
* *
@ -274,9 +307,13 @@ class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
$line .= $this->_lineEnding; $line .= $this->_lineEnding;
// Write to file // Write to file
fwrite($pFileHandle, $line); if ($this->_excelCompatibility) {
fwrite($pFileHandle, mb_convert_encoding($line,"UTF-16LE","UTF-8"));
} else {
fwrite($pFileHandle, $line);
}
} else { } else {
throw new Exception("Invalid parameters passed."); throw new Exception("Invalid data row passed to CSV writer.");
} }
} }

View File

@ -81,6 +81,9 @@ Fixed in develop branch:
- Feature: (Progi1984) & (blazzy) Work items 9605 - Implement support for AutoFilter in PHPExcel_Writer_Excel5 - Feature: (Progi1984) & (blazzy) Work items 9605 - Implement support for AutoFilter in PHPExcel_Writer_Excel5
- Feature: (MBaker) Modified ERF and ERFC Engineering functions to accept Excel 2010's modified acceptance of negative arguments - Feature: (MBaker) Modified ERF and ERFC Engineering functions to accept Excel 2010's modified acceptance of negative arguments
- Feature: (k1LoW) Support SheetView `view` attribute (Excel2007) - Feature: (k1LoW) Support SheetView `view` attribute (Excel2007)
- Feature: (MBaker) Excel compatibility option added for writing CSV files
While Excel 2010 can read CSV files with a simple UTF-8 BOM, Excel2007 and earlier require UTF-16LE encoded tab-separated files.
The new setExcelCompatibility(TRUE) option for the CSV Writer will generate files with this formatting for easy import into Excel2007 and below.
- General: (alexgann) Add Currency detection to the Advanced Value Binder - General: (alexgann) Add Currency detection to the Advanced Value Binder
- General: (MBaker) Work item 18404 - setCellValueExplicitByColumnAndRow() do not return PHPExcel_Worksheet - General: (MBaker) Work item 18404 - setCellValueExplicitByColumnAndRow() do not return PHPExcel_Worksheet
- General: (MBaker) Work item 18324 - Reader factory doesn't read anymore XLTX and XLT files - General: (MBaker) Work item 18324 - Reader factory doesn't read anymore XLTX and XLT files