以编程方式创建 .xls 文件

6
有没有办法在iOS程序中通过编程方式创建Excel文件?

没有答案,但是同样的问题:http://stackoverflow.com/questions/15269767/how-to-create-a-custom-xls-file-programatically?rq=1 和 https://dev59.com/HGTWa4cB1Zd3GeqPDGo8 和 https://dev59.com/FWbWa4cB1Zd3GeqPUj6p?lq=1 - Thilo
2个回答

4
这是 XLS 文件的 XML 表示的简化版本,只需创建一个原始的 XML,并将其保存为 XLS 即可。
<?xml version="1.0"?>
<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">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Version>14.0</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>11720</WindowHeight>
  <WindowWidth>25600</WindowWidth>
  <WindowTopX>0</WindowTopX>
  <WindowTopY>0</WindowTopY>
  <TabRatio>600</TabRatio>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:Color="#000000"/>
   <NumberFormat/>
  </Style>
  <Style ss:ID="s62">
   <Interior/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Apple Inc">
  <Table x:FullColumns="1"
   x:FullRows="1" ss:StyleID="s62" ss:DefaultColumnWidth="53"
   ss:DefaultRowHeight="12">
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="90"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="180"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="60" ss:Span="5"/>
   <Column ss:Index="9" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="48"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="60"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="48"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="90" ss:Span="1"/>
   <Column ss:Index="14" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="300"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="55" ss:Span="1"/>
   <Row ss:AutoFitHeight="0" ss:Height="20">
    <Cell ss:StyleID="s63"><Data ss:Type="String">Room</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Description</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">level</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">level</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">level</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">level</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Date</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Audit</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Risk</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Value</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Score</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">ScoreName</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Action</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Comments</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Test</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Test</Data></Cell>
   </Row>
    <Row>
        <Cell><Data ss:Type="Number">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="Number">data value</Data></Cell>
        <Cell><Data ss:Type="Number">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">data value</Data></Cell>
        <Cell><Data ss:Type="String">Vau</Data></Cell>
        <Cell><Data ss:Type="String">Test</Data></Cell>
    </Row>
 </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Unsynced/>
   <Print>
    <ValidPrinterInfo/>
    <HorizontalResolution>600</HorizontalResolution>
    <VerticalResolution>600</VerticalResolution>
   </Print>
   <PageLayoutZoom>0</PageLayoutZoom>
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>15</ActiveRow>
     <ActiveCol>13</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
   <AllowFormatCells/>
   <AllowSizeCols/>
   <AllowSizeRows/>
   <AllowInsertCols/>
   <AllowInsertRows/>
   <AllowInsertHyperlinks/>
   <AllowDeleteCols/>
   <AllowDeleteRows/>
   <AllowSort/>
   <AllowFilter/>
   <AllowUsePivotTables/>
  </WorksheetOptions>
 </Worksheet>
</Workbook>

您也将能够创建不同的样式。
  <Style ss:ID="s63">
   <Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
   <Protection/>
  </Style>

很不幸,这似乎无法在Excel 2016中正确打开。 - TheYellowSquares
更正一下,它可以工作,但是您需要将数据单元格从数字数据类型修改为字符串数据类型,或者将这些字段中的“数据值”更改为数字。 - TheYellowSquares
第三次更新,当Excel打开时,.xls会导致您收到警告消息,请在下面的XML文档中使用以下行"<?mso-application progid="Excel.Sheet"?>",并将文件保存为.xml,以避免发生这种情况。 供参考,这被称为Office 2003 XML格式。 - TheYellowSquares

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接