写入 CSV 文件换行字符显示为文本
Writing a CSV File New Line Characters Showing as Text
我正在尝试使用 php 编写 CSV 文件。我已经尝试了 Stack Overflow 上给出的许多示例,但是当我在 Open Office 或 Notepad++ 中打开我的 CSV 文件时,它们都将换行符 \n 显示为文本而不是换行符。例如,数字 id 列表显示为:
id\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n15\n16\n17\n18\n
我尝试在 id 字符串周围加上引号,但结果只是输出:
"id"\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n15\n16\n17\n18\n
如何换行?我也尝试过 \r\n,我尝试添加一行代码来指定编码,这是在另一个 post 中建议的,但它也只是作为文本添加。感谢任何帮助,谢谢!
生成 CSV 字符串的代码:
//format array to be written to CSV
public function data_to_csv($data, $headers = TRUE) {
if ( ! is_array($data)) {
return 'invalid Data provided';
}
$CSVText="";
$array = array();
if ($headers) {
$array2 = get_object_vars($data[0]);
$properties = array_keys($array2);
$CSVText.=implode(',', $properties).'\n';
}
foreach ($data as $row) {
$array = get_object_vars($row);
$properties = array_values($array);
$CSVText.=implode(',',$properties).'\n';
}
return $CSVText;
}
写入文件的代码:
require_once(APPPATH.'libraries/pclzip/pclzip.lib.php');
$rand=random_string('alnum',16);
$created=FALSE;
while (!$created){
if(!is_dir($this->config->item('temp_path').$rand)) {
mkdir($this->config->item('temp_path').$rand,0755);
$dir=$this->config->item('temp_path').$rand;
$created=TRUE;
}
}
$programDataCSV = $this->extractor_model->data_to_csv($programData['data'], true);
$zipPath=$this->config->item('temp_path')."$rand/search_results_data.zip";
$zip= new PclZip($zipPath);
$programPath=$this->config->item('temp_path')."$rand/programs.csv";
if ($programData['resultCount']>0){
//open filestream for program data
$program_handle=fopen($programPath,'a');
if (!$program_handle){
show_error('could not open hand for program data');
}
fwrite($program_handle,$programDataCSV);
fclose($program_handle);
$zip->add($programPath,PCLZIP_OPT_REMOVE_PATH,$this->config->item('temp_path').$rand);
}
$data=file_get_contents($zipPath);
$this->load->helper('download');
$this->load->helper("file");
delete_files($this->config->item('temp_path').$rand);
rmdir($this->config->item('temp_path').$rand);
force_download('search_results_data.zip',$data);
您的 \n
是单引号。试试双引号:"\n".
我正在尝试使用 php 编写 CSV 文件。我已经尝试了 Stack Overflow 上给出的许多示例,但是当我在 Open Office 或 Notepad++ 中打开我的 CSV 文件时,它们都将换行符 \n 显示为文本而不是换行符。例如,数字 id 列表显示为:
id\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n15\n16\n17\n18\n
我尝试在 id 字符串周围加上引号,但结果只是输出:
"id"\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n15\n16\n17\n18\n
如何换行?我也尝试过 \r\n,我尝试添加一行代码来指定编码,这是在另一个 post 中建议的,但它也只是作为文本添加。感谢任何帮助,谢谢!
生成 CSV 字符串的代码:
//format array to be written to CSV
public function data_to_csv($data, $headers = TRUE) {
if ( ! is_array($data)) {
return 'invalid Data provided';
}
$CSVText="";
$array = array();
if ($headers) {
$array2 = get_object_vars($data[0]);
$properties = array_keys($array2);
$CSVText.=implode(',', $properties).'\n';
}
foreach ($data as $row) {
$array = get_object_vars($row);
$properties = array_values($array);
$CSVText.=implode(',',$properties).'\n';
}
return $CSVText;
}
写入文件的代码:
require_once(APPPATH.'libraries/pclzip/pclzip.lib.php');
$rand=random_string('alnum',16);
$created=FALSE;
while (!$created){
if(!is_dir($this->config->item('temp_path').$rand)) {
mkdir($this->config->item('temp_path').$rand,0755);
$dir=$this->config->item('temp_path').$rand;
$created=TRUE;
}
}
$programDataCSV = $this->extractor_model->data_to_csv($programData['data'], true);
$zipPath=$this->config->item('temp_path')."$rand/search_results_data.zip";
$zip= new PclZip($zipPath);
$programPath=$this->config->item('temp_path')."$rand/programs.csv";
if ($programData['resultCount']>0){
//open filestream for program data
$program_handle=fopen($programPath,'a');
if (!$program_handle){
show_error('could not open hand for program data');
}
fwrite($program_handle,$programDataCSV);
fclose($program_handle);
$zip->add($programPath,PCLZIP_OPT_REMOVE_PATH,$this->config->item('temp_path').$rand);
}
$data=file_get_contents($zipPath);
$this->load->helper('download');
$this->load->helper("file");
delete_files($this->config->item('temp_path').$rand);
rmdir($this->config->item('temp_path').$rand);
force_download('search_results_data.zip',$data);
您的 \n
是单引号。试试双引号:"\n".