自己写的导出CSV文件

index.php

 tocsv.php

Connect($host,$dbname,$dbpass,$db); $conn->Execute("set names gb2312"); $sql="select * from *****"; //$lines=mysql_row_nums($sql); $rs=$conn->Execute($sql); //export to csv file $strBuffer_Csv = ""; $strBuffer_Csv .= "fieldid".","; $strBuffer_Csv .= "fieldname".","; $strBuffer_Csv .= "htmltype".","; $strBuffer_Csv .= "datatype".","; $strBuffer_Csv .= "/r/n"; while($row=$rs->FetchRow()) { $strBuffer_Csv .= $row['FIELDID'].","; $strBuffer_Csv .= $row['FIELDNAME'].","; $strBuffer_Csv .= $row['HTMLTYPE'].","; $strBuffer_Csv .= $row['DATATYPE'].","; $strBuffer_Csv .= "/r/n"; } $strBuffer_Csv = mb_convert_encoding( $strBuffer_Csv,"SJIS","utf-8") ; $strCsvName = mb_convert_encoding( "field.csv","SJIS","utf-8") ; header('Content-Type: text/x-csv'); header('Content-Disposition: inline; filename="' . $strCsvName . '"'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); echo $strBuffer_Csv; } } ?>

里面用到了adodb,这个是次要的,主要的部分是,前面的是为链接数据库做准备的:

//export to csv file $strBuffer_Csv = ""; $strBuffer_Csv .= "fieldid".","; $strBuffer_Csv .= "fieldname".","; $strBuffer_Csv .= "htmltype".","; $strBuffer_Csv .= "datatype".","; $strBuffer_Csv .= "/r/n"; while($row=$rs->FetchRow()) { $strBuffer_Csv .= $row['FIELDID'].","; $strBuffer_Csv .= $row['FIELDNAME'].","; $strBuffer_Csv .= $row['HTMLTYPE'].","; $strBuffer_Csv .= $row['DATATYPE'].","; $strBuffer_Csv .= "/r/n"; } $strBuffer_Csv = mb_convert_encoding( $strBuffer_Csv,"SJIS","utf-8") ; $strCsvName = mb_convert_encoding( "field.csv","SJIS","utf-8") ; header('Content-Type: text/x-csv'); header('Content-Disposition: inline; filename="' . $strCsvName . '"'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); echo $strBuffer_Csv; }

 

你可能感兴趣的:(PHP学习开发)