M
M Schnell
All,
I'm trying to take an excel file and insert data from a database into specific cells and then send the file to the user. I've been able to open the excel file and can even read data from specific cells. However, when I try to insert data into the sheet and then save it back, the data gets lost. Here is the applicable part of my code.
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
sConnectionString = sConnectionString + " Data Source=" +sEmailPath & "\energy.xls" + ";"
sConnectionString = sConnectionString + "Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
objConn = New OleDbConnection(sConnectionString)
Try
objConn.Open()
Catch ex As Exception
'Error handling code
End Try
'Create new OleDbCommand to return data from worksheet.
objCmdSelect = New OleDbCommand("SELECT * FROM [Sheet1$]", objConn)
'Create new OleDbDataAdapter that is used to build a DataSet
'based on the preceding SQL SELECT statement.
objDA = New OleDbDataAdapter
'Pass the Select command to the adapter.
objDA.SelectCommand = objCmdSelect
'Create new DataSet to hold information from the worksheet.
objDS = New DataSet
'Fill the DataSet with the information from the worksheet.
objDA.Fill(objDS, "Energy")
With objDS.Tables(0)
.Rows(1).Item(1) = ""
.Rows(1).Item(2) = 0
.Rows(1).Item(3) = "CL"
.Rows(1).Item(4) = SeriesFlowFactorLabel.Text
.Rows(1).Item(5) = (CType(hpPerFanMotorLabel.Text, Int32) * CType(numberOfFanMotorsLabel.Text, Integer)).ToString
.Rows(1).Item(6) = (CType(pumpMotorCountLabel.Text, Int32) * CType(pumpMotorCountLabel.Text, Integer)).ToString
.Rows(1).Item(6) = quantityLabel.Text
End With
objDS.AcceptChanges()
objDA.Update(objDS, "Energy")
objConn.Close()
objDS.Dispose()
Any help would be appreciated.
Michael Schnell
I'm trying to take an excel file and insert data from a database into specific cells and then send the file to the user. I've been able to open the excel file and can even read data from specific cells. However, when I try to insert data into the sheet and then save it back, the data gets lost. Here is the applicable part of my code.
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
sConnectionString = sConnectionString + " Data Source=" +sEmailPath & "\energy.xls" + ";"
sConnectionString = sConnectionString + "Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
objConn = New OleDbConnection(sConnectionString)
Try
objConn.Open()
Catch ex As Exception
'Error handling code
End Try
'Create new OleDbCommand to return data from worksheet.
objCmdSelect = New OleDbCommand("SELECT * FROM [Sheet1$]", objConn)
'Create new OleDbDataAdapter that is used to build a DataSet
'based on the preceding SQL SELECT statement.
objDA = New OleDbDataAdapter
'Pass the Select command to the adapter.
objDA.SelectCommand = objCmdSelect
'Create new DataSet to hold information from the worksheet.
objDS = New DataSet
'Fill the DataSet with the information from the worksheet.
objDA.Fill(objDS, "Energy")
With objDS.Tables(0)
.Rows(1).Item(1) = ""
.Rows(1).Item(2) = 0
.Rows(1).Item(3) = "CL"
.Rows(1).Item(4) = SeriesFlowFactorLabel.Text
.Rows(1).Item(5) = (CType(hpPerFanMotorLabel.Text, Int32) * CType(numberOfFanMotorsLabel.Text, Integer)).ToString
.Rows(1).Item(6) = (CType(pumpMotorCountLabel.Text, Int32) * CType(pumpMotorCountLabel.Text, Integer)).ToString
.Rows(1).Item(6) = quantityLabel.Text
End With
objDS.AcceptChanges()
objDA.Update(objDS, "Energy")
objConn.Close()
objDS.Dispose()
Any help would be appreciated.
Michael Schnell