html table to CSV or 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 .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j+开发者_如何学Python+) {
$csv_output .= "\"" . $rowr[$j]."\",";
}
$csv_output .= "\n";
}
$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.
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 .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= "\"" . $rowr[$j]."\",";
}
$csv_output .= "\n";
}
$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!
精彩评论