Friday, 7 August 2015

Compute The Long Distance Phone Charges From A Csv File

Use VB.NET to read a CSV file of phone charges.


Knowing store information in a character separated values (CSV) file and use it to compute long distance phone charges using Visual Basic.NET can save you time. A CSV file can be created using Microsoft Office Excel. Excel is a spreadsheet application commonly used to store and analyze large amounts of data. VB.NET is a computer programming language used to develop Windows applications. VB.NET is relatively easy to learn and use.


Instructions


1. Start Microsoft Office Excel and type the following:


In "A1," type "Country"


In "A2," type "Mexico"


In "A3," type "Canada"


In "A4," type "Brazil"


In "B1," type "Rate/Min"


In "B2," type "2.5"


In "B3," type "5.5"


In "B4," type "4.2"


2. Press "Ctrl" and "S" to open the "Save As" dialog box. Select "C:\" next to "Save in." Next to "File Name," type "longDistanceCharges" and select "CSV (MS-DOS)(*.csv)" next to "Save as type." Click "Save."


3. Start "Microsoft Visual Basic Express," select the "File" menu and click "New Project." Click "Windows Forms Application" under "Visual Studio Installed Templates" and select "OK."


4. Press "Ctrl" plus "Alt" plus "X" to open the "Toolbox." Double-click "Button" under "Common Controls" to add a new button to "Form1." Double-click "Text Box" to add a new text box to your form.


5. Click "Project" and select "Add Reference." Click "COM," select "Microsoft Excel (version number) Object Library" and click "OK."


6. Double-click "Button1" to open the "Form1.vb" module. Press "Ctrl" plus "A," then "Delete" to remove all existing code.


7. Copy and paste the following code into your "Form1.vb" module:


Imports Excel = Microsoft.Office.Interop.Excel


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim XLApp As Excel.Application


Dim XLWkBook As Excel.Workbook


Dim XLWkSheet As Excel.Worksheet


Dim country(10) As String


Dim rates(10) As Double


Dim ratesStr As String


Dim min As Integer


Dim totalAmount As Double


min = Me.TextBox1.Text


XLApp = New Microsoft.Office.Interop.Excel.Application


XLWkBook = XLApp.Workbooks.Open("C:\longDistanceCharges.csv")


XLWkSheet = XLWkBook.Worksheets("longDistanceCharges")


country(1) = XLWkSheet.Cells(2, 1).value


country(2) = XLWkSheet.Cells(3, 1).value


country(3) = XLWkSheet.Cells(4, 1).value


ratesStr = XLWkSheet.Cells(1, 2).value


rates(5) = XLWkSheet.Cells(2, 2).value


rates(6) = XLWkSheet.Cells(3, 2).value


rates(7) = XLWkSheet.Cells(4, 2).value


totalAmount = min * rates(5)


MsgBox("Total cost for " & country(1) & ": $" & totalAmount)


totalAmount = min * rates(6)


MsgBox("Total cost for " & country(2) & ": $" & totalAmount)


totalAmount = min * rates(7)


MsgBox("Total cost for " & country(3) & ": $" & totalAmount)


XLWkBook.Close()


XLApp.Quit()


End Sub


End Class


The code will calculate phone charges based on the values in the CSV file you created. The results for each country will be displayed through a message box.


8. Press "F5" to run your program, then enter the amount of minutes in the text box. Click "Button1" to calculate the results using the CSV file.

Tags: Cells value, XLWkSheet Cells, XLWkSheet Cells value, Microsoft Office, Cells value rates, cost country