html 表格转为 CSV 或 Excel

发布于 2024-11-08 16:55:57 字数 1355 浏览 4 评论 0原文

好吧,我开始有点疯狂了...

jQuery 表到 CSV 导出

我正在看那条线索。

它确实有效,但输出是按每行而不是 HTML 表行执行所有操作。例如,我有一个位于一个 HTML 单元格中的地址:

1234 Berryman Lane
Atlanta, GA 12345
Unit # 54A

当它输出到 Excel 时,这将是三行,而不是一个单元格,其中包含返回值。

此外,据我所知,没有办法用该解决方案去除 HTML 单元格内部的 HTML...

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


我的意思是,我宁愿拥有一些可以获取 HTML 单元格中最内部数据(不包括单元格内部的 HTML)的东西,并将其转换为 CSV。现在有什么东西能做到这一点吗?


更新

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

$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;

我认为这是我的守门员。它可以毫无问题地转到 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 .= "\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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱你一生 2024-11-15 16:55:58

感谢您的所有建议。正如我编辑的帖子中所述,下面的脚本是一个很好的解决方案:

$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;

我认为这是我的守护者。它可以毫无问题地转到 CSV 文件,加载到 Excel 中也没有任何问题,这是我的梦想成真!

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!

还给你自由 2024-11-15 16:55:57

好吧,您始终可以在 excel 中使用相同的标记,因为如果您使用 .xls 文件类型,它可以呈现 html,呈现 html 表并将内容类型更改为“application/vnd.ms-excel”,并为该响应指定一个文件名作为 *.xls,您应该有一个可用的 Excel 工作表。

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.

哑剧 2024-11-15 16:55:57

如果数据中没有双引号,您可以将每个单元格的内容用双引号括起来,这样您的数据如下所示:

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

现在您可以在数据中包含逗号。您可能还需要处理数据中的新行和返回,可能最好将它们完全删除,但这取决于您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文