简体   繁体   English

使用XML格式化Excel单元格

[英]Format Excel Cells with XML

I want to generate an Excel programmatically (Salesforce Apex) something similar to below screenshot. 我想以编程方式生成Excel(Salesforce Apex)类似于下面的屏幕截图。 The number of Cells, background color of the cells will be decided at the run time and hence using programmatic way. 细胞的数量,细胞的背景颜色将在运行时决定,因此使用编程方式。 在此输入图像描述

To achieve this I tried to apply inline styles for Cell > Data but it seems we can't apply inline styles there. 为了实现这一点,我尝试为Cell> Data应用内联样式,但似乎我们不能在那里应用内联样式。 For example styles get applied to first Cell with ss:StyleID="s66" . 例如,样式应用于具有ss:StyleID="s66"第一个Cell ss:StyleID="s66" But for the second Cell it doesn't work inline styles in that manner. 但对于第二个Cell,它不能以这种方式工作内联样式。 In my requirement since I can't pre-define the style I need some dynamic way. 在我的要求中,因为我无法预先定义样式,所以我需要一些动态的方式。 Can anyone confirm if this is not possible or provide any guidance? 任何人都可以确认这是不可能的还是提供任何指导?

<Row>

    <Cell ss:StyleID="s66"><Data ss:Type="String">Test Sheet1</Data></Cell>
    <Cell ><Data ss:Type="String"><Font ss:Color="#FF0000">Sample Text</Font></Data></Cell>

</Row>

The XML you are trying to use is Office 2003 SpreadsheetML . 您尝试使用的XMLOffice 2003 SpreadsheetML The reference is XML Spreadsheet Reference . 参考是XML Spreadsheet Reference

If you look at the "XML Spreadsheet Tag Hierarchy", you will see, that the namespace ss is always prefixed there. 如果查看“XML电子表格标记层次结构”,您将看到命名空间ss始终在那里作为前缀。 So it is not the default namespace. 所以它不是默认的命名空间。 The default namespace is html . 默认名称空间是html So the Font , B , Sup tags are not prefixed by namespace. 因此FontBSup标签不以名称空间为前缀。

But in current Excel versions, if saved as Office 2003 SpreadsheetML , the default namespace is set to xmlns="urn:schemas-microsoft-com:office:spreadsheet" which is ss . 但在当前的Excel版本中,如果保存为Office 2003 SpreadsheetML ,则默认命名空间设置为xmlns="urn:schemas-microsoft-com:office:spreadsheet" ,即ss So if one wants using html tags, they must be prefixed by html . 因此,如果想要使用html标签,它们必须以html为前缀。

Example: 例:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">

 <Worksheet ss:Name="Tabelle1">
  <Table>
   <Row>
    <Cell><Data ss:Type="String"><html:Font x:Color="#FF0000">Test</html:Font></Data></Cell>
    <Cell><Data ss:Type="String"><html:B>E = m c <html:Sup>2</html:Sup></html:B></Data></Cell>
   </Row>
  </Table>
 </Worksheet>

</Workbook>

Another option would be changing the default namespace to xmlns="http://www.w3.org/TR/REC-html40" which is html . 另一种选择是将默认命名空间更改为xmlns="http://www.w3.org/TR/REC-html40" ,即html Then the html tags needs not be prefixed by html but then the ss tags must. 那么html标签不需要以html为前缀,但是必须使用ss标签。

Example: 例:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<ss:Workbook xmlns="http://www.w3.org/TR/REC-html40"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">

 <ss:Worksheet ss:Name="Tabelle1">
  <ss:Table>
   <ss:Row>
    <ss:Cell><ss:Data ss:Type="String"><Font x:Color="#FF0000">Test</Font></Data></Cell>
    <ss:Cell><ss:Data ss:Type="String"><B>E = m c <Sup>2</Sup></B></Data></Cell>
   </ss:Row>
  </ss:Table>
 </ss:Worksheet>

</ss:Workbook>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM