Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to open excel file after export it
i have export excel file in directory but not open automaticaly
hear is code

VB
Private Sub ExportReport(ByVal dr As SqlDataReader, ByVal Header As String)
        Try
            Dim dg As New DataGrid()

            Dim dtSchema As DataTable = dr.GetSchemaTable()
            Dim dt As DataTable = New DataTable()
            ' You can also use an ArrayList instead of List<>
            Dim listCols As List(Of DataColumn) = New List(Of DataColumn)()
            Dim Count As Integer = 0
            If Not dtSchema Is Nothing Then
                For Each drow As DataRow In dtSchema.Rows
                    If Count < dtSchema.Rows.Count - 3 Then
                        Dim columnName As String = System.Convert.ToString(drow("ColumnName"))
                        Dim column As DataColumn = New DataColumn(columnName)
                        listCols.Add(column)
                        dt.Columns.Add(column)
                    Else
                        Exit For
                    End If
                    Count += 1
                Next drow
            End If

            ' Read rows from DataReader and populate the DataTable
            Do While dr.Read()
                Dim dataRow As DataRow = dt.NewRow()
                For i As Integer = 0 To listCols.Count - 1
                    dataRow((CType(listCols(i), DataColumn))) = dr(i)
                Next i
                dt.Rows.Add(dataRow)
            Loop


            Dim dsExport As New DataSet()
            Dim tw As New System.IO.StringWriter()
            Dim hw As New System.Web.UI.HtmlTextWriter(tw)
            Dim dgGrid As New DataGrid()
            dgGrid.DataSource = dt

            ' hw.WriteLine(Request.MapPath("."))

            'Report Header
            'hw.WriteLine("<b>" & Header & "</b>")
            'hw.WriteLine("<br />")
            'hw.WriteLine(JobTitle)
            'hw.WriteLine("<br />")
            'hw.WriteLine("<b>Posted On :</b>  " & PostedOn)
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
            hw.WriteLine("<b><font size='5'> " & Header & " </font></b>")
            hw.WriteBreak()
            hw.WriteBreak()
            '   Get the HTML for the control.
            dgGrid.HeaderStyle.Font.Bold = True
            dgGrid.HeaderStyle.ForeColor = Drawing.Color.White
            dgGrid.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#7d6221")
            dgGrid.HeaderStyle.CssClass = "fixed"
            dgGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            dgGrid.HeaderStyle.Height = Unit.Pixel(100)
            dgGrid.HeaderStyle.VerticalAlign = VerticalAlign.Top
            dgGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
            dgGrid.ItemStyle.VerticalAlign = VerticalAlign.Top
            dgGrid.ItemStyle.HorizontalAlign = HorizontalAlign.Left
            dgGrid.DataBind()
            dgGrid.RenderControl(hw)

            Dim strPath As String = Server.MapPath("~") & "\PDFFile\"
            Dim dir As New System.IO.DirectoryInfo(strPath)
            Dim ArrayFile As Array = dir.GetFiles()

            'Dim dt1 As DateTime = DateTime.UtcNow
            'Dim dt2 As DateTime = Now

            'Dim t As TimeSpan = dt1 - dt2

            'For Each File1 In ArrayFile
            '    If File1.CreationTime < Now.AddMinutes(-5) Then
            '        System.IO.File.Delete(File1.fullname)
            '    End If

            'Next


            Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
            Dim Path As String = "Tanker Position Report" & ".xls"
            Dim str As String = strPath & Path
            ' System.IO.File.WriteAllText(strPath & Path, style & tw.ToString())
            System.IO.File.WriteAllText(str, style & tw.ToString())


            Response.Clear()
            Response.Write(tw.ToString())
            Response.End()

        Catch ex As Exception
            Throw ex

        End Try


    End Sub
Posted

This is in c#.
it will be also in VB.net.
try
C#
System.Diagnostics.Process.Start("path of excel file with extension");
 
Share this answer
 
Comments
DINESH K MAURYA 12-Sep-11 4:45am    
it work nice but my html page in which i defing botton get blank
uspatel 12-Sep-11 5:54am    
eleborate more.
Use this code to open the excel file :

VB
'Place the path of the file in file variable
Dim file As String = "C:\test.xls"

'This will open the file if file exists
If System.IO.File.Exists(file) Then
	System.Diagnostics.Process.Start(file)
End If


Hope this helps.
All the best.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900