Public Overloads Sub ExportToExcel(ByVal filepath As String)
Dim xlsApp As Excel.Application
Dim xlsWkbk As Excel.Workbook
Dim xlsSht As Excel.Worksheet
Dim xlsRange As Excel.Range
Dim templateName As String = "c:\Data\Suez_HourlyData.xlt"
Dim tmpBillDate As String = Me.PostDate.ToString("MM/dd/yyyy")
Dim x As Object = Type.Missing
' Detail variables
Dim i As Integer
Dim hdrRows As Integer = 7
Dim nRow As Integer
Dim nCol As Integer
Dim time1 As Date = Now()
Try
xlsApp = New Excel.Application
xlsApp.DisplayAlerts = False
xlsWkbk = xlsApp.Workbooks.Open(templateName, x, x, x, x, x, x, x, x, x, False, x, x, x)
xlsSht = xlsWkbk.ActiveSheet
xlsApp.Visible = False
' Start writing to Excel
' Header output
xlsRange = xlsSht.Cells(1, 2) 'B1
xlsRange.Value2 = Me.EdcAcct
xlsRange = xlsSht.Cells(2, 2) 'B2
xlsRange.Value2 = Me.KyBaLeadZeros
xlsRange = xlsSht.Cells(3, 2) 'B3
xlsRange.Value2 = Me.KyEnroll
xlsRange = xlsSht.Cells(4, 2) 'B4
xlsRange.Value2 = tmpBillDate
' Detail output
For i = 0 To Me.RowCount - 1
nRow = hdrRows + (i + 1)
nCol = 1 ' SR_SEQ_No
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.SrSeqNo
nCol = 2 ' DT
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.Dt(i)
nCol = 3 ' HHMM
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.Hhmm(i)
nCol = 4 ' MeteredKWh
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.MeteredKwh(i)
nCol = 5 ' ActualKWh
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.ActualKwh(i)
nCol = 6 ' Peak
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.Peak(i)
nCol = 7 ' Indexprice
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.IndexPrice(i)
nCol = 8 ' Price adder
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.PriceAdder(i)
nCol = 9 ' Pricing Zone
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.PricingZone(i)
nCol = 10 ' Index Only Energy Charge
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.IndexOnly(i)
nCol = 11 ' Index Adder Energy Charge
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = Me.IndexPlusAdder(i)
nCol = 12 ' Post Date
xlsRange = xlsSht.Cells(nRow, nCol)
xlsRange.Value2 = tmpBillDate
Next
' Save the workbook
'xlsWkbk.AcceptAllChanges()
xlsWkbk.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal, _
x, x, False, False, Excel.XlSaveAsAccessMode.xlNoChange, _
Excel.XlSaveConflictResolution.xlLocalSessionChanges, False, x, x)
Catch comEx As COMException
Debug.WriteLine(comEx.ToString())
Catch ex As Exception
Debug.WriteLine(ex.ToString())
Finally
' Free up resources
xlsWkbk.Close()
xlsApp.Quit()
ReleaseComObject(xlsRange)
ReleaseComObject(xlsSht)
ReleaseComObject(xlsWkbk)
ReleaseComObject(xlsApp)
End Try
Dim time2 As Date = Now()
Dim timeDiffInSeconds As Double = (time2.Ticks - time1.Ticks) / 10000000.0
Debug.WriteLine("Excel time in seconds: " & timeDiffInSeconds)
End Sub
Private Sub ReleaseComObject(ByRef reference As Object)
Try
Do Until _
System.Runtime.InteropServices.Marshal.ReleaseComObject(reference) <= 0
Loop
Catch ex As COMException
Debug.WriteLine(ex.Message)
Finally
reference = Nothing
End Try
End Sub