将HTML转换为Excel RTF,反之亦然 [英] Convert HTML to Excel Rich Text and vice versa

查看:99
本文介绍了将HTML转换为Excel RTF,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHPExcel 在MySQL DB和Excel 2007工作表之间传输数据.它在大多数情况下都能正常工作,但是我遇到了一个问题.

I'm using PHPExcel for transferring data between MySQL DB and Excel 2007 worksheets. It works well on most situations, but I encountered one problem.

数据库中的某些字段包含HTML数据.我需要尽可能地保留Excel单元格中的格式.如我所知,Excel允许在单元格内进行以下格式设置(PHPExcel_RichText类支持所有这些格式):新行[这些行可用于跟踪<p></p>块],字体名称,大小,颜色,粗体,斜体,下划线,删除线,下标,上标.假设这些就足够了,那么我们可以忽略其他HTML格式.

Some of the fields in DB contain HTML data. I need to preserve the formatting in Excel cells as much as possible. As I could figure out, Excel allows the following formatting inside cells (PHPExcel_RichText class supports all of these): new lines [these can be used to track <p></p> blocks], font name, size, color, bold, italic, underline, strikethrough, subscript, superscript. Suppose these are enough, so we can ignore other HTML formatting.

将HTML数据转换为Excel Rich Text的最佳方法(最简单,最快),反之亦然?

What is the best (easiest, fastest) way to convert HTML data to Excel Rich Text and vice versa?

我想到的一个解决方案是创建一个将遍历HTML的函数(使用DOMDocument左右),将\n放置在块元素之后,为<b><i>创建PHPExcel_RichText_Run对象等等,并忽略所有其他元素.我觉得这将是昂贵的",尤其是在处理诸如<b>some <i>formatted<i> text</b>

One solution I've in mind is to create a function that will traverse the HTML [using DOMDocument or so], place \n after block elements, create PHPExcel_RichText_Run objects for <b>, <i> etc, and ignore all other elements. I feel this will be quite "expensive", especially when dealing with nested structures, like <b>some <i>formatted<i> text</b>

是否有更好的方法(使用或不使用PHPExcel)?

Is there any better way to do this, with or without PHPExcel?

另一个想法:我注意到,当以 XML Spreadsheet 2003 格式导出时,XML内会出现以下内容:

One more idea: I noticed that when exporting in XML Spreadsheet 2003 format the following appears inside XML:

<ss:Data ss:Type="String"
  xmlns="http://www.w3.org/TR/REC-html40"><Font html:Color="#000000">this is </Font><B><Font
    html:Color="#000000">some </Font><I><Font html:Color="#000000">formatted</Font></I><Font
    html:Color="#000000"> text</Font></B></ss:Data>

这是正常的HTML4.我的意思是,看来Excel 可以理解纯HTML.因此,也许有某种方法可以将HTML直接传递给Excel,而无需将其转换为PHPExcel_RichText对象...(尽管请注意,如果我能够导出为.xlsx格式,那将是最好的选择)

which is normal HTML4. I mean it seems that Excel can understand plain HTML. So maybe there is some way to pass HTML directly to Excel without converting it to PHPExcel_RichText objects... (although note that it would be best if I'll be able to export to .xlsx format)

推荐答案

HTML到Rich-Text Runs在来年的PHPExcel开发路线上:但是,计划的方法是使用DOMDocument解析标记.

HTML to Rich-Text Runs is on the PHPExcel development roadmap for the coming year: however, the planned method was to use DOMDocument to parse the markup.

我们为PHPExcel本身采用的任何解决方案都必须使用RichText Runs来提供一致性.尽管MS Excel本身可以处理Excel的直接导入,并且(如您在Excel 2003提供的SpreadSheetML xml格式中所指出的那样),但在其他不同的Excel格式(BIFF和OfficeOpenXML)中,这是不一致的.

Any solution that we adopt for PHPExcel itself will have to use RichText Runs to provide consistency. While MS Excel itself can handle direct imports of Excel, and (as you've noted in the SpreadSheetML xml format offered by Excel 2003), this isn't consistent across the other different Excel formats (BIFF and OfficeOpenXML).

这篇关于将HTML转换为Excel RTF,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆