使用XML格式化Excel单元格

时间:2018-03-13 05:44:34

标签: xml excel spreadsheetml

我想以编程方式生成Excel(Salesforce Apex)类似于下面的屏幕截图。细胞的数量,细胞的背景颜色将在运行时决定,因此使用编程方式。 enter image description here

为实现这一点,我尝试为Cell>应用内联样式。数据,但似乎我们不能在那里应用内联样式。例如,样式应用于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>

1 个答案:

答案 0 :(得分:5)

您尝试使用的XMLOffice 2003 SpreadsheetML。引用为XML Spreadsheet Reference

如果查看“XML电子表格标记层次结构”,您会看到命名空间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>

另一种选择是将默认命名空间更改为xmlns="http://www.w3.org/TR/REC-html40" html。然后,html代码不需要html作为前缀,但ss代码必须为。{/ p>

示例:

<?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>