Programmatically export Excel files to XML using xmlMaps

不打扰是莪最后的温柔 提交于 2020-01-23 16:52:09

问题


With the Excel addin OfficeExcel2003XMLToolsAddin I've been able to define XML mapping for an Excel Worksheet (this addin converts a range to a XML list) and now I'm able to manually save the Excel file as a XML file, using Save as.

Excel correctly produces something like

<?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>

Now, I would like to programmatically do the same (hopefully using c#, .NET 4.0).

I tried using npoi and Microsoft Office Interop Excel, using this code

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);

trying with all the enumerations listed on XlFileFormat reference page, with no success.

Any suggestions? Thanks


回答1:


This should work

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();

data should contain XML




回答2:


Linq to Excel Provider:

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

and then use linq to xml....



来源:https://stackoverflow.com/questions/6309174/programmatically-export-excel-files-to-xml-using-xmlmaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!