doumangzhen7204 2011-05-22 22:20
浏览 169
已采纳

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!

  • 写回答

3条回答 默认 最新

  • dsjbest2014 2011-10-01 00:14
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测