Window Services for creating Excel File

Mar 25 2011 9:35 AM
Hi,
My name si Marian from Timisoara City, Roamania and i salute everybody from this forum, is very, very helpful
I have one big problem for me and maybe little for you

I want to create a windows services which must create one excel file and send this file via email

I dont find the solution, if i write my code in normal windows app its work very fine, but when i put my code in one windows service nothing, only a lot of error
I put my code here, if some one can help me or if someone know some tutorials for my problem.

*****  CODE  **********************************************************************

Imports System.IO
Imports System.Net.Mail
Imports System.Runtime.InteropServices.Marshal
Imports System.Data
Public Class EntReportMailSender
    Dim WithEvents mvTimer As System.Timers.Timer

    '*********************************************************************
    Protected Overrides Sub OnStart(ByVal args() As String)
        mvTimer = New System.Timers.Timer(10 * 1000)
        mvTimer.Enabled = True
        FfTestDatatableTableAdapter.Fill(EntDataSetxsd.ffTestDatatable)
    End Sub

    '*********************************************************************
    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

    '*********************************************************************

    Private Sub tmrRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles tmrRefresh.Tick
    End Sub

    '*********************************************************************

    Private Sub mvTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mvTimer.Elapsed

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet1 As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet1 = xlWorkBook.Sheets("sheet1")

        'xlWorkSheet1.Cells(1, 1) = DefectDataGridView.Item(0, 0).Value.ToString
        'xlWorkSheet1.Cells(1, 1) = TestGrid.Columns(0).HeaderText
        'xlWorkSheet1.Cells(1, 2) = TestGrid.Columns(1).HeaderText


        For i = 0 To AdrLogDgw.RowCount - 2
            For j = 0 To AdrLogDgw.ColumnCount - 1
                xlWorkSheet1.Cells(i + 2, j + 1) = AdrLogDgw(j, i).Value.ToString()
            Next
        Next

            xlWorkSheet1.Name = "Daily"
            xlWorkSheet1.SaveAs("C:\Report.xls")
            xlWorkBook.Close()
            xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet1)

    End Sub
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

****  END OFF CODE  *****************************

Thanks !


Answers (1)