使用xmlMaps以编程方式将Excel文件导出到XML

时间:2011-06-10 16:15:07

标签: c# xml excel excel-interop npoi

使用Excel插件OfficeExcel2003XMLToolsAddin我已经能够为Excel工作表定义XML映射(此插件将范围转换为XML列表),现在我能够手动使用“另存为”将Excel文件另存为XML文件。

Excel正确生成类似

的内容
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Row>
        <brand>Brand1</brand>
        <Italian>Description1</Italian>
        <English>Description2</English>
    </Row>
    <Row>
        <brand>Brand2</brand>
        <Italian>Description3</Italian>
        <English>Description4</English>
    </Row>
</Root>

现在,我想以编程方式做同样的事情(希望使用c#,.NET 4.0)。

我尝试使用npoi和Microsoft Office Interop Excel,使用此代码

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Workbooks.OpenXML(@"excelFile.xls");
xlApp.Workbooks[1].SaveAs(xmlFile, XlFileFormat.SOME_FORMAT);

尝试XlFileFormat reference page上列出的所有枚举,但没有成功。

有什么建议吗?感谢

2 个答案:

答案 0 :(得分:1)

Linq to Excel Provider:

http://solidcoding.blogspot.com/2008/01/linq-to-excel-provider-25.html

然后使用linq to xml ....

答案 1 :(得分:1)

这应该有效

Application app = new Application();
app.Workbooks.Open(@"C:\Sample.xlsx",
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing);
string data = string.Empty;
app.ActiveWorkbook.XmlMaps[1].ExportXml(out data);
app.Workbooks.Close();

数据应包含XML