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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝