最近在做一个php的项目,涉及到导出excel文件功能,其中关于中文乱码问题,网上收集了一下相关资料,自己做个要点记录:
1.采用phpMyAdmin的做法.用HTMLExcel
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
2. 页面的字符集定义成UTF-8:
3..当数据库数据也存在中文,导出时,设置数据库字符集为utf8:
mysql_query("SET NAMES 'utf8'");
4. 具体实例:
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
require_once("../config.inc.php");
require_once("../pass.php");
require_once("../include/search_public.php");
$project_index = $_GET["project_index"];
$start_time = $_GET["start_time"];
$end_time = $_GET["end_time"];
$generate_time = date("YmdHis");
$show_time = "";
if ($start_time == "" && $end_time == "") {
$show_time = "全部";
} else if ($start_time != "" && $end_time == "") {
$show_time = $start_time." ~ 至今";
} else if ($start_time == "" && $end_time != "") {
$show_time = "开始 ~ ".$end_time;
} else if ($start_time != "" && $end_time != "") {
$show_time = $start_time." ~ ".$end_time;
}
/*
* Created on 2012-2-26
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header( "Content-type:application/vnd.ms-excel" );
header( "Content-Disposition:attachment;filename=ordered_history_".$generate_time.".xls" );
header("Pragma: no-cache");
header("Expires: 0");
$config = array();
$config = @parse_ini_file('../config/config.ini');
if(empty($config)) {
$config = @parse_ini_file('config/config.ini');
}
$dbhost = $config['db.config.host'] . ':' . $config['db.config.port'];
$dbuser = $config['db.config.username']; //用户名
$dbpass = $config['db.config.password']; //密码
$dbname = $config['db.config.dbname']; //数据库名
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
mysql_query("SET NAMES 'utf8'");
$sql="select c.menu_name, c.restaurant_name, c.price, b.account_name, b.account_email, a.*, d.project_name from ordered_history_t a, account_t b, menu_t c, project_t d where a.project_index=d.project_index and a.menu_index = c.menu_index and a.account_id = b.account_id";
add_to_sql_for_equal($sql, "a.project_index", $project_index);
add_to_sql_for_between_str($sql, "a.order_time", $start_time, $end_time);
$sql = $sql." order by a.order_time DESC;";
$result=mysql_query($sql);
$query_sum = "SELECT SUM(c.price) as price_sum, count(*) as order_count from ordered_history_t a, account_t b, menu_t c where a.menu_index = c.menu_index and a.account_id = b.account_id";
add_to_sql_for_equal($query_sum, "a.project_index", $project_index);
add_to_sql_for_between_str($query_sum, "a.order_time", $start_time, $end_time);
$result2 = mysql_query($query_sum, $conn);
$price_count = 0;
$order_count = 0;
if($result2 && mysql_num_rows($result2) > 0) {
while($row2=mysql_fetch_array($result2)) {
if ($row2['price_sum'] != null) {
$price_count = $row2['price_sum'];
$order_count = $row2['order_count'];
}
}
}
?>
订餐数据统计: | |
订餐时间: | <?php echo $show_time;?> |
订餐金额总计: | <?php echo $price_count;?>元 |
订餐记录数: | 共<?php echo $order_count;?>条 |
订餐详情列表: | |||||
订餐人 | 加班项目 | 菜谱名 | 价格 | 订餐人邮件 | 订餐时间 |
---|
while($row =mysql_fetch_array($result)){
?>
<?php echo $row['account_name'];?>if ($row['project_index'] == 1) {
?>
<?php echo $row['project_name']."(".$row['comments'].")";?>} else {
?>
<?php echo $row['project_name'];?>}
?>
<?php echo $row['menu_name'];?><?php echo $row['price'];?>元<?php echo $row['account_email'];?><?php echo $row['order_time'];?>}
mysql_close($conn);
?>