2

我需要阅读 excel 单元格中可用的格式信息。excel 文件可能包含一个带有“一些示例文本”之类的文本的单元格。每个单词都有不同的格式信息。就像单词“Some”可能是粗体并且有不同的字体颜色和大小。下一个词可能有不同的词。

我们可以读取单个单元格值的单个格式信息集吗?如果是,请让我知道如何做同样的事情。

我使用过 NPOI 和 OpenXML SDK 2.5,但没有运气。最好不要使用 excel 互操作,因为这将在服务器中处理。

4

2 回答 2

2

Here is a complete "hacker's answer" to your question.

I made a small Excel file with a single cell like this:

screen shot of spreadsheet

I saved the file, and closed it. Next, I changed the extension of the file from .xlsx to .zip - this allows you to open it and look at the contents. In a subfolder named xl you find a file called "sharedStrings.xml" that contains all the strings in the workbook, and their formatting within the cell. This is a giant array - this is done so that if the same value is entered in multiple cells, it is stored only once. Work through this file to get all the strings in all the cells, and their format...

For this particular cell, the sharedStrings.xml file contents start out as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="1" uniqueCount="1">
<si>
<r>
<t xml:space="preserve">This </t>
</r>
<r>
<rPr>
<b/>
<sz val="11"/>
<color theme="1"/>
<rFont val="Calibri"/>
<family val="2"/>
<scheme val="minor"/>
</rPr>
<t>cell</t>
</r>
<r>
<rPr>
<sz val="11"/>
<color theme="1"/>
... etc

By reading this file you will be able to reconstruct all the strings with their formatting. There MUST be a better way...

于 2013-02-04T21:18:16.877 回答
1

您正在谈论从 Cell 读取 RichText 值。NPOI 2.0 最终版本将支持它。该版本将于 2014 年第一季度发布。

于 2013-10-24T09:44:18.317 回答