使用 PHP 和 PHP 将 HTML 导出到 EXCEL jQuery

发布于 2024-10-07 01:13:09 字数 636 浏览 1 评论 0原文


我想将 HTML table 导出到 MS Excel 文档(最好是 PDF),因为它显示在 HTML/CSS 页面上。我读了很多关于它的页面和主题(主要是在堆栈上),但每个人似乎都在谈论导出表格,而不是格式化最终的 Excel 文件。

我认为它应该像这样工作:

  1. 单击导出按钮:调用 jQuery
  2. jQuery 从 table 的 HTML/CSS 创建一个纯 HTML 文档
  3. jQuery 调用 PHP 函数/类 (http://phpexcel.codeplex.com 也许)从重新格式化的 HTML 代码生成 EXCEL 文件
  4. 使用 PHP 发送的标头,浏览器要求用户保存/打开 Excel 生成的文件

这可行吗?如果是的话,你知道有什么 jQuery 插件和 PHP 类/函数可以做到这一点吗?如果没有,您对此有何想法?
编辑:感谢 Matt,我想到了使用 XLS 文件模板,这可能吗?它将避免我在模板文件中完成的所有格式设置。是否可以?


干杯,
尼古拉斯.

I would like to export an HTML table to a MS Excel document (and PDF ideally) as it is displayed on the HTML/CSS page. I read a lot of pages and topics about it (on stack mainly) but everyone seemed to be talking about exporting the table, and not formatting the final excel file.

I think it should work like this:

  1. click on export button: call to jQuery
  2. jQuery creates a pure HTML document from the HTML/CSS of the table
  3. jQuery calls a PHP function/class (http://phpexcel.codeplex.com maybe) to generate the EXCEL file from the HTML code reformatted
  4. With the headers sent by PHP, the browser asks the user to save/open the excel generated file

Would this work? If so do you know any jQuery plugins and PHP classes/functions to do so? If not, what is your ideas about it?
EDIT: Thanks to Matt, I came to the idea of using an XLS file template, is it possible? It will avoid me all the formatting as it would be done in the template file. Is it possible?

Cheers,
Nicolas.

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

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

发布评论

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

评论(3

贵在坚持 2024-10-14 01:13:09

听起来您希望 PHP 文件屏幕抓取 jQuery 生成的页面?这并非不可能(您可以使用 cUrl 函数执行此操作,请查看 这篇文章)但是,如果您可以通过任何其他方式获取表数据,那就更好了。表数据是如何产生的?该表是否正在由用户编辑?除非用户添加内容/样式,否则您应该能够从其他来源提取数据,而且会更容易。

首先,您应该知道在 PHP 中生成 Excel 文件主要有两种方法:

  1. 生成 CSV
  2. 生成 XML 兼容文档以供 Excel 使用(通过 PHPExcel 或类似方法)
    库)

选项 1 不提供任何样式,仅提供数据。

选项 2 可以添加样式(尽管由于 Excel 繁琐的格式,它非常混乱)。此外,您无法保证样式将转换为其他应用程序(OO.org、StarOffice、Lotus、WP Office 等) - XML 将无法一致地呈现,并且您的文档样式可能会丢失或降级。

如果你必须求助于屏幕抓取,那么获取 CSS 样式将是最困难的部分。查看 这篇文章了解有关如何开始的一些想法。

祝你好运!

It sounds like you want the PHP file to screen scrape the jQuery generated page? This is not impossible (you can do so with the cUrl functions, take a look at this article) however if you can obtain the table data in any other way, it would be preferable. How is the table data originated? Is the table being edited by the user? Unless the user is adding content/styles, you should be able to pull the data from another source, and it will be easier.

First you should know there are primarily two methods to generate an Excel file in PHP:

  1. generate a CSV
  2. generate an XML-compliant document for Excel consumption (via PHPExcel or similar
    library)

Option 1 offers no styles whatsoever, just data.

Option 2 can add styles (though it's terribly obfuscated due to Excel's cumbersome format). Additionally, you have no guarantee the styles will translate to other applications (OO.org, StarOffice, Lotus, WP Office, etc.) - the XML will not be rendered consistently and your document styles will likely be lost or degraded.

If you have to resort to screen scraping, getting the css styles is going to be the most difficult part. Take a look at some of the solutions offered in this article for some ideas on how to get started.

Good luck!

紫瑟鸿黎 2024-10-14 01:13:09

这可能有助于实际的 Excel 电子表格编写... http ://www.appservnetwork.com/modules.php?name=News&file=article&sid=8

我以前使用过它,但没有添加任何其他格式。我想这是可能的,不知何故......

This might help with the actual Excel spreadsheet writing... http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8

I've used it before, but haven't added any additional formatting to it. I imagine it's possible, somehow...

心舞飞扬 2024-10-14 01:13:09

我最终选择用PHPexcel重做所有格式。缺点是,如果我更改呈现的 HTML 中的某些内容,我必须在我创建的 PHPexcel 帮助器中执行相同的操作,反之亦然。

谢谢大家的建议。
尼古拉斯.

I finally choose to redo all the formatting with PHPexcel. The drawback is that if I change something in the HTML rendered I have to do the same in the PHPexcel helper I've created and vice versa.

Thank you for your suggestion everyone.
Nicolas.

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