Xcel Comparison IN VISUAL STUDIO



Using Excel In VISUAL 2008

Hamsa Gayatri Palepu

CIS 764, Fall 2008

Purpose:

This tutorial shows use of Microsoft Visual Studio 2008 using and using Excel sheets as a data source to build a web application.

Topics:

Overview

Prerequisites

Creating Excel sheets

Create a new project

Overview:

is a rich programming framework for building web-based applications. It offers outstanding support for both developers and administrators, providing improved ease-of-use, tool support, reliability, scalability, administration and security.

In this tutorial if a person receives any packet he/she is informed with a mail in which the tracking number of the packet is present. All the packet information is stored in the excel sheets.

The excel sheet which contains the email, its path is hardcoded in .net code. Whereas we have to select the other file which contains the packet information using the browse button.

Prerequisites:

Before starting the tutorial, you should:

1. Have Microsoft Visual Studio 2008 installed.

2. Have Microsoft Office Excel 2007 installed.

3. Create an Excel containing columns Name and Email.

4. Create another Excel containing columns Slno, Type, Code and Count.

Create Excel Sheets:

We compare two excel sheets, so creation of two excel sheets is must. One excel sheet must contain columns Name and Email. The other excel sheets must contain Slno, Type, Code and Count. (NOTE: Type and Name are same fields).Code is the tracking number of the packet. These excel sheets must be saved as Microsoft Office Excel 97-2003 Worksheet (.xls).

TUTORIAL:

Create a new project

Step 1: Open Microsoft Visual Studio 2008.

[pic]> Programs > Microsoft Visual Studio 2008 >[pic][pic]

Step 2:

Create a New Web Site

Click on the visual studio 2008 it opens startup page as below. Here select New Website.

File > New Web Site…

[pic]

Step 3:

Select Web Site and click OK

[pic]

Step 4:

This opens a Default aspx page.

[pic]

Step 5:

Click on the Property Window and Rename the file (Here it is renamed as MailExcel)

[pic]

Step 6:

After renaming the solution explorer appears as follows:

[pic]

Step 7: switch to design view

Click on the Source button at the bottom of the page. It appears as follows..[pic]

Step 8:

The Design view appears as follows

[pic]

Step 9:

From the toolbox click, drag and drop FileUpload.

[pic]

In the source view we see

Replace this with

*

By using this browse button we can select any excel sheet saved on the computer.

RequiredFieldValidator: - Makes the associated input control a required field.

Step 10:

Drag and drop a button adjacent to the browse.

[pic]

In the source view we see

Replace this with

Step 11:

Change properties to the button and rename it.

[pic]

Step 12:

Similarly add another button send mail

and change its properties.

Also add a hiddenfield from the toolbox and rename it as MailFilePath.

Add

This helps to view the data and if the data oveflows we get a scroll on the right side.

In the Source view

Add the function

Untitled Page

//

Step 13:

Now the design view appears as follows after editing the code.

[pic]

Step 14:

Double click on MailExcel.aspx.vb in the Solution Explorer.

[pic]

Step 15:

It appears as follows:-

[pic]

Step 16:

The above code in MailExcel.aspx.vb must be removed.

Now

Add the import statements

Imports System.Data.OleDb

Imports System.Text

Imports System.IO

Declare all the variables (Dim: Declares variables and allocates storage space.)

Partial Class MailExcel

Inherits System.Web.UI.Page

Dim objda As OleDbDataAdapter

Dim objds As Data.DataSet

Dim Datafilepath As String

Dim dsretData, dsretMail As Data.DataSet

The function performed when we click the display button is given below

Protected Sub Display_Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Display_Btn.Click

DataView()

End Sub

This in turn calls the function DataView().

Sub DataView()

Dim strbChk As New StringBuilder

Dim rs As Data.DataView = Nothing

Dim myView As Data.DataView

If Path.GetExtension(browseFile.PostedFile.FileName).ToLower = ".xls" Then

Dim strquery As String = "select distinct type from [Sheet1$] where type ''"

MailFilePath.Value() = browseFile.PostedFile.FileName

'----------Ecel file path from where u will retrive the data

If Not MailFilePath.Value().ToString().Substring(1, 1) = ":"

Then

MailFilePath.Value() = "C:\test\" + MailFilePath.Value().ToString()

End If

dsretData = New Data.DataSet

dsretData = ExeExlqry(strquery, MailFilePath.Value())

' Function to retive data

Datafilepath = "C:\test\Mail Notifier Map File123.xls"

Dim strqry As String = "select * from [Sheet1$]"

dsretMail = New Data.DataSet

sretMail = ExeExlqry(strqry, Datafilepath)

SendMail_Btn.Enabled = True

Else

Response.Write("alert('Upload Only Excel file')")

divSIP.InnerHtml = ""

Exit Sub

End If

strbChk = New StringBuilder

strbChk.Append("")

'appending the data from data set to string builder

For j As Integer = 0 To dsretData.Tables(0).Rows.Count - 1

myView = dsretMail.Tables(0).DefaultView

rs = FetchData(myView,dsretData.Tables(0).Rows(j).Item(0).ToString,"Name")

If rs.Count > 0 Then

strbChk.Append("" & dsretData.Tables(0).Rows(j).Item(0).ToString & "" & rs.Item(0).Row(1).ToString & "")

Else

strbChk.Append("" & dsretData.Tables(0).Rows(j).Item(0).ToString & "Mail Id Not Available")

End If

Next

strbChk.Append(" ")

divSIP.InnerHtml = strbChk.ToString

'Displaying the data to div

End Sub

In the DataView we call two more functions namely FetchData and ExeExlqry.

Private Function FetchData(ByVal rs As Data.DataView, _ByVal FilterOn As String, ByVal TypeorName As String) As Data.DataView

rs.RowFilter = TypeorName & "='" & FilterOn & "'"

Return rs

End Function

Function ExeExlqry(ByVal strQry As String, ByVal Path As String) As Data.DataSet

Try

objds = New Data.DataSet

'--conection string to retrive the data from excel

Dim con As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Path & ";Extended properties=Excel 8.0")

objda = New OleDbDataAdapter(strQry, con)

objds = New Data.DataSet

objda.Fill(objds)

'---To store the excel data into the data set

objda.Dispose()

Return objds

Catch ex As Exception

If Not objds Is Nothing Then objds = Nothing

End Try

End Function

The function performed when we click the SendMail button is given below

Protected Sub SendMail_Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendMail_Btn.Click

Dim strbChk As New StringBuilder

Dim rs As Data.DataView = Nothing

Dim myView As Data.DataView

Dim strquery As String = "select * from [Sheet1$]"

dsretData = New Data.DataSet

dsretData = ExeExlqry(strquery, MailFilePath.Value())

' Function to retive data

Datafilepath = "C:\test\Mail Notifier Map File123.xls"

'Path of the excel which contains the column email

dsretMail = New Data.DataSet

dsretMail = ExeExlqry(strquery, Datafilepath)

For j As Integer = 0 To dsretMail.Tables(0).Rows.Count - 1

myView = dsretData.Tables(0).DefaultView

rs = FetchData(myView,dsretMail.Tables(0).Rows(j).Item(0).ToString, "Type")

strbChk = New StringBuilder

strbChk.Append("")

'appending the data from data set to string builder

For P As Integer = 0 To rs.Count - 1

strbChk.Append("" & rs.Item(P).Row(1).ToString & " " & rs.Item(P).Row(2).ToString & " " & rs.Item(P).Row(3).ToString & " ")

Next

strbChk.Append(" ")

If rs.Count Then

sendConfMail(dsretMail.Tables(0).Rows(j).Item(1).ToString,strbChk)

End If

Next

divSIP.InnerHtml = strbChk.ToString

'Displaying the data to div

SendMail_Btn.Enabled = False

End Sub

This in turn calls the function sendConfMail().

Private Sub sendConfMail(ByVal ToId As String, ByVal sbMailBody As StringBuilder)

Dim strMailServer As String = "smtp.ksu.edu"

'--write smtpserver ip

Dim strMailUser As String = ""

'--User Name

Dim strMailPassword As String = ""

'----Password

Dim strMailSmtpSvrPort As String = "25"

Dim strMailSendUsing As String = "2"

Dim strMailSmtpAuthne As String = "1"

Dim strFromMailId As String = "hamsa@ksu.edu"

'------------------FromId

Dim strBody = "" & vbCrLf _

& "" & vbCrLf _

& "" & vbCrLf _

& " Package In Receiving " & vbCrLf _

& "" & vbCrLf _

& "" & vbCrLf _

& " You have recieved the following package(s). Please pickup the package(s) as soon as possible" & vbCrLf _

& "" & vbCrLf _

& " Thank-You" & vbCrLf _

& " " & vbCrLf _

& "" & vbCrLf _

& " Receiving Department" & vbCrLf _

& " " & vbCrLf _

& "" & vbCrLf _

& "" & vbCrLf

Dim objCDO As Object

objCDO = CreateObject("CDO.Message")

With objCDO.Configuration.Fields

.Item("") = strMailServer

.Item("") = strMailSmtpSvrPort

.Item("") = strMailSendUsing

.Item("") = strMailSmtpAuthne

.Item("") = strMailUser

.Item("") = strMailPassword

.Update()

End With

objCDO.From = strFromMailId

objCDO.To = ToId

objCDO.Subject = "Package In Receiving"

objCDO.htmlBody = strBody + sbMailBody.ToString

objCDO.Send()

objCDO = Nothing

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

End Class

Step 17:

Save the website.

Then File > Save All

[pic]

Step 18:

Then set MailExcel.aspx as a startup page.

[pic]

Step 19:

Make sure the excel sheet which has the email is placed in the correct path and write the path in the code where it is commented as 'Path of the excel which contains the column email.

Step 20:

Now click on Run button ([pic]) which is present on the top.

Step 21:

Now the page appears as follows:

[pic]

NOTE:

If the page opens in [pic] copy paste the link

[pic] From the [pic] and paste it in Mozilla Firefox.

This is done because the Mozilla does not work properly with the java scripts.

Step 22:

Click on the browse button and search for the excel sheet which has to be compared with the other excel sheet.

When we click on browse a window appears

[pic]

Select excel sheet and Click open.

Step 23:

The path appears as follows

[pic]

Step 24:

Click on the Display button.

It appears as follows:

[pic]

The sendmail button is visible now.

Step 25:

Click on the send mail button.

It appears as follows:

[pic]

Step 26:

The mails are sent to the respective mail ids with the tracking number.

[pic]

[pic]

[pic]

[pic]

The End.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download