头像

解决导出csv中文乱码两个方法

来源:https://www.sucaihuo.com/topic/2354.html 素材火管理员 2021-04-17 08:33浏览(64)

function exportCsv()
{
// 需要导出的内容
$data = [
[\'name\' => \'张三\', \'score\' => \'80\'],
[\'name\' => \'李四\', \'score\' => \'90\'],
[\'name\' => \'王五\', \'score\' => \'60\'],
];
// 文件名,这里都要将utf-8编码转为gbk,要不可能出现乱码现象
$filename = utfToGbk(\'导出csv文件.csv\');

// 拼接文件信息,这里注意两点
// 1、字段与字段之间用逗号分隔开
// 2、行与行之间需要换行符
$fileData = utfToGbk(\'姓名, 分数\') . "\\n";
foreach ($data as $value) {
$temp = $value[\'name\'] . \',\' .
$value[\'score\'];
$fileData .= utfToGbk($temp) . "\\n";
}

// 头信息设置
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=" . $filename);
header(\'Cache-Control:must-revalidate,post-check=0,pre-check=0\');
header(\'Expires:0\');
header(\'Pragma:public\');
echo $fileData;
exit;
}


/**
* 字符转换(utf-8 => GBK)
*/
function utfToGbk($data)
{
return iconv(\'utf-8\', \'GBK\', $data);
}
exportCsv();

第二个方法 多列导出CSV数据

function fopen_utf8($filename) {
$encoding = \'\';
$handle = fopen($filename, \'r\');
$bom = fread($handle, 2);
// fclose($handle);
rewind($handle);

if ($bom === chr(0xff) . chr(0xfe) || $bom === chr(0xfe) . chr(0xff)) {
// UTF16 Byte Order Mark present
$encoding = \'UTF-16\';
} else {
$file_sample = fread($handle, 1000) + \'e\'; //read first 1000 bytes
// + e is a workaround for mb_string bug
rewind($handle);

$encoding = mb_detect_encoding($file_sample, \'UTF-8, UTF-7, ASCII, EUC-JP,SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP\');
}
if ($encoding) {
stream_filter_append($handle, \'convert.iconv.\' . $encoding . \'/UTF-8\');
}
return ($handle);
}

$file = \'1.csv\';
if (($handle = fopen_utf8($file, "r")) === FALSE)
return;
while (($cols = fgetcsv($handle, 1000, "\\t")) !== FALSE) {
//echo $cols[0];
// echo "<pre>";
// print_r($cols);
// echo "</pre>";
$arr[] = $cols;
}
echo "<pre>";
print_r($arr);
echo "</pre>";

淘宝导出CSV,已亲测

function totaobao() {

//加载所选商品
// $map[\'goods_id\'] = array(\'in\', $this->returnid());
// $result = $this->showgoods($map);
//写入CSV并保存数据
$header = iconv(\'UTF-8\', \'GB2312\', "宝贝名称,宝贝类目,店铺类目,新旧程度,省,城市,出售方式,宝贝价格,加价幅度,宝贝数量,有效期,运费承担,平邮,EMS,快递,付款方式,支付宝,发票,保修,自动重发,放入仓库,橱窗推荐,开始时间,心情故事,宝贝描述,宝贝图片,宝贝属性,团购价,最小团购件数,邮费模版ID,会员打折,修改时间,上传状态,图片状态,返点比例,新图片,视频,销售属性组合,用户输入ID串,用户输入名-值对,商家编码,销售属性别名,代充类型,宝贝编号");

header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=ybkgoods.csv");
header(\'Cache-Control:must-revalidate,post-check=0,pre-check=0\');
header(\'Expires:0\');
header(\'Pragma:public\');
echo "$header\\n";
$result = array(
0 => array(
"goods_name" => "素材火sucaihuo.com" . date("Y-m-d"),
"shop_price" => "100",
"goods_number" => 200,
"goods_thumb" => "https://img.alicdn.com/imgextra/i4/731815187/O1CN01pcS4Qn1oBh46d6wYO_!!731815187.png"
)
);

//输出数据体
foreach ($result as $value) {
//过滤描述字段
// $desc = csvdatafilter("www.yibaikuan.com", $value[\'goods_desc\']);
//截取图片文件名
$desc = "素材火www.sucaihuo.com";
// $img = msubstr($value[\'goods_thumb\'], 24, 25);
$img = $value[\'goods_thumb\'];
//转义
$str = $value[\'goods_name\'] . ",,,0,懒人之家,金华,b," . $value[\'shop_price\'] . ",0," . $value[\'goods_number\'] . ",14,1,20,20,10,,,0,0,1,0,1,," . $img . "," . $desc . "," . $img . ",,,,0,0,,100,,0," . $img . ":0:0:|;,,,,,,,0,";

$arr = explode(",", $str);
$pic_json = \'7d5308b928dccd695c1bd9b0753231e8:1:0:|https://img.alicdn.com/bao/uploaded/i4/731815187/O1CN01pcS4Qn1oBh46d6wYO_!!731815187.png;\';
$arr[35] = $pic_json;
$arr[23] = $pic_json;
$arr[25] = $pic_json;
// echo "<pre>";
// print_r($arr);
// echo "</pre>";
$str = implode(",", $arr);
$body = iconv(\'UTF-8\', \'GB2312\', $str);
echo "$body\\n";
}
}

totaobao();




评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2