1. I got to read data from the SQL Server DB and represent the report in the Excel format.
To do it I wrote the following code:
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Namespace SQLServertoExcel
' Summary description for Form2.
Public Class Form2
Inherits System.Windows.Forms.Form
Friend WithEvents button1 As System.Windows.Forms.Button
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
' Required for Windows Form Designer support
InitializeComponent()
End Sub 'New
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
' <summary>
' Required method for Designer support - do not modify
' the contents of this method with the code editor.
' </summary>
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'button1
'
Me.button1.Location = New System.Drawing.Point(32, 72)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(216, 112)
Me.button1.TabIndex = 0
Me.button1.Text = "Populate Excel With Customer Information From Microsoft SQL Server"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub 'InitializeComponent
' <summary>
' The main entry point for the application.
' </summary>
<STAThread()> _
Shared Sub Main()
Application.Run(New Form2())
End Sub 'Main
Private Function GetData() As System.Data.DataTable
Dim conn As SqlConnection
Dim strConn As String
'Set the Conn String to the NSRCG DB
'Don't forget to add TRUSTED_CONNECTION = TRUE for Development
strConn = "server=MPR23;database=NSRCG;TRUSTED_CONNECTION=TRUE"
conn = New SqlConnection(strConn)
Dim adapter As New SqlDataAdapter("select * FROM tlkpStatus", conn)
Dim myDataSet As New DataSet()
Try
adapter.Fill(myDataSet, "Status"
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Return myDataSet.Tables(0)
End Function 'GetData
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
Dim excel As New Excel.ApplicationClass()
Dim rowIndex As Integer = 1
Dim colIndex As Integer = 0
excel.Application.Workbooks.Add(True)
Dim table As DataTable = GetData()
Dim col As DataColumn
For Each col In table.Columns
colIndex += 1
excel.Cells(1, colIndex) = col.ColumnName
Next col
Dim row As DataRow
For Each row In table.Rows
rowIndex += 1
colIndex = 0
Dim col1 As DataColumn
For Each col1 In table.Columns
colIndex += 1
excel.Cells(rowIndex, colIndex) = row(col1.ColumnName).ToString()
Next col1
Next row
excel.Visible = True
End Sub 'button1_Click
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class 'Form2
End Namespace 'SQLServertoExcel
2. When the user updates whatever fields in that Excel document and saves Excel, Im supposed to write a code to update the DB. That's what I don't know how to do.
This could theoreticly be done but unless you are tied into doing it that way I would suggest that you just show a datagrid on the form. Have them make any updates in the datagrid and then click a button to show the excel report again. (In this button click you could update the database as well.)
The excel file would need to be saved and closed to be able to read back the data anyway. Or have a special form and use VBA to capture events in the exel document and send the data back to the database.
Either way you would really be wasting your time. VB.net also has Crystal Reports in it. Crystal reports can be exported to excel as well.
Let me know what you decide to do. If you still decide to go with your first decision let me know and I will try to help you work it out.
I'm back to you again.
They still want it to be with Excel. They even want to put the button on the Excel form, which will be clicked if some updates were made and would be responsible to point Excel doc to the certain folder and save it there. After that I'll have to use that document as my Data Source. Later on, I'll not update some certain values in the DB with that Excel data. But what I'll do instead is just after saving the existing data into a certain place, I'll delete the entire data from the table and insert the new one from my new Data Source, which will be an updated and saved Excel Doc. What do you think about it?
I think the easiest way would be to use the excel sheet as a datasource and load it into a dataset. Then you could just update it from the dataset. I think that would be much easier than reading the excel file row by row cell by cell and updating that way.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.