在SO中我的导出功能参考了一些话题,但是有中文问题。在我将数据从DB导出到csv之后我用我的朋友pc(os win XP,MS excel 2007)打开这台电脑我可以显示中文字符而没有任何问题(在MS excel中)。
但是当我用我的电脑打开(os win 7,MS excel 2007)时,它无法显示中文措辞的正确字符。以下是我的输出和完整代码。
原始数据:美不勝收價
,导出后:ÃÀ²»RÝÊÕPù
创建此导出功能是为了让我的客户将所有产品数据导出到csv。
<?php
//Set maximum memory limit
ini_set('memory_limit', '50M');
//Load
require('includes/application_top.php');
//Var
$product_id = mysql_real_escape_string($_POST['product_id']);
$file = 'product_export';
//Language
$language_array = array(
'1' => '1',
'2' => '4',
'3' => '6'
);
//Chinese CODE
$chinese_encoding = "GB2312"; //GB2312
if(isset($product_id)) {
//We need to escape comma and double quote
function escape_csv_value($value) {
$value = str_replace('"', '""', $value); // First off escape all " and make them ""
if(preg_match('/,/', $value) or preg_match("/\n/", $value) or preg_match('/"/', $value)) { // Check if I have any commas or new lines
return '"'.$value.'"'; // If I have new lines or commas escape them
} else {
return $value; // If no new lines or commas just return the value
}
}
//CSV Header
$data = "";
$row_output = array();
$row_output[] = "Product Code";
$row_output[] = "Barcode";
$row_output[] = "Name (EN)";
$row_output[] = "Name (CN)";
$row_output[] = "Name (BM)";
$row_output[] = "Model";
$row_output[] = "Cost";
$row_output[] = "Price";
$row_output[] = "Quantity";
$row_output[] = "Weight";
$row_output[] = "Keyword";
$row_output[] = "Image Url";
$row_output[] = "Descrription (EN)";
$row_output[] = "Descrription (CN)";
$row_output[] = "Descrription (BM)";
$row_output[] = "Screenshot 1";
$row_output[] = "Screenshot 2";
$row_output[] = "Screenshot 3";
$row_output[] = "Screenshot 4";
$row_output[] = "Screenshot 5";
$row_output[] = "Screenshot 6";
$row_output[] = "Screenshot 7";
$row_output[] = "Screenshot 8";
$row_output[] = "Manufacturer Name";
$row_output[] = "Product ID";
$data .= join(',', $row_output)."\n"; // Join all values without any trailing commas and add a new line
//$values = tep_db_query("SELECT * FROM `products` WHERE products_id IN ( ".$product_id." )");
$values = tep_db_query("SELECT * FROM `products` WHERE products_id='151' ");
$row_output = array();
while ($rowr = tep_db_fetch_array($values)) {
$row_output[] = escape_csv_value($rowr["products_code"]);
$row_output[] = escape_csv_value($rowr["products_bar_code"]);
//Name
foreach ($language_array as $lang) {
if ($lang == '1') {
$description1 = tep_db_query("SELECT * FROM `products_description` WHERE products_id = '" . $rowr["products_id"] . "' and language_id='1'");
$row_description1 = tep_db_fetch_array($description1);
if (preg_match("/\p{Han}+/u", $row_description1["products_name"])) {
$products_name_UTF_1 = mb_convert_encoding(escape_csv_value($row_description1["products_name"]), $chinese_encoding, "UTF-8");
}
else {
$products_name_UTF_1 = mb_convert_encoding(escape_csv_value($row_description1["products_name"]), $chinese_encoding, "HTML-ENTITIES");
}
$row_output[] = escape_csv_value($products_name_UTF_1);
}
elseif ($lang == '6') {
$description6 = tep_db_query("SELECT * FROM `products_description` WHERE products_id = '" . $rowr["products_id"] . "' and language_id='6'");
$row_description6 = tep_db_fetch_array($description6);
if (preg_match("/\p{Han}+/u", $row_description6["products_name"])) {
$products_name_UTF_6 = mb_convert_encoding(escape_csv_value($row_description6["products_name"]), $chinese_encoding, "UTF-8");
}
else {
$products_name_UTF_6 = mb_convert_encoding(escape_csv_value($row_description6["products_name"]), $chinese_encoding, "HTML-ENTITIES");
}
$row_output[] = escape_csv_value($products_name_UTF_6);
}
elseif ($lang == '4') {
$description4 = tep_db_query("SELECT * FROM `products_description` WHERE products_id = '" . $rowr["products_id"] . "' and language_id='4'");
$row_description4 = tep_db_fetch_array($description4);
if (preg_match("/\p{Han}+/u", $row_description4["products_name"])) {
$products_name_UTF_4 = mb_convert_encoding(escape_csv_value($row_description4["products_name"]), $chinese_encoding, "UTF-8");
}
else {
$products_name_UTF_4 = mb_convert_encoding(escape_csv_value($row_description4["products_name"]), $chinese_encoding, "HTML-ENTITIES");
}
$row_output[] = escape_csv_value($products_name_UTF_4);
}
}
if($rowr["products_model"]) {
$row_output[] = escape_csv_value($rowr["products_model"]);
}
else {
$row_output[] = " ";
}
if($rowr["products_cost"]) {
$row_output[] = escape_csv_value($rowr["products_cost"]);
}
else {
$row_output[] = " ";
}
if($rowr["products_price"]) {
$row_output[] = escape_csv_value($rowr["products_price"]);
}
else {
$row_output[] = " ";
}
if($rowr["products_quantity"]) {
$row_output[] = escape_csv_value($rowr["products_quantity"]);
}
else {
$row_output[] = " ";
}
if($rowr["products_weight"]) {
$row_output[] = escape_csv_value($rowr["products_weight"]);
}
else {
$row_output[] = " ";
}
if($rowr["products_keyword"]) {
if (preg_match("/\p{Han}+/u", $rowr["products_keyword"])) {
$products_keyword_UTF = mb_convert_encoding(escape_csv_value($rowr["products_keyword"]), $chinese_encoding, "UTF-8");
//this is chinese
} else {
$products_keyword_UTF = mb_convert_encoding(escape_csv_value($rowr["products_keyword"]), "HTML-ENTITIES", "UTF-8");
//this is UTF8
}
}
else {
$products_keyword_UTF = " ";
}
$row_output[] = escape_csv_value($products_keyword_UTF);
$row_output[] = escape_csv_value($rowr["products_image"]);
//Description
foreach ($language_array as $lang) {
if ($lang == '1') {
$description1 = tep_db_query("SELECT * FROM `products_description` WHERE products_id = '" . $rowr["products_id"] . "' and language_id='1'");
$row_description1 = tep_db_fetch_array($description1);
if (preg_match("/\p{Han}+/u", $row_description1["products_description"])) {
$products_description_UTF_1 = mb_convert_encoding($row_description1["products_description"], $chinese_encoding, "UTF-8");
}
else {
$products_description_UTF_1 = mb_convert_encoding($row_description1["products_description"], $chinese_encoding, "HTML-ENTITIES");
}
if ($products_description_UTF_1) {
$row_output[] = escape_csv_value($products_description_UTF_1);
}
else {
$row_output[] = " ";
}
}
elseif ($lang == '6') {
$description6 = tep_db_query("SELECT * FROM `products_description` WHERE products_id = '" . $rowr["products_id"] . "' and language_id='6'");
$row_description6 = tep_db_fetch_array($description6);
if (preg_match("/\p{Han}+/u", $row_description6["products_description"])) {
$products_description_UTF_6 = mb_convert_encoding($row_description6["products_description"], $chinese_encoding, "UTF-8");
}
else {
$products_description_UTF_6 = mb_convert_encoding($row_description6["products_description"], $chinese_encoding, "HTML-ENTITIES");
}
if ($products_description_UTF_6) {
$row_output[] = escape_csv_value($products_description_UTF_6);
}
else {
$lang6 = "1";
}
}
elseif ($lang == '4') {
$description4 = tep_db_query("SELECT * FROM `products_description` WHERE products_id = '" . $rowr["products_id"] . "' and language_id='4'");
$row_description4 = tep_db_fetch_array($description4);
if (preg_match("/\p{Han}+/u", $row_description4["products_description"])) {
$products_description_UTF_4 = mb_convert_encoding($row_description4["products_description"], $chinese_encoding, "UTF-8");
}
else {
$products_description_UTF_4 = mb_convert_encoding($row_description4["products_description"], $chinese_encoding, "HTML-ENTITIES");
}
if ($products_description_UTF_4) {
$row_output[] = escape_csv_value($products_description_UTF_4);
}
else {
$lang4 = "1";
}
}
}
if ($lang6 == '1') {
$row_output[] = " ";
}
elseif ($lang4 == '1') {
$row_output[] = " ";
}
//Screenshot
$query_screenshot = tep_db_query("SELECT * FROM `products_screenshot` WHERE products_id = '" . $rowr["products_id"] . "' and main='0' LIMIT 8");
$ss_i = 0;
while ($row_ss = tep_db_fetch_array($query_screenshot)) {
$row_output[] = escape_csv_value($row_ss['thumbnail']);
if(escape_csv_value($row_ss['thumbnail'])) {
$ss_i ++;
}
}
//Fix if screenshot no value we create dummy column for this
$ss_i_total = 8 - $ss_i;
for ($i=1; $i<=$ss_i_total; $i++)
{
$row_output[] = " ";
}
//Manufacturer
$query_get_manufacturers_id = tep_db_query("SELECT * from `manufacturers` WHERE manufacturers_id='" . $rowr["manufacturers_id"] . "' ");
$manufacturers_id_result = tep_db_fetch_array($query_get_manufacturers_id);
$row_output[] = escape_csv_value($manufacturers_id_result["manufacturers_name"]);
$row_output[] = escape_csv_value($rowr["products_id"]);
$data .= join(',', $row_output)."\n";
$row_output = ''; // Clear the contents of the $row variable to start a new row
}
}
$filename = $file . "_" . date("d-m-Y__H-i-A");
header("Content-type: text/csv; charset=UTF-8");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=" . $filename . ".csv");
print $data;
exit;
?>