PeterBaileyUk
asked on
export data table to excel in vb.net
I am looking to export a data table in vbn.net to excel and tried the function from ID: 25867766.
but I think it needs a reference and I am not sure what that is.
Many on googling use the microsoft office.interop but thats not working for me.
I am using office 365 latest version on a home pc and in visual studio 2015 in windows form.
but I think it needs a reference and I am not sure what that is.
Many on googling use the microsoft office.interop but thats not working for me.
I am using office 365 latest version on a home pc and in visual studio 2015 in windows form.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
*the function is for a datatable
If you just want to create a CSV file, have you check my article?
ASKER
Sorry Eric our timezones are out a little, yes I read your article and I am wondering to create an xml file instead as its in your article and might be better. will visual studio 15 have the bits in needs to do this? or will i need to add the additions you mentioned in your article. I ask as time probably has moved on a lot now.
ASKER
Ive decided to export my data as XML. the microsoft website has an example which ive followed but i get an unhandled error when it tries to write the dataset and not sure why.
Imports System.Data.SqlClient
Module Module1
Public Sub GetDifferences()
Dim StrSelected As String
Dim TableName As String
Dim dtFieldNameRecords As DataTable
Dim dtEditRecords As DataTable
Dim PKName As String
Dim ModelIDName As String
Dim FieldName As String
Dim y As Long
Dim VehCatName As String
Dim StrPath As String
FieldName = ""
TableName = ""
VehCatName = ""
PKName = ""
ModelIDName = ""
StrPath = ""
Dim sSet As DataSet = New DataSet("Records")
Dim DTExport As DataTable = New DataTable
StrSelected = FrmMain.CBClients.SelectedValue.ToString()
Select Case StrSelected
Case = 1
TableName = "tClient"
PKName = "abiCode"
VehCatName = "ABI_cat"
ModelIDName = "abiCode"
StrPath = "N:\Data\Abi"
Case = 2
TableName = "CAPDATA"
PKName = "CAPid_CAPcat"
VehCatName = "CAP_cat"
ModelIDName = "CapVehicleID"
StrPath = "N:\Data\Cap"
Case = 3
TableName = "GLASS FULL TABLE"
PKName = "GLASSid_GLASScat"
VehCatName = "GLASS_cat"
ModelIDName = "Model_id"
StrPath = "N:\Data\Glass"
Case = 4
TableName = "TVIDATA"
PKName = "DERIVATIVE_CODE"
VehCatName = "TVIVehicleTyep"
ModelIDName = "DERIVATIVE_ID"
StrPath = "N:\Data\Thatcham"
Case Else
TableName = "n/a"
End Select
dtFieldNameRecords = Nothing
dtEditRecords = Nothing
y = 0
FrmMain.ProgressBar1.Step = 1
FrmMain.ProgressBar1.Minimum = 1
FrmMain.ProgressBar1.Value = FrmMain.ProgressBar1.Minimum
dtFieldNameRecords = GetFields(TableName)
Debug.Print(y)
For Each drAccessRecord As DataRow In dtFieldNameRecords.Rows
y = dtFieldNameRecords.Rows.Count
FrmMain.ProgressBar1.Maximum = y
FieldName = drAccessRecord("column_name")
If FieldName = "BATCH" Or FieldName = PKName Or FieldName = ModelIDName Then
Else
dtEditRecords = GetEdits(FieldName, TableName, PKName, VehCatName)
sSet.Tables.Add(dtEditRecords)
End If
FrmMain.ProgressBar1.PerformStep()
FrmMain.Label3.Text = "# of Files Read Veh Cat = " & Math.Round((FrmMain.ProgressBar1.Value.ToString / y) * 100, 2) & "%"
FrmMain.Label3.Refresh()
Next
Debug.Print(StrPath)
Dim xmlData As String = sSet.GetXml()
sSet.WriteXml(StrPath)
MsgBox("finished")
dtFieldNameRecords = Nothing
dtEditRecords = Nothing
End Sub
ASKER
row 89 is the problem and the path looks ok but what about filename of the xmlfile itself
what is the value of StrPath? it needs to contains the filename as well. Do you have write access to these folders the N: drive?
ASKER
thats what it is i didnt specify that ok just looking now.
ASKER
all done thank you Eric
ASKER
I started down this route then discovered that the function is for a dataset so maybe if this cant be got working I could write back to a table in the sql server DB, if there is a limitation to get the data to excel. excel can read a csv file but it creates more stages.
Open in new window