1

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

有什么建议么?谢谢

4

2 回答 2

1

Linq to Excel 提供程序:

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

然后使用 linq to xml....

于 2011-06-10T16:25:25.237 回答
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

于 2011-06-30T15:16:45.227 回答