<?php
$path = 'WEB-INF';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);function utf2gbk($string) //utf2gbk:utf转换gbk函数;返回经过处理的字符串。{
$out = iconv("UTF-8", "gbk", $string);
return $out;
}
$topic_name = $_POST['topic_name'];
$id = mysql_connect("localhost", "root", "123");
mysql_select_db("crm", $id);
$q = "select * from customers_dtf where topic_name='$topic_name'";
$rs = mysql_query($q);
$n = mysql_num_rows($rs);
$shuzu[$n + 1] = array();
$shuzu[0] = array('id', 'topic_name', 'contact', 'name', 'phone', 'address',
'postalcode', 'indust_name', 'economy_id', 'management', 'oper_state',
'data_contact', 'content', 'user_id', 'rating', 'notes', 'state');
$i = 1;
while ($rows = mysql_fetch_array($rs)) {
$id = $rows['id'];
$topic_name = $rows['topic_name'];
$contact = $rows['contact'];
$name = $rows['name'];
$phone = $rows['phone'];
$address = $rows['address'];
$postalcode = $rows['postalcode'];
$indust_name = $rows['indust_name'];
$economy_id = $rows['economy_id'];
$management = $rows['management'];
$oper_state = $rows['oper_state'];
$data_contact = $rows['data_contact'];
$content = $rows['content'];
$user_id = $rows['user_id'];
$rating = $rows['rating'];
$notes = $rows['notes'];
$state = $rows['state']; $id = utf2gbk($id);
$topic_name = utf2gbk($topic_name);
$contact = utf2gbk($contact);
$name = utf2gbk($name);
$phone = utf2gbk($phone);
$address = utf2gbk($address);
$postalcode = utf2gbk($postalcode);
$indust_name = utf2gbk($indust_name);
$economy_id = utf2gbk($economy_id);
$management = utf2gbk($management);
$oper_state = utf2gbk($oper_state);
$data_contact = utf2gbk($data_contact);
$content = utf2gbk($content);
$user_id = utf2gbk($user_id);
$rating = utf2gbk($rating);
$notes = utf2gbk($notes);
$state = utf2gbk($state); $shuzu[$i] = array($id, $topic_name, $contact, $name, $phone, $address, $postalcode,
$indust_name, $economy_id, $management, $oper_state, $data_contact, $content, $user_id,
$rating, $notes, $state);
$i += 1;
}
require_once 'Spreadsheet/Excel/Writer.php';// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();// sending HTTP headers
$workbook->send('test.xls');// Creating a worksheet
$worksheet = &$workbook->addWorksheet('My first worksheet');
for ($row = 0; $row < $n + 1; $row++) {
for ($col = 0; $col < count($shuzu[0]); $col++) {
$worksheet->writeString($row, $col, $shuzu[$row][$col]); // 在 sheet-1 中写入数据
}
}
$workbook->close(); // 完成下载
?>
结果打开EXECL 时候就出现XLS格式与扩展名字不一致提示
$path = 'WEB-INF';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);function utf2gbk($string) //utf2gbk:utf转换gbk函数;返回经过处理的字符串。{
$out = iconv("UTF-8", "gbk", $string);
return $out;
}
$topic_name = $_POST['topic_name'];
$id = mysql_connect("localhost", "root", "123");
mysql_select_db("crm", $id);
$q = "select * from customers_dtf where topic_name='$topic_name'";
$rs = mysql_query($q);
$n = mysql_num_rows($rs);
$shuzu[$n + 1] = array();
$shuzu[0] = array('id', 'topic_name', 'contact', 'name', 'phone', 'address',
'postalcode', 'indust_name', 'economy_id', 'management', 'oper_state',
'data_contact', 'content', 'user_id', 'rating', 'notes', 'state');
$i = 1;
while ($rows = mysql_fetch_array($rs)) {
$id = $rows['id'];
$topic_name = $rows['topic_name'];
$contact = $rows['contact'];
$name = $rows['name'];
$phone = $rows['phone'];
$address = $rows['address'];
$postalcode = $rows['postalcode'];
$indust_name = $rows['indust_name'];
$economy_id = $rows['economy_id'];
$management = $rows['management'];
$oper_state = $rows['oper_state'];
$data_contact = $rows['data_contact'];
$content = $rows['content'];
$user_id = $rows['user_id'];
$rating = $rows['rating'];
$notes = $rows['notes'];
$state = $rows['state']; $id = utf2gbk($id);
$topic_name = utf2gbk($topic_name);
$contact = utf2gbk($contact);
$name = utf2gbk($name);
$phone = utf2gbk($phone);
$address = utf2gbk($address);
$postalcode = utf2gbk($postalcode);
$indust_name = utf2gbk($indust_name);
$economy_id = utf2gbk($economy_id);
$management = utf2gbk($management);
$oper_state = utf2gbk($oper_state);
$data_contact = utf2gbk($data_contact);
$content = utf2gbk($content);
$user_id = utf2gbk($user_id);
$rating = utf2gbk($rating);
$notes = utf2gbk($notes);
$state = utf2gbk($state); $shuzu[$i] = array($id, $topic_name, $contact, $name, $phone, $address, $postalcode,
$indust_name, $economy_id, $management, $oper_state, $data_contact, $content, $user_id,
$rating, $notes, $state);
$i += 1;
}
require_once 'Spreadsheet/Excel/Writer.php';// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();// sending HTTP headers
$workbook->send('test.xls');// Creating a worksheet
$worksheet = &$workbook->addWorksheet('My first worksheet');
for ($row = 0; $row < $n + 1; $row++) {
for ($col = 0; $col < count($shuzu[0]); $col++) {
$worksheet->writeString($row, $col, $shuzu[$row][$col]); // 在 sheet-1 中写入数据
}
}
$workbook->close(); // 完成下载
?>
结果打开EXECL 时候就出现XLS格式与扩展名字不一致提示
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "";
$DB_DBName = "test";
$DB_TBLName = "test";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date('Y-m-d H:i');
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$title\n");
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
$i++;
}
return (true);
?>
加上三行代码
mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");