我正在使用核心 php 代码将数据导出到 excel。以下是我的代码:
header('Content-Type: text/html; charset=utf-8');
mysql_connect("localhost","root","");
mysql_select_db("test");
//----------------------------------------
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
//----------------------------------------
$filename = "excelwork.xls";
$exists = file_exists('excelwork.xls');
if($exists)
{
unlink($filename);
}
$filename = "excelwork.xls";
$fp = fopen($filename, "wb");
$sql = "select * from test";
$result = mysql_query($sql);
$$insert = "";
$insert_rows = "";
for ($i = 1; $i < mysql_num_fields($result); $i++)
{
$insert_rows .= mysql_field_name($result,$i) . "\t";
}
$insert_rows.="\n";
fwrite($fp, $insert_rows);
while($row = mysql_fetch_row($result))
{
$insert = $row[1]. "\t" .$row[2]. "\t".$row[3]. "\t".$row[4]. "\t".$row[5];
$insert .= "\n"; // serialize($assoc)
fwrite($fp, $insert);
}
if (!is_resource($fp))
{
echo "cannot open excel file";
}
echo "success full export";
fclose($fp);
============这是我的Mysql表================
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`status` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `test`
--
INSERT INTO `test` (`id`, `name`, `status`) VALUES
(1, 'सजल', 1),
(2, 'सूर्या', 1),
(3, 'धनश्री', 1),
(4, 'मनीष', 1),
(5, 'राहुल', 1);
输出 excel 文件给出以下输出:
सजल
सूरà¥à¤¯à¤¾
धनशà¥à¤°à¥€
मनीष
राहà¥à¤²
但是当我使用 Phpmyadmin 导出到 xls 时,输出是正确的:
सजल
सूर्या
धनश्री
मनीष
राहुल
请告诉我我的代码有什么问题。我尝试了很多东西,搜索了很多帖子。
谢谢