php数据库操作带中文,php导出Excel中文,带mysql数据库操作

本文介绍了如何在PHP项目中通过设置HTMLExcel的命名空间、页面字符集、数据库字符集以及正确编写SQL查询,来避免中文数据在Excel文件中的乱码问题。详细步骤包括配置数据库连接、使用utf8编码和实例代码展示。
摘要由CSDN通过智能技术生成

最近在做一个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);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值