将包含多个XML的文件作为表格导入excel
我正在尝试将一个文件导入excel工作表,该工作表中包含多个xml文件。如下所示:您只能有一条标识线(将包含多个XML的文件作为表格导入excel,xml,excel,Xml,Excel,我正在尝试将一个文件导入excel工作表,该工作表中包含多个xml文件。如下所示:您只能有一条标识线( using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string rootTag = "Root";
StreamReader reader = new StreamReader(FILENAME, Encoding.UTF8);
string xml = "";
string inputLine = "";
int lineCount = 0;
while((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if(++lineCount > 2)
{
if(inputLine.StartsWith("<?xml") || inputLine.StartsWith("<" + rootTag) || inputLine.StartsWith("</" + rootTag))
{
inputLine = ""; //remove line
}
}
xml += inputLine;
}
//add back closing tag
xml += "</" + rootTag + ">";
XDocument doc = XDocument.Parse(xml);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
字符串rootTag=“Root”;
StreamReader=新的StreamReader(文件名,Encoding.UTF8);
字符串xml=”“;
字符串inputLine=“”;
int lineCount=0;
而((inputLine=reader.ReadLine())!=null)
{
inputLine=inputLine.Trim();
如果(++行数>2)
{
如果(inputLine.StartsWith(“考虑从一个大的原始文件中提取各个xml文件。然后,将这些单独的文件读入Excel
以下是用于提取解决方案的VBA宏:
Sub ExtractXML()
Dim strPath As String, DataLine As String, strOutput As String, strNewPath As String
Dim FileNum As Integer
Dim fso As Object, oFile As Object
Dim i As Long
strPath = "C\Path\To\XMLRawFile.xml"
Set fso = CreateObject("Scripting.FileSystemObject")
FileNum = FreeFile()
Open strPath For Input As #FileNum
strOutput = ""
i = 1
While Not EOF(FileNum)
' READ EACH LINE OF RAW XML FILE
Line Input #FileNum, DataLine
strOutput = strOutput & DataLine & vbNewLine
' OUTPUT INDIVIUDAL FILES AT THE END OF EACH ROOT NODE
If DataLine = "</Root>" Then
strNewPath = "C\Path\To\XMLNewFile" & i & ".xml"
Set oFile = fso.CreateTextFile(strNewPath)
oFile.WriteLine strOutput
oFile.Close
strOutput = ""
Set oFile = Nothing
i = i + 1
End If
Wend
Set fso = Nothing
MsgBox "Successfully extracted raw xml into multiple xml files!", vbInformation
End Sub
Sub-ExtractXML()
Dim strPath作为字符串,DataLine作为字符串,strOutput作为字符串,strNewPath作为字符串
Dim FileNum作为整数
作为对象的Dim fso,作为对象的oFile
我想我会坚持多久
strPath=“C\Path\To\XMLRawFile.xml”
设置fso=CreateObject(“Scripting.FileSystemObject”)
FileNum=FreeFile()
打开strPath作为#FileNum输入
strOutput=“”
i=1
而不是EOF(FileNum)
'读取原始XML文件的每一行
行输入#FileNum,数据行
strOutput=strOutput&DataLine&vbNewLine
'在每个根节点的末尾输出单个文件
如果DataLine=“”,则
strNewPath=“C\Path\To\XMLNewFile”&i&“.xml”
文件集=fso.CreateTextFile(strNewPath)
oFile.WriteLine strOutput
奥菲尔,关门
strOutput=“”
文件集=无
i=i+1
如果结束
温德
设置fso=无
MsgBox“已成功将原始xml提取到多个xml文件中!”,vbInformation
端接头