导出为ex​​cel或csv

我在网站上工作,我需要做一个Excel文件或至less一个Excel文件打开的CSV文件。 我尝试了很多东西。 我search谷歌,stackoverflow等解决scheme,但我发现似乎不是为我工作。 我试过jQuery解决scheme,我尝试了PHP解决scheme。

JSFiddle: http : //jsfiddle.net/terryyounghk/KPEGU/

当我用这个小提琴制作一个csv的输出是:

csv输出的例子

我希望输出是在单独的colums中,而不是一个。

这里也是我试过的PHP解决scheme:

header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment;Filename=document_name.xls"); echo "<html>"; echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">"; echo "<body>"; echo "<table>"; echo "<tr>"; echo "<td>Henk</td>"; echo "<td>Henk2</td>"; echo "</tr>"; echo "<tr>"; echo "<td>bob</td>"; echo "<td>bob2</td>"; echo "</tr>"; echo "</table>"; echo "</body>"; echo "</html>"; 

这输出:

php输出的例子

这里的输出是在正确的列和行。 但不知何故,我不能得到网格线。

这只是我试过的吨的两个例子。 有人知道如何解决这个问题吗?

在你提供的JsFiddle示例中,将colDelim = '","'更改为colDelim = '";"'因为它需要逗号分隔值; 使string落在不同的单元格中。

更新了JsFiddle

我可以看到你想要使用表进行格式化,但CSV是用逗号分隔的。

我发现的最好的方法是使用这样的数组:

 // setup array. Note null values to create empty cells $list = array( array('First row', NULL, 'some_data', NULL), array('Second row', 'some data', NULL, 'some data'), ); // set header types header("Content-type: text/csv"); header("Cache-Control: no-store, no-cache"); header('Content-Disposition: attachment; filename="most_popular-'.$_POST['start_date'].'-'.$_POST['end_date'].'.csv"'); // tell php to open the page as an output (download) $outstream = fopen("php://output",'w'); // loop through and put each array row into the csv, comma seperated. foreach( $list as $row ) { fputcsv($outstream, $row, ','); } exit; 

只要记住,数组中的每一行都需要与其他行完全相同,否则生成csv的循环会变得非常困惑。

 **Try this** <?php error_reporting(E_ERROR); include("dbConnect.inc.php"); //$ip = $_SERVER['HTTP_CLIENT_IP $select_table=mysql_query("SELECT * FROM `key_cat`"); $setExcelName="Keyword_reports"; header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename='.$setExcelName.'.csv'); /* $setExcelName="Keywords"; header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=".$setExcelName."_Reoprt.xls"); header("Pragma: no-cache"); header("Expires: 0"); */ $rows = mysql_fetch_assoc($select_table); if($rows) { getcsv(array_keys($rows)); } while($rows) { getcsv($rows); $rows = mysql_fetch_assoc($select_table); } // get total number of fields present in the database function getcsv($no_of_field_names) { $separate = ''; // do the action for all field names as field name foreach ($no_of_field_names as $field_name) { if (preg_match('/\\r|\\n|,|"/', $field_name)) { $field_name = '' . str_replace('', $field_name) . ''; } echo $separate . $field_name; //sepearte with the comma $separate = ','; } //make new row and line echo "\r\n"; } ?>