944,184 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 6973
  • C# RSS
Oct 8th, 2006
0

Saving Excel Spreadsheet using ADO.net gives inconsistent results

Expand Post »
I wanted to be able to interrogate an Excel spreadsheet and programmatically save all the worksheets as text files (.csv).
I succesfully managed to do this using the .NET COM interop Excel collection of interfaces but even though this works beautifully I thought it would be nice if I could achieve the same results using the ADO.net classes.

For the purpose of this query please regard an individual Exel worksheet as a 'table'.
Having coded this I find that the Jet4.0 Excel Database engine does not interpret the 'table' in the same way as the COM interop interfaces. The problem being that where a cell contains a "," (comma) the ADO datareader interprets that cell as two cells.

Eg "16, The Ridings" is interpretted as two cells even though when viewed in Excel it is in one cell. I am unable to figure out how to override this undesirable behaviour and wondered if anyone has come across this problem (and solved it)

Here is the basics of the code:

using (DbCommand command = connection.CreateCommand())
{
//Create a directory based on the Spreadsheet name to store the csv versions of the worksheets
string newDirectory = _fileNameandPath.Remove(_fileNameandPath.LastIndexOf("."));
if (!Directory.Exists(newDirectory))
Directory.CreateDirectory(newDirectory);
foreach (string workSheetName in _workSheets)
{
//Create a csv file for this worksheet
StreamWriter sw = File.CreateText(newDirectory + "\\" + workSheetName + ".csv"); 
// Worksheets are referenced by their worksheet names
// We require all rows from the worksheet
command.CommandText = "SELECT * FROM [" + workSheetName + "$]";
if (connection.State != ConnectionState.Open)
connection.Open();
StringBuilder dataLine = new StringBuilder();
//Populate the DataReader with data from the worksheet
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
//Reset the string builder
dataLine.Remove(0, dataLine.Length);
//Build comma delimited string
for (int fieldCount = 0; i < dr.FieldCount; i++)
{
dataLine.Append(dr[fieldCount]);
//Exclude trailing comma from final field
if(fieldCount  != dr.FieldCount - 1)
dataLine.Append(",");
}
sw.WriteLine(dataLine);
}
sw.Close();
}
}
}
:rolleyes:
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
peterbyrne is offline Offline
16 posts
since Oct 2006
Oct 9th, 2006
0

Re: Saving Excel Spreadsheet using ADO.net gives inconsistent results

Solved.
Replace this

C# Syntax (Toggle Plain Text)
  1. dataLine.Append(dr[fieldCount]);

with this
C# Syntax (Toggle Plain Text)
  1. dataLine.Append("\"" + dr[fieldCount] + "\"");

Thanks to the MSDN forum
Reputation Points: 10
Solved Threads: 1
Newbie Poster
peterbyrne is offline Offline
16 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: operator error
Next Thread in C# Forum Timeline: help deciding a graduation project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC