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

使用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参考页面上列出的所有枚举,但没有成功。

有什么build议么? 谢谢

这应该工作

 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

Linq to Excel提供程序:

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

然后使用LINQ到XML ….