473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Read XML based file that is recognized by Excel

I am having a problem reading an Excel file that is XML based. The directory
I am reading contains Excel files that can be of two types. Either generic Microsoft
based or XML based. I am reading the Microsoft based files with an OleDbDataAdapter.
Then filling the contents of the first worksheet into a dataset.

However when I try to add the XML based file to my dataset using an XmlTextReader
I can never seem to get it to save to a different table in the dataset in the same
format as when I use an OleDbDataAdapter for the generic Excel files. If I save
it(using ds.ReadXml) to my existing DataSet (which already has one table in it)
and no table gets created.

I tried saving it to a brand new DataSet and saw that 10 tables were created. I assume
mostly all are these are style sheet information. The last table (entitled "data") seems
to have all my data. I then tried to do a copy of that table and place it in my existing
DataSet. However column header information is not captured and I can't read the
table because when I open up the DataSet at a (later point in my program) I retrieve
datarow elements by specifying column names and the OleDb connection complains
it can't find them.

Perhaps there is a tweak I need to the XmlTextReader or perhaps my XML based
Excel file? I was thinking worse case I would try to find some OS command to convert
the file to a real Microsoft based format but obviously it would be easier to tweak
the XmlTextReader or the file itself. Here is a snippet of the code along with a
sample of the XML text file. If anyone has any solutions, it would be greatly appreciated!!

OleDbConnection objConn = new OleDbConnection(fileConnectionString);
try
{
objConn.Open(); //<---- This will trigger an exception if the file is XML based
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(fileContents,filename);
}
catch
{
DataSet ds = new DataSet();
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new System.IO.FileStream (filename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
ds.ReadXml(myXmlReader, XmlReadMode.ReadSchema);
// Close the XmlTextReader
myXmlReader.Close();
DataTable xmlData = ds.Tables["data"].Copy();
xmlData.TableName = filename;
fileContents.Tables.Add(xmlData);
}
finally
{
objConn.Close();
}

<?xml version='1.0'?>
<ss:Workbook xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'>
<ss:Styles>
<ss:Style ss:ID='1'>
<ss:Font ss:Bold='1'/>
</ss:Style>
<ss:Style ss:ID="s22">
<ss:NumberFormat ss:Format="Short Date"/>
</ss:Style>
</ss:Styles>
<ss:Worksheet ss:Name='Survey'>
<ss:Table><ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Column ss:Width='60'/>
<ss:Row ss:StyleID='1'>
<ss:Cell><ss:Data ss:Type='String'>MEM_ID</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>CALL_DATE</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>SURVEY_FORM</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>SOURCE</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>REASON_CODE</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>ACTION_CODE</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>OPERATOR_ID</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>COMMENTS</ss:Data></ss:Cell>
</ss:Row>

<ss:Row>
<ss:Cell><ss:Data ss:Type='String'>555555555</ss:Data></ss:Cell>
<ss:Cell ss:StyleID="s22"><ss:Data ss:Type='DateTime'>2005-10-21T12:00:00.000</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>DNTL</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>MSPA</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>ee</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>ff</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'>elmer_fudd</ss:Data></ss:Cell>
<ss:Cell><ss:Data ss:Type='String'></ss:Data></ss:Cell>
</ss:Row>
</ss:Table> </ss:Worksheet> </ss:Workbook>

Nov 17 '05 #1
0 4659

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Majordomo | last post by:
-- >>>> --36742377 **** Command '--36742377' not recognized. >>>> Content-Type: text/plain; charset=us-ascii **** Command 'content-type:' not recognized. >>>> Content-Transfer-Encoding: 7bit...
2
by: Reza Solouki | last post by:
Hello, I never had to do anything with office tools programmatically till now(I guess there is first time for everything), so this might be a very silly and simple question for some of you. Any...
0
by: ntuyen01 | last post by:
Hi All, I try to read the data from the excel, but I ran into this problem I have two excel files. file one.xls data inside like this: 1234 1234 1234 1234 abc-123 ...
0
by: Vader | last post by:
I am new to this forum. Thanks in advance for and help. The following is what I am looking for: 1. I need help with VB code to open a MS Word (.doc) file. 2. Read lines from the MS Word (.doc)...
2
by: wstsoi | last post by:
hi I have to read images from spreadsheet, is it possible to do with php?
1
by: Defacta | last post by:
Hi, When I upload an excel file, PHP does not recognize the type of this file: echo $HTTP_POST_FILES ; returns: application/octet-stream Whereas a word file returns: application/msword
2
by: phub11 | last post by:
I'm wanting to populate a drop-down based on the contents of one column in an Excel file, and then submit the file to a PHP script. I've gotten PHP to read and process the Excel file fine...
0
by: Patrice | last post by:
Hello, I'm streaming an Excel file to IE using the content-disposition header. If the user opens the file, Excel appends a "" to its name (the name looks file in IE temporary internet files). It...
0
by: paintballer4lfe | last post by:
Hello, i am trying to create a code or get something that lets me read an excel file and take whats in the excel file and then put all of the cells into a list box, or import certain cells into a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.