A. php導出excel或csv
<?php
ob_start();
header("<meta http-equiv=\"content-type\" content=\"text/html;charset=uft-8\">");
header("Content-Type: application/vnd.ms-excel");
header("Expires:0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=文件名稱");
$row = $db -> ... ... # 這里可以進行資料庫的操作,記住:ob_start();前不要有輸出
?>
B. php導出csv欄位數據怎麼對應
php導入CSV文件:
$line_number = 0;
$handle = fopen("1.xls","r");
while ($data = fgetcsv ($handle, 100000, ",")) {
if($line_number == 0){
$line_number++;
continue;
}
//z這樣就可以去掉表頭的那一行
for ($i = 0; $i < count($data); $i++) {
$zian = $data[$i];
}
}
2.php導出CSV文件
header( "Cache-Control: public" );
header( "Pragma: public" );
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=txxx.csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();
$header_str = iconv("utf-8",'gbk',"信息id,標題,名稱,電話,QQ,Email,內容,時間\n");
$file_str="";
$mysqli= new mysqli('localhost','root','','test');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql='select * from messages';
$mysqli->query("set names utf8 ;");
$result=$mysqli->query($sql);
if($result){
while ($row = mysqli_fetch_assoc($result)){
$file_str.= $row['id'].','.$row['title'].','.$row['name'].','."'{$row['telephone']}'".','.$row['qq'].','.$row['email'].','.str_ireplace(',',',',$row['content']).','.$row['retime']."\n";
}
}else{
echo "nonono!!!";
}
$file_str= iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;
?>
C. php導出csv文件時,只能在瀏覽器上顯示文件內容,沒有下載提示,更沒有下載的csv文件,幫忙指點啊!
據我的理解,export_csv這個函數的功能就是顯示csv內容。
按照你的說明猜測,你應該是先將資料庫的內容輸出到csv文件里,然後再給出一個指向該文件的下載鏈接就可以了。
export_csv這個函數可以設計成直接用 file 操作函數寫入內容就可以了,因為csv本身是純文本文件,沒有什麼特殊的文件格式。
D. php從資料庫導出csv格式的Excel表格是,欄位本身就有逗號怎麼辦說在欄位上加雙引號,但加在哪
$data[$key]=implode("\t",$data[$key]);
你的程序用這個語句把一行數組轉換為TAB分割的字元串,這樣EXCEL是可以打開的,只是不完全符合CSV的規則(逗號分開),要改為逗號分開,而且每個欄位要用引號,該語句修改為下面這樣既可:
$data[$key]='"' . implode('","',$data[$key]) . '"';
E. 求源碼!PHP導出數據到csv文件
<?php
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "";
$DB_DBName = "DBName";
$DB_TBLName = "DB_TBLName";
$savename = date("YmjHis");
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
mysql_query("Set Names 'gbk'");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=gbk");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
//header("Pragma: no-cache");
$now_date = date("Y-m-j H:i:s");
//$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);
?>
F. php 生成csv文件並提示保存
<?php
//文件名
$filename="test.csv";
//數據(具體的根據需要做處理,如果是從資料庫查詢,原理與此類似,只需設置好寫入格式和數據即可。
$data="測試csv";
//設置header
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');header('Pragma:public');
echo$data;
?>