Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27353

VS 2010 Exporting to Excel from ListView

$
0
0
I am working on a small program for class that requires exporting data in the listing area to an external file. (There is more to the assignment but this is part one.)

I looked through all the posts about exporting to Excel then compiled what I thought would work for my assignment. I have specific errors (noted in the code in the zipped file attached) that I need to fix before I debug the program. I am learning as I go and rely heavily on what others have figured out. I am using VB 2010 Professional. Any help would be greatly appreciated. I have put in notes where problems are.

I have copied the code below for those who want a quick reference.

Thank You!
Imports Microsoft.Office.Interop

Public Class frmMain

Private total As Decimal


Private Structure ApplianceRecord
Dim strApplianceName As String
Dim strApplianceCost As String
Dim strAppliancePower As String
Dim strApplianceHours As String
Dim strApplianceWater As String
Dim strApplianceWaterCost As String
Dim strApplianceResult As String

End Structure

Dim Appliance() As ApplianceRecord
Dim ApplianceIndex As Integer

Private Sub AddEntry(ByVal Index As Integer)

'AddEntry transfers the data in the textboxes to the Next Appliance Record Structure
Dim strData As String
Dim ApplianceNameOnly As String

ApplianceNameOnly = Appliance(ApplianceIndex).strApplianceName
strData = Appliance(ApplianceIndex).strApplianceName + _
Appliance(ApplianceIndex).strApplianceCost + _
Appliance(ApplianceIndex).strAppliancePower + _
Appliance(ApplianceIndex).strApplianceHours + _
Appliance(ApplianceIndex).strApplianceWater + _
Appliance(ApplianceIndex).strApplianceWaterCost + _
Appliance(ApplianceIndex).strApplianceResult
lstDisplay.Items.Add(ApplianceNameOnly)

'Adds records to the ListView box
Dim li As New ListViewItem
li = lstTotal.Items.Add(Appliance(ApplianceIndex).strApplianceName)
li.SubItems.Add(Appliance(ApplianceIndex).strApplianceCost)
li.SubItems.Add(Appliance(ApplianceIndex).strAppliancePower)
li.SubItems.Add(Appliance(ApplianceIndex).strApplianceHours)
li.SubItems.Add(Appliance(ApplianceIndex).strApplianceWater)
li.SubItems.Add(Appliance(ApplianceIndex).strApplianceWaterCost)
li.SubItems.Add(Appliance(ApplianceIndex).strApplianceResult)

'Declared variable to hold the total of the results


End Sub

Private Sub ValidateData()

Dim strName As String
strName = txtName.Text
If strName = "" Then
MessageBox.Show("Please enter the name of the appliance")
txtName.Clear()
txtName.Focus()
' Generates the "show" of the statements and fields for water if txtName meets certain text
End If

Dim strCost As String
strCost = txtCost.Text
If strCost = "" Then
MessageBox.Show("Please enter the cost per kilowatt")
txtCost.Clear()
txtCost.Focus()
End If

Dim strPower As String
strPower = txtPower.Text
Try 'Validates the entered data for txtPower.Text
strPower = Double.Parse(txtPower.Text)
Catch ex As Exception
MessageBox.Show("Enter a numeric value between 0 and 6.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtPower.Clear()
txtPower.Focus()
End Try

Dim strHours As String
strHours = txtHours.Text
Try 'Validates the entered data for txtHours.Text
strHours = Double.Parse(txtHours.Text)
Catch ex As Exception
MessageBox.Show("Enter a numeric value between 0 and 24.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtHours.Clear()
txtHours.Focus()
End Try

Dim strWater As String
strWater = txtWater.Text

Dim strWaterCost As String
strWaterCost = txtWaterCost.Text

Dim strResult As String
strResult = lblResult.Text

ReDim Preserve Appliance(ApplianceIndex)

Appliance(ApplianceIndex).strApplianceName = strName
Appliance(ApplianceIndex).strApplianceCost = strCost
Appliance(ApplianceIndex).strAppliancePower = strPower
Appliance(ApplianceIndex).strApplianceHours = strHours
Appliance(ApplianceIndex).strApplianceWater = strWater
Appliance(ApplianceIndex).strApplianceWaterCost = strWaterCost
Appliance(ApplianceIndex).strApplianceResult = strResult

AddEntry(ApplianceIndex)

ApplianceIndex += 1

End Sub

'Event generated by clicking on an item in the listbox
Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged
Dim Index As Integer
Index = lstDisplay.SelectedIndex
DisplaySelectedAppliance(Index)
End Sub

'Transfers data from Structure element to Textboxes
Private Sub DisplaySelectedAppliance(ByRef Index As Integer)
txtShowName.Text = Appliance(Index).strApplianceName
txtShowCost.Text = Appliance(Index).strApplianceCost
txtShowPower.Text = Appliance(Index).strAppliancePower
txtShowHours.Text = Appliance(Index).strApplianceHours
txtShowWater.Text = Appliance(Index).strApplianceWater
txtShowWaterCost.Text = Appliance(Index).strApplianceWaterCost
lblShowTotal.Text = Appliance(Index).strApplianceResult
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

'Clears the values of all input fields and hides the water statements/fields
txtName.Text = ""
txtCost.Text = ""
txtPower.Text = ""
txtHours.Text = ""
lblResult.Text = ""
lblWater.Hide()
lblWaterCost.Hide()
txtWater.Hide()
txtWaterCost.Hide()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

'Variables used to convert text to string
Dim strCost As String
Dim strPower As String
Dim strHours As String
Dim strWater As String
Dim strWaterCost As String

'Associates the values of the text fields to the string variables
strCost = Val(txtCost.Text)
strPower = Val(txtPower.Text)
strHours = Val(txtHours.Text)
strWater = Val(txtWater.Text)
strWaterCost = Val(txtWaterCost.Text)

'Creates the variable to hold the result string
Dim decResult As Decimal

'Calculates the total cost to use an appliance based on user input
decResult = Val(strCost * strPower * strHours) + (strWater * strWaterCost)

'Associates the result string to the label field and formats result to currency
lblResult.Text = decResult.ToString("c")


End Sub

Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click

ValidateData()

'Creates the total of cost as each record is added
Dim result As Decimal

result = lblResult.Text
total = total + result
lblGrandTotal.Text = total.ToString("c")


'Clears the values of all input fields and hides the water statements/fields
txtName.Text = ""
txtCost.Text = ""
txtPower.Text = ""
txtHours.Text = ""
lblResult.Text = ""
lblWater.Hide()
lblWaterCost.Hide()
txtWater.Hide()
txtWaterCost.Hide()

End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Hides the statements and fields for water and the cost of water on the add panel
lblWater.Hide()
lblWaterCost.Hide()
txtWater.Hide()
txtWaterCost.Hide()

End Sub

Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
If txtName.Text = "Washer" Or txtName.Text = "washer" Or txtName.Text = "washing machine" Then
lblWater.Show()
lblWaterCost.Show()
txtWater.Show()
txtWaterCost.Show()
Exit Sub
End If
End Sub


'From this point to end is where the Export data subroutines are new. Everything else works perfectly to this point.
Private Sub ExportRecords(ByVal vntData As Object, ByVal vntHeader As Object, ByVal ws As Excel.Worksheet)
Dim lngRow As Long
Dim intCol As Integer
Dim varData As Object
Dim intStart As Integer
Dim x As Long
Dim y As Long

'Select all Cells and set the number format to string
ws.Cells.Select()
ws.Cells.NumberFormat = "@"
ws.Cells(1, 1).Select()
lngRow = UBound(vntData, 2) + 2
intCol = UBound(vntData, 1) + 1
intStart = 2 'Start from line 2

'Add headers
For x = 1 To intCol
varData = vntHeader(x - 1)
ws.Cells(1, x) = CStr(varData)
ws.Cells(1, x).Font.Bold = True
Next

'Freeze Row 2
ws.Rows(2).Select()
ws.Activate()

'Add Data
For y = 1 To intCol
For x = intStart To lngRow
varData = vntData(y - 1, x - 2)
If varData Is Nothing Then 'Make sure no null values, Excel will choke
'Add 1 to Move down a column
ws.Cells(x + 1, y) = ""
Else
ws.Cells(x + 1, y) = CStr(varData) 'Convert To String to preserve formatting
End If
Next
Next
'Resize Columns to Fit
ws.Columns.AutoFit()
ws.Range("a2", "a2").Select()
End Sub

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim objExcel As Excel.Application
Dim objWrkSht As Excel.Worksheet
Dim x As Integer

'Create Excel Object
objExcel = CreateObject("Excel.Application")

'Add the Workbook
objExcel.Workbooks.Add()
objWrkSht = objExcel.ActiveWorkbook.Sheets(1)
objExcel.Visible = True

'Fill the Workbook with data
Call ExportRecords(ByVal vntData As Object, ByVal vntHeader As Object, ByVal ws As Excel.Worksheet)
'Errors are the ByVal part of the above sentence

objExcel.Interactive = True

End Sub


Private Sub ExportListViewtoExcel(ByVal li As Control) 'Based on my program what should be listed as the control?
Dim vntHeader As Object
Dim vntData As Object
Dim x As Long
Dim y As Long
Dim intCol As Integer
Dim lngRow As Long

'Get Counts
intCol = CInt(lstTotal.Columns.Count - 1)
lngRow = CLng(lstTotal.Items.Count - 1)
ReDim vntData(intCol, lngRow)

'Create Header Array
For x = 0 To intCol
ReDim Preserve vntHeader(x)
vntHeader(x) = lstTotal.Columns(x + 1).Text 'Error on lstTotal.Columns
Next

'Create Data Array
For x = 0 To lngRow
vntData(0, x) = lstTotal.Item(x + 1).Text 'Error on lstTotal.Item

For y = 1 To intCol
vntData(y, x) = lstTotal.Item(x + 1).SubItems(y) 'Error on lstTotal.Item
Next
Next

End Sub


End Class
Attached Files

Viewing all articles
Browse latest Browse all 27353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>