Dataset to excel gives error

Wirloff

Member
Joined
Mar 2, 2005
Messages
19
Location
Belgium
Programming Experience
1-3
Hi,
I'm tryin to export a dataset to excel but I keep getting:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred Additional information: Exception from HRESULT: 0x800A03EC.

On the Interet i've read that I should have something to do with my "culture info". This is my code:


Dim
excel As New Excel.Application

excel.Visible =
True

excel.UserControl = True

Dim workBook As Object = excel.Workbooks

Dim ds As DataSet
Dim da As OleDbDataAdapter

ds =
New DataSet

Dim oConn As OleDbConnection

oConn =
New OleDbConnection(mstrConn)

Dim strSQL As String

strSQL = "select * from tblLocation"

da =
New OleDbDataAdapter(strSQL, oConn)

da.Fill(ds, "tblLocation")

Dim col As DataColumn

For Each col In ds.Tables("tblLocation").Columns

colIndex += 1

excel.Cells(1, colIndex) = col.ColumnName

Next col

Dim row As DataRow

For Each row In ds.Tables("tblLocation").Rows

rowIndex += 1

colIndex = 0

For Each col In ds.Tables("tblLocation").Columns

colIndex += 1

excel.Cells(rowIndex, colIndex) = row(col.ColumnName).ToString()

Next col
Next row

Anybody who knows how to solve this??
 
Hello there ,Now u r using interoperatibility method which is slower than managed code .why don't u use directly the ADO.NET method to write the data to ur excell sheet that is easier and better option .I do not have the code now coz i am far from my computer any how u can find it in MSDN Online site.I am sure of that.
 
Solution found

Hi,
I have found the answer to my question. I got the error because of different "culture info".. I do not completely understand it, but I do know that the next sentences work ! :)

at the begin of the code I just placed:

Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture =
New System.Globalization.CultureInfo("en-US")


end at the end you just set it back to normal:

System.Threading.Thread.CurrentThread.CurrentCulture = oldCI

 
Back
Top