如何将一个Open XML文档保存(并打开)为Excel文件?

时间:2019-05-08 09:57:06

标签: openxml

我有一个Open XML文档。我想知道如何将其另存为.xlsx文件。

我尝试打开Excel软件并执行Developer -> Import,但是它以Excel格式打开了我的XML文件-但是它实际上是将XML文件作为XML文档打开,而没有作为Open XML文档的Excel版本打开。

这是我拥有的Open XML文档:

<?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">
<Worksheet     ss:Name="Forecast">
<Names>
<NamedRange     ss:Name="Print_Area"/>
</Names>
<Table>
<Row>
  <Cell><Data ss:Type="String">Day1</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
  <Cell><Data ss:Type="String">Day2</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
  <Cell><Data ss:Type="String">Day3</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
  <Cell><Data ss:Type="String">Day4</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
  <Cell><Data ss:Type="String">Day5</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>

<Cell/>

  <Cell><Data ss:Type="String">NP25</Data></Cell>
  <Cell><Data ss:Type="String">NP26</Data></Cell>
  <Cell><Data ss:Type="String">NP27</Data></Cell>
  <Cell><Data ss:Type="String">NP28</Data></Cell>
  <Cell><Data ss:Type="String">NP29</Data></Cell>
  <Cell><Data ss:Type="String">NP30</Data></Cell>
</Row>

<Row>
    <Cell><Data>1</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>2</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>3</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>4</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>5</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>

  <Cell/>

    <Cell><Data>25</Data></Cell>
    <Cell><Data>26</Data></Cell>
    <Cell><Data>27</Data></Cell>
    <Cell><Data>28</Data></Cell>
    <Cell><Data>29</Data></Cell>
    <Cell><Data>30</Data></Cell>
</Row>
<Row>
    <Cell><Data>1</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>2</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>3</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>4</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>5</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>

  <Cell/>

    <Cell><Data>25</Data></Cell>
    <Cell><Data>26</Data></Cell>
    <Cell><Data>27</Data></Cell>
    <Cell><Data>28</Data></Cell>
    <Cell><Data>29</Data></Cell>
    <Cell><Data>30</Data></Cell>
</Row>
<Row>
    <Cell><Data>1</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>2</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>3</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>4</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>5</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>

  <Cell/>

    <Cell><Data>25</Data></Cell>
    <Cell><Data>26</Data></Cell>
    <Cell><Data>27</Data></Cell>
    <Cell><Data>28</Data></Cell>
    <Cell><Data>29</Data></Cell>
    <Cell><Data>30</Data></Cell>
</Row>
<Row>
    <Cell><Data>1</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>2</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>3</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>4</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>5</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>

  <Cell/>

    <Cell><Data>25</Data></Cell>
    <Cell><Data>26</Data></Cell>
    <Cell><Data>27</Data></Cell>
    <Cell><Data>28</Data></Cell>
    <Cell><Data>29</Data></Cell>
    <Cell><Data>30</Data></Cell>
</Row>
<Row>
    <Cell><Data>1</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>2</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>3</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>4</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>
    <Cell><Data>5</Data><NamedCell
  ss:Name="pRange_default"/><NamedCell     ss:Name="Print_Area"/></Cell>

  <Cell/>

    <Cell><Data>25</Data></Cell>
    <Cell><Data>26</Data></Cell>
    <Cell><Data>27</Data></Cell>
    <Cell><Data>28</Data></Cell>
    <Cell><Data>29</Data></Cell>
    <Cell><Data>30</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

This is the result of my import

您可以看到Excel导入将我的XML文件作为文本XML文档打开,而不是将其视为OpenXML文档。请帮忙!

1 个答案:

答案 0 :(得分:0)

抱歉重播晚了。

要让 Excel 将此 xml 文件作为电子表格打开,您需要在文档属性中包含 Excel progid。

public sealed class SizedDictionary<TKey, TValue> : Dictionary<TKey, TValue> {

private int maxSize;
private Queue<TKey> keys;

public SizedDictionary(int size) {
    maxSize = size;
    keys = new Queue<TKey>();
}

new public void Add (TKey key, TValue value) {
    if (key==null) throw new ArgumentNullException();
    base.Add(key, value);
    keys.Enqueue(key);
    if (keys.Count > maxSize) base.Remove(keys.Dequeue());
}

new public bool Remove (TKey key) {
    if (key==null) throw new ArgumentNullException();
    if (!keys.Contains(key)) return false;
    var newQueue = new Queue<TKey>();
    while (keys.Count>0) {
        var thisKey = keys.Dequeue();
        if (!thisKey.Equals(key)) newQueue.Enqueue(thisKey);
    }
    keys=newQueue;
    return base.Remove(key);
}
}