943,701 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 28824
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 4th, 2007
-1

Import/Export Excel

Expand Post »
I am trying to import and export to/from excel.

This needs to be able to be done on a weekly basis.

I have managed to find a couple of ways of doing this, but I need a way to set column width and row colours and wondered if anyone might know of a way of doing this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
TaoistTotty is offline Offline
46 posts
since Aug 2007
Sep 4th, 2007
0

Re: Import/Export Excel

what approach are you using? Is it PIA's or COM?
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Sep 4th, 2007
0

Re: Import/Export Excel

I have a new problem now!

I am trying to use bulk copy to import the excel spreadsheet, but I keep getting the following error:

The given ColumnMapping does not match up with any column in the source or destination.

I have tries a blank table and one that matches the headers used in the spreadsheet, but this has not made a difference.

What am I missing?

I am using Vista and have Office 2007 installed although the spreadsheet is on Office 2003 format.

The code I am using is:

Dim opendlg As New OpenFileDialog
opendlg.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
If opendlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim pathandfile As String = opendlg.FileName
Dim connection As OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "data source=" & pathandfile & ";Extended Properties='Excel 8.0;'")
Dim Command = New OleDb.OleDbCommand("select * from [Cornwall$]", connection)
connection.Open()
Dim dr As Data.IDataReader = Command.ExecuteReader
Dim bulkCopy As SqlClient.SqlBulkCopy = New SqlClient.SqlBulkCopy("Data Source=.\SQLEXPRESS;Initial Catalog=CoaST GTBS Database;Integrated Security=True")
bulkCopy.DestinationTableName = "LocalIn"
bulkCopy.WriteToServer(dr)
Dim adapter As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Cornwall$]", connection)
adapter.Fill(Me.CoaST_GTBS_DatabaseDataSet.LocalIn)
connection.Close()

The error is shown at the bulkcopy.writetoserver(dr).

Thank you for any assistance.
Reputation Points: 10
Solved Threads: 0
Light Poster
TaoistTotty is offline Offline
46 posts
since Aug 2007
Sep 4th, 2007
0

Re: Import/Export Excel

You are missing the field mapping just before the Destination Table.

' Set up the column mappings by name.
Dim mapID As New _
SqlBulkCopyColumnMapping("ProductID", "ProdID")
bulkCopy.ColumnMappings.Add(mapID)

You have to do it for all the fields
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Sep 4th, 2007
0

Re: Import/Export Excel

There are diff. overloads for function (SqlBulkCopyColumnMapping)
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Sep 5th, 2007
0

Re: Import/Export Excel

Thank for this binoj_daniel, will will start typing.

Is there anything in the copy I am using that would allow the colour of rows to be able to be picked up?

I need to be able to pick these up from the spreadsheet and also copy back to a spreadsheet.
Reputation Points: 10
Solved Threads: 0
Light Poster
TaoistTotty is offline Offline
46 posts
since Aug 2007
Sep 5th, 2007
0

Re: Import/Export Excel

binoj-daniel, have just tried adding the code you gave me, but am still showing the same error.

When I had a few spelling mistakes I was give the error showing that there wasn't X column.

Do you have any idea where to start looking?
Reputation Points: 10
Solved Threads: 0
Light Poster
TaoistTotty is offline Offline
46 posts
since Aug 2007
Sep 5th, 2007
0

Re: Import/Export Excel

so you mapped all the columns? I would suggest you to create a test table with 2 columns and try this first. This way you can figure out where is the problem.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Sep 6th, 2007
0

Re: Import/Export Excel

Thank you for your suggestion, if only I had been thinking this would have been obvious.

I also found some sizing problems.

Can something along the lines about be used to pick up the row colours?
Reputation Points: 10
Solved Threads: 0
Light Poster
TaoistTotty is offline Offline
46 posts
since Aug 2007
Sep 6th, 2007
0

Re: Import/Export Excel

Iam not sure if the bulk copy supports formatting the row colors, But you can do whatever you want with the excel file using .NET PIA's. This gives you the most control over the excel application.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006

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 VB.NET Forum Timeline: Lost path
Next Thread in VB.NET Forum Timeline: Send images to Crystal Reports - VB.NET





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


Follow us on Twitter


© 2011 DaniWeb® LLC