html表格为CSV或Excel

html表格为CSV或Excel

问题描述:

Alright, I'm starting to go a little crazy...

jQuery Table to CSV export

I'm looking at that thread.

It does work, but the output does everything by each LINE not by the HTML table rows. For example, I have an address that is in one HTML cell:

1234 Berryman Lane
Atlanta, GA 12345
Unit # 54A

That will be THREE rows when it outputs to excel, instead of one cell, with returns in it.

Further, there's no way to strip out the HTML that is inside of the HTML cells with that solution, as far as I know...

Finally, Excel gives a warning when opening that file.


What I'm getting at, is that I'd rather just have something that can take the inner most data in the HTML cells (not including HTML inside of a cell), and rip it to CSV. Is there anything that does this well these days?


UPDATE

Well, I just found the best thing yet, this is pretty perfect:

$table = 'myTable';
$file = 'exportFile';

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= "\"" . $row['Field']."\",";
$i++;
}
}
$csv_output .= "
";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= "\"" . $rowr[$j]."\",";
}
$csv_output .= "
";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;

I think this is my keeper. It goes to a CSV file with no issues, loads in Excel with no issues, it's my dream come true!

好吧,我开始变得有点疯狂...... p>

jQuery Table to CSV export p>

I 正在查看该线程。 p>

它确实有效,但输出通过每个LINE而不是HTML表行完成所有操作。 例如,我有一个位于一个HTML单元格中的地址: p>

  1234 Berryman Lane 
Atlanta,GA 12345 
Unit#54A 
  code>  pre>  
 
 

当它输出到excel而不是一个单元格时会返回三行,其中包含返回值。 p>

此外,没有办法删除HTML 据我所知,在这个解决方案的HTML单元格内部... p>

最后,Excel在打开该文件时会发出警告。 p>


我得到的是,我宁愿只有一些东西能够吸收HTML单元格中最内层的数据(不包括HTML内部) (单元格),并将其翻录为CSV。 这些天有什么能做得好吗? p>


更新 strong> p>

嗯,我 刚刚找到最好的东西,这是非常完美的: p>

  $ table ='myTable'; 
 $ file ='exportFile'; 
 
 $ result = mysql_query  (“SHOW COLUMNS FROM”。$ table。“”); 
 $ i = 0; 
if(mysql_num_rows($ result)&gt; 0){
while($ row = mysql_fetch_assoc($ result)){
 $  csv_output。=“\”“。​​$ row ['Field']。”\“,”; 
 $ i ++; 
} 
} 
 $ csv_output。=“
”; 
 
 $ values  = mysql_query(“SELECT * FROM”。$ table。“”); 
while($ rowr = mysql_fetch_row($ values)){
for($ j = 0; $ j&lt; $ i; $ j ++){
 $  csv_output。=“\”“。​​$ rowr [$ j]。”\“,”; 
} 
 $ csv_output。=“
”; 
} 
 
 $ filename = $ file。“_  “.date(”Ym-d_H-i“,time()); 
header(”Content-type:application / vnd.ms-excel“); 
header(”Content-disposition:csv“.date(”Ymd“  “)。”。csv“); 
header(”Content-disposition:filename =“。$ filename。”。csv“); 
print $ csv_output; 
exit; 
  code>  pre> 
  
 

我认为这是我的守护者。 它转到一个没有问题的CSV文件,在Excel中加载没有任何问题,这是我的梦想成真! p> div>

Thanks for all suggestions. As stated in my edited post, this script below was a great solution:

$table = 'myTable';
$file = 'exportFile';

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= "\"" . $row['Field']."\",";
$i++;
}
}
$csv_output .= "
";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= "\"" . $rowr[$j]."\",";
}
$csv_output .= "
";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;

I think this is my keeper. It goes to a CSV file with no issues, loads in Excel with no issues, it's my dream come true!

well, you can always use the same markup in excel as it can render html if you use the .xls file type, render the html table and change the content type to "application/vnd.ms-excel" specify a filename for that response as *.xls and you should have a usable excel sheet.

If there are no dobule quotes in the data, you can wrap the content of each cell in double quotes so your data looks like:

"...","...","..."

Now you can have commas in the data. You may also need to deal with new lines and returns in the data, probably best to remove them completely but that's up to you.