使用C#和BizTalk将Excel(xlsx)转换为XML [英] Convert Excel (xlsx) to XML with C# and BizTalk
问题描述
我已经查看了该论坛上有关类似问题的大多数主题,但没有找到我真正想要的东西.
I have looked at most of the topics on this forum regarding similar questions but haven't found exactly what I am looking for.
我正在尝试使用C#为BizTalk 2013 R2
编写管道组件,以将传入的Excel 2010
.xlsx
文件简单地转换为裸/基本XML表示形式.
I am trying to write a pipeline component for BizTalk 2013 R2
using C# to simply convert an incoming Excel 2010
.xlsx
file to it's bare/base XML representation.
我不想针对它运行任何模板或XLST转换它或类似的东西.我只想按原样返回所述电子表格的基础XML表示形式.
I do not want to run any templates against it or XLST transform it or anything like that. I simply just want to return the underlying XML representation of said spreadsheet as is.
看来这应该是一个非常简单的任务,但我根本不知道该怎么做.
It seems like this should be a very easy task but I can't figure out how to do it at all.
我发现的所有内容都需要使用DataTables
并遍历行和单元格(通过OpenXML
)以输出特定的XML表示形式,该表示形式更易于理解,但不是我想要的.
Everything I've found requires working with DataTables
and looping through rows and cells (via OpenXML
) to output a specific XML representation that is more human readable but that isn't what I want.
我想要该电子表格的实际Microsoft XML表示形式.
I want the actual Microsoft XML representation of that spreadsheet.
任何帮助将不胜感激.
推荐答案
确定,无需进行任何解压缩即可解决问题.
OK, figured it out without having to do any unzipping of the file.
如果您使用SAX方法将工作表加载到在以下位置找到的OpenXmlReader
中:
If you use the SAX approach to loading the worksheet into an OpenXmlReader
found here:
https://msdn.microsoft .com/en-us/library/office/gg575571(v = office.15).aspx
然后您可以像这样使用阅读器获取OuterXml
:
You can then use the reader to get the OuterXml
like so:
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filepath, false))
{
WorkbookPart wbPart = spreadSheetDocument.WorkbookPart;
OpenXmlReader reader = OpenXmlReader.Create(wbPart);
while (reader.Read())
{
if (reader.ElementType == typeof(Sheet))
{
Sheet sheet = (Sheet)reader.LoadCurrentElement();
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(sheet.Id));
OpenXmlReader wsReader = OpenXmlReader.Create(wsPart);
while (wsReader.Read())
{
if(wsReader.ElementType == typeof(Worksheet))
{
Worksheet wsPartXml = (Worksheet)wsReader.LoadCurrentElement();
Console.WriteLine(wsPartXml.OuterXml + "\n");
}
}
}
}
Console.ReadKey();
}
这篇关于使用C#和BizTalk将Excel(xlsx)转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!