From 60c9bf391cede3b95db94384960ec9572a2f413a Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Thu, 15 Aug 2013 18:10:29 +0100 Subject: [PATCH] Excel compatibility mode for CSV Writer --- Classes/PHPExcel/Reader/Excel2003XML.php | 2 +- Classes/PHPExcel/Writer/CSV.php | 21 +++++++++------------ Examples/02types-xls.php | 11 +++++++++++ Examples/02types.php | 12 +++++++++++- Examples/16csv.php | 16 +++++++++++++++- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/Classes/PHPExcel/Reader/Excel2003XML.php b/Classes/PHPExcel/Reader/Excel2003XML.php index 1d16855d..acde1cca 100644 --- a/Classes/PHPExcel/Reader/Excel2003XML.php +++ b/Classes/PHPExcel/Reader/Excel2003XML.php @@ -22,7 +22,7 @@ * @package PHPExcel_Reader * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL - * @version ##VERSION##, ##DATE## + * @version 1.7.9, 2013-06-02 */ diff --git a/Classes/PHPExcel/Writer/CSV.php b/Classes/PHPExcel/Writer/CSV.php index 2ca7c258..6d624eeb 100644 --- a/Classes/PHPExcel/Writer/CSV.php +++ b/Classes/PHPExcel/Writer/CSV.php @@ -114,18 +114,19 @@ class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_W } 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 + fwrite($fileHandle, "\xEF\xBB\xBF"); // 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 + // Write the UTF-8 BOM code if required fwrite($fileHandle, "\xEF\xBB\xBF"); } // Identify the range that we need to extract from the worksheet - $maxCol = $sheet->getHighestColumn(); - $maxRow = $sheet->getHighestRow(); + $maxCol = $sheet->getHighestDataColumn(); + $maxRow = $sheet->getHighestDataRow(); // Write rows to file for($row = 1; $row <= $maxRow; ++$row) { @@ -300,11 +301,7 @@ class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_W $line .= $this->_lineEnding; // Write to file - if ($this->_excelCompatibility) { - fwrite($pFileHandle, mb_convert_encoding($line,"UTF-16LE","UTF-8")); - } else { - fwrite($pFileHandle, $line); - } + fwrite($pFileHandle, $line); } else { throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer."); } diff --git a/Examples/02types-xls.php b/Examples/02types-xls.php index 1e9025d6..81d41914 100644 --- a/Examples/02types-xls.php +++ b/Examples/02types-xls.php @@ -121,6 +121,17 @@ $objRichText->createText(', unless specified otherwise on the invoice.'); $objPHPExcel->getActiveSheet()->setCellValue('A13', 'Rich Text') ->setCellValue('C13', $objRichText); + +$objRichText2 = new PHPExcel_RichText(); +$objRichText2->createText("black text\n"); + +$objRed = $objRichText2->createTextRun("red text"); +$objRed->getFont()->setColor( new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED ) ); + +$objPHPExcel->getActiveSheet()->getCell("C14")->setValue($objRichText2); +$objPHPExcel->getActiveSheet()->getStyle("C14")->getAlignment()->setWrapText(true); + + $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); diff --git a/Examples/02types.php b/Examples/02types.php index 4fc4114a..386e4f5b 100644 --- a/Examples/02types.php +++ b/Examples/02types.php @@ -122,7 +122,17 @@ $objRichText->createText(', unless specified otherwise on the invoice.'); $objPHPExcel->getActiveSheet()->setCellValue('A13', 'Rich Text') ->setCellValue('C13', $objRichText); - + +$objRichText2 = new PHPExcel_RichText(); +$objRichText2->createText("black text\n"); + +$objRed = $objRichText2->createTextRun("red text"); +$objRed->getFont()->setColor( new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED ) ); + +$objPHPExcel->getActiveSheet()->getCell("C14")->setValue($objRichText2); +$objPHPExcel->getActiveSheet()->getStyle("C14")->getAlignment()->setWrapText(true); + + $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); diff --git a/Examples/16csv.php b/Examples/16csv.php index 9f940518..8a197178 100644 --- a/Examples/16csv.php +++ b/Examples/16csv.php @@ -38,7 +38,7 @@ date_default_timezone_set('Europe/London'); include "05featuredemo.inc.php"; /** PHPExcel_IOFactory */ -require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php'; +require_once '../Classes/PHPExcel/IOFactory.php'; echo date('H:i:s') , " Write to CSV format" , EOL; @@ -85,6 +85,20 @@ echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL; +echo date('H:i:s') , " Write to CSV format" , EOL; +$callStartTime = microtime(true); + +$objWriterCSV = PHPExcel_IOFactory::createWriter($objPHPExcelFromCSV, 'CSV'); +$objWriterCSV->setExcelCompatibility(true); +$objWriterCSV->save(str_replace('.php', '_excel.csv', __FILE__)); +$callEndTime = microtime(true); +$callTime = $callEndTime - $callStartTime; +echo date('H:i:s') , " File written to " , str_replace('.php', '_excel.csv', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; +echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL; +// Echo memory usage +echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL; + + // Echo memory peak usage echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;