PhpExcel Wrap文本问题

我在将.xlsx文件转换为.csv时遇到问题。 当我尝试转换它时,csv文件出现与文本包装激活。 问题是,我需要将csv文件导入数据库,目前的格式不允许我这样做。 我有问题,如何在保存csv文件时禁用文本换行,或者在将文件导入数据库时​​忽略文本换行?

兑换:

require_once 'Classes/PHPExcel.php'; // (this should include the autoloader) require_once 'Classes/PHPExcel/IOFactory.php'; $excel_readers = array( 'Excel5' , 'Excel2003XML' , 'Excel2007' ); $reader = PHPExcel_IOFactory::createReader('Excel2007'); $reader->setReadDataOnly(true); $path = 'temp.xlsx'; $excel = $reader->load($path); $writer = PHPExcel_IOFactory::createWriter($excel, 'CSV'); $writer->setDelimiter(','); $writer->save('temp.csv'); 

导入数据库:

 if (file_exists('temp.csv')) { $i=0; require "connection.php"; $handle = fopen("temp.csv", "r"); $import=$db->prepare("INSERT INTO adherence( dateandtime, lastname, firstname, paidtime, approvedtime, notadhering) VALUES( ?,?,?,?,?,?)"); while (($data = fgetcsv($handle, 1000, ',', "'")) !== FALSE) { if($i>0) { $data = str_replace('"', '', $data); $myDate = date("Y/m/d",strtotime(str_replace('/','-',$data[0]))); $import->bindParam(1, $myDate, PDO::PARAM_STR); $import->bindParam(2, $data[1], PDO::PARAM_STR); $import->bindParam(3, $data[2], PDO::PARAM_STR); $import->bindParam(4, $data[3], PDO::PARAM_STR); $import->bindParam(5, $data[4], PDO::PARAM_STR); $import->bindParam(6, $data[5], PDO::PARAM_STR); $import->execute(); } $i++; } $removal=$db->prepare("delete FROM adherence WHERE approvedtime = '0' OR notadhering IS NULL"); $removal->execute(); fclose($handle); echo 'IMPORTED' ; } 

任何援助将不胜感激!

用excel打开编辑生成的csv文件: 用excel打开生成的csv文件

禁用包装文本后,同一文件 禁用包装文本后,同一文件

我怀疑这是造成问题的“包装文本”,而是单元格中的新行字符。 PHPExcel不提供任何手段来自动删除这些。 最好的select是迭代单元格,用空格replace所有出现的"\n"

编辑

就像是

 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { foreach ($worksheet->getColumnIterator() as $column) { $cellIterator = $column->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(true); foreach ($cellIterator as $cell) { // Convert any rich text cells to plain text if ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_INLINE) { $cell->setValueExplicit($cell->getValue()->getPlainText()); } // Remove any newline characters in string cells if ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING) { $cell->setValue(str_replace("\n", " ", $cell->getValue())); } } } } 

可能有帮助