I have a file i receive from a blackbox system, the file is somehow mixed between xml and excel, when i open the file with excel i get first a warning message, and if i open it with text editor, the following xml head is:
<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="Default">
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom" />
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="sHeader">
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom" />
<Font ss:Bold="1"/>
<NumberFormat ss:Format="@"/>
</Style>
i tried many solutions includes (first try to import the file to DataGridView then export it to csv however i always get Unrecognized database format
First, what kind of xls mixed with xml file is this? how can i remove all these head information and just have a simple csv file?
UPDATE: I found a way to load data from this excel-XML file however i recieve all data in one column
this is the code i used:
XmlDocument xml = new XmlDocument();
string filePath = @"C:\temp\test.xml";
xml.Load(filePath);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
XmlElement root = xml.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("//ss:Data", nsmgr);
dataGridView1.DataSource= ConvertXmlNodeListToDataTable(nodeList);
public static DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
{
DataTable dt = new DataTable();
int TempColumn = 0;
foreach (XmlNode node in xnl.Item(0).ChildNodes)
{
TempColumn++;
DataColumn dc = new DataColumn(node.Name, System.Type.GetType("System.String"));
if (dt.Columns.Contains(node.Name))
{
dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
}
else
{
dt.Columns.Add(dc);
}
}
int ColumnsCount = dt.Columns.Count;
for (int i = 0; i < xnl.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < ColumnsCount; j++)
{
dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
}
dt.Rows.Add(dr);
}
return dt;
}
}
I found a solution:
Use microsoft name space to load the xls-xml file Get xmlNodeList
Please Notice that in my region we are using semicolon as separator
public static XmlNodeList ParseExcelEXMLFormat(string filePath)
{
try
{
XmlDocument xml = new XmlDocument();
xml.Load(filePath);
XmlNamespaceManager nsSchema = new XmlNamespaceManager(xml.NameTable);
nsSchema.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
XmlElement root = xml.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("//ss:Data", nsSchema);
return nodeList;
}
catch (Exception)
{
throw;
}
}
then convert XmlNodeList to StringBuilder
public static StringBuilder XMLNodeListToStringBuilderConverter(XmlNodeList xmlNodeList, string separator)
{
try
{
StringBuilder sb = new StringBuilder();
DataTable dt = new DataTable();
foreach (XmlNode node in xmlNodeList.Item(0).ChildNodes)
{
DataColumn dc = new DataColumn(node.FirstChild.InnerText, System.Type.GetType("System.String"));
dt.Columns.Add(dc);
}
int ColumnsCount = dt.Columns.Count;
string[] columnNames = dt.Columns.Cast<DataColumn>().
Select(column => column.ColumnName).
ToArray();
sb.AppendLine(string.Join(separator, columnNames));
string[] rows = new string[ColumnsCount];
for (int i = 1; i < xmlNodeList.Count; i++) // loop through rows
{
for (int j = 0; j < ColumnsCount; j++) // loop through columns
{
rows[j] = xmlNodeList.Item(i).ChildNodes[j].InnerText.Replace(separator, ",").Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " "); // remove seperator from original text, it will casue problem
}
sb.AppendLine(string.Join(separator, rows));
Array.Clear(rows, 0, ColumnsCount);
}
return sb;
}
catch (Exception)
{
throw;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.