使用MVC将XML Spreadsheet导入Excel,Cell格式总是一般,如何更改?

Sim*_*bon 6 c# xml asp.net-mvc

遗留系统使用Excel 2003,因此我无法使用任何较新的第三方产品,因为它们适用于2007及更高版本,并且已经留下了传统的XML电子表格创建系统.

我现在需要在Excel中修改单元格格式,以便数字是数字,日期是日期等等......因为当前所有内容都在Excel格式化为常规.

我已经扫描了网页,并在局部视图中尝试了以下编码,以获得样式和工作簿部分:

<ss:Style ss:ID="MyTime">
<NumberFormat ss:Format="Long Time" />
</ss:Style>
<ss:Style ss:ID="MyNumber">
<NumberFormat ss:Format="General Number" />
</ss:Style>  
Run Code Online (Sandbox Code Playgroud)

这在XML outfile中表示

<Cell ss:StyleID='MyNumber'><Data ss:Type='Number'>419,717,200</Data></Cell>
Run Code Online (Sandbox Code Playgroud)

这是在主视图中创建的,并使用以下方式逐行构建:

private static IHtmlString ToXmlColumn(string columnContent, string ssType, string ssFormat)
{
 return MvcHtmlString.Create(string.Format("<Cell ss:StyleID='{2}'><Data ss:Type='{1}'>{0}</Data></Cell>", SecurityElement.Escape(columnContent), ssType, ssFormat));
}
Run Code Online (Sandbox Code Playgroud)

下载使用application/vnd.ms-excel的基础

public ExcelActionResult(string fileName, TModel model, ControllerContext context, string viewName = null)
        : base("application/vnd.ms-excel")
    {
        _context = context;
        _model = model;
        _viewName = viewName ?? (string)_context.RouteData.Values["action"];

        FileDownloadName = fileName;



    }
Run Code Online (Sandbox Code Playgroud)

但是,当我下载xls并在Excel 2003中重新打开它时,所有单元格格式仍然是General.

我错过了什么或我做错了什么?

这段代码在系统中无处不在,所以我正在寻找修复或解决方法,而不是需要重写它或使用XML输出来创建XSD和映射,因为截止日期对我来说太紧张了.

因为我可以在底部正式回答我自己的问题,这里是答案:

如果其他人需要找到类似的东西,我会自己回答.

在这里查看要使用的自定义代码:

http://office.microsoft.com/en-gb/excel-help/number-format-codes-HP005198679.aspx

但我会考虑更换这个:

<ss:Style ss:ID="MyTime">
<NumberFormat ss:Format="Long Time" />
</ss:Style>

<ss:Style ss:ID="MyNumber">
<NumberFormat ss:Format="General Number" />
</ss:Style>  
Run Code Online (Sandbox Code Playgroud)

如果想要一个数字,请使用以下内容

<ss:Style ss:ID="MyTime">
<NumberFormat ss:Format="HH:MM:SS" />
</ss:Style>

<ss:Style ss:ID="MyNumber">
<NumberFormat ss:Format="0" />
</ss:Style>  
Run Code Online (Sandbox Code Playgroud)

或者如果希望12000在自定义而不是一般情况下显示为12,000.00

<ss:Style ss:ID="MyNumber">
<NumberFormat ss:Format="#,##0.00" />
</ss:Style>  
Run Code Online (Sandbox Code Playgroud)

我花了一整天的时间在互联网上寻找以上内容,所以我会在任何XML Spreadsheet查询中回答这个问题,因为它很难找到并且很难找到.

Her*_* Yu 2

没有 Excel 2003,但 Excel 2013 可以将其另存为“XML Spreadsheet 2003(*.xml)”,结果如下 XML 文件。

此样式将显示为无小数,无千位“,”。

<Style ss:ID="s64">
  <NumberFormat ss:Format="0"/>
</Style
Run Code Online (Sandbox Code Playgroud)

样式 ID 应链接到列标记,例如<Column ss:StyleID="s64" ss:Width="61.5"/>

此样式将显示为 2 位小数,并以“,”作为 1000 分隔符。

<Style ss:ID="s66">
  <NumberFormat ss:Format="Standard"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

这应该是长日期时间格式:

<NumberFormat ss:Format="m/d/yy\ h:mm;@"/>
Run Code Online (Sandbox Code Playgroud)

希望这对您来说是正确的方向。祝你好运!

<?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">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author>YourName</Author>
  <LastAuthor>YourName</LastAuthor>
  <Created>2015-06-25T18:25:51Z</Created>
  <Version>15.00</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>9195</WindowHeight>
  <WindowWidth>17280</WindowWidth>
  <WindowTopX>0</WindowTopX>
  <WindowTopY>0</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <NumberFormat ss:Format="[h]:mm:ss;@"/>
  </Style>
  <Style ss:ID="s64">
   <NumberFormat ss:Format="0"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Column ss:StyleID="s63" ss:AutoFitWidth="0"/>
   <Column ss:StyleID="s64" ss:Width="60"/>
   <Row>
    <Cell><Data ss:Type="String">My Time</Data></Cell>
    <Cell><Data ss:Type="String">My Number</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="DateTime">1899-12-31T12:12:34.000</Data></Cell>
    <Cell><Data ss:Type="Number">123213</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>
   <Print>
    <ValidPrinterInfo/>
    <HorizontalResolution>600</HorizontalResolution>
    <VerticalResolution>600</VerticalResolution>
   </Print>
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveCol>1</ActiveCol>
     <RangeSelection>C2</RangeSelection>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>
Run Code Online (Sandbox Code Playgroud)