使用xmlMaps以编程方式将Excel文件导出到XML [英] Programmatically export Excel files to XML using xmlMaps

查看:150
本文介绍了使用xmlMaps以编程方式将Excel文件导出到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Excel插件 OfficeExcel2003XMLToolsAddin 我已经能够为Excel工作表定义XML映射(此插件将范围转换为XML列表),现在我可以手动使用以下方法将Excel文件另存为XML文件:另存为.

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正确生成类似

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>

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

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

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

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

尝试使用 XlFileFormat中列出的所有枚举参考页,但没有成功.

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

有什么建议吗?谢谢

推荐答案

这应该有效

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

这篇关于使用xmlMaps以编程方式将Excel文件导出到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆