使用XML格式化Excel单元格

hig*_*ive 3 xml excel spreadsheetml

我想以编程方式生成一个Excel(Salesforce Apex),类似于下面的屏幕截图。单元的数量,单元的背景色将在运行时确定,并因此使用编程方式。 在此处输入图片说明

为了实现这一点,我尝试为“单元格”>“数据”应用内联样式,但是似乎我们不能在那里应用内联样式。例如,样式通过应用于第一个单元格ss:StyleID="s66"。但是对于第二个Cell,它不能以这种方式使用内联样式。由于无法预先定义样式,因此我需要一些动态的方法。谁能确认这是否可行或提供任何指导?

<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>
Run Code Online (Sandbox Code Playgroud)

Axe*_*ter 5

The XML you are trying to use is Office 2003 SpreadsheetML. The reference is XML Spreadsheet Reference.

如果查看“ XML Spreadsheet Tag Hierarchy”,您将看到名称空间ss始终在此添加前缀。因此它不是默认的名称空间。默认名称空间为html。所以FontBSup标签不被命名前缀。

但是在当前Excel版本中,如果另存为Office 2003 SpreadsheetML,则默认名称空间设置xmlns="urn:schemas-microsoft-com:office:spreadsheet"ss。因此,如果要使用html标签,则必须在标签前面加上html

例:

<?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>
Run Code Online (Sandbox Code Playgroud)

另一个选择是将默认名称空间更改xmlns="http://www.w3.org/TR/REC-html40"html。这样,html标签不必以前缀,html但是ss标签必须前缀。

例:

<?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>
Run Code Online (Sandbox Code Playgroud)