在数据库中导出后如何打开Excel文件 [英] how to open excel file after exporting in database

查看:69
本文介绍了在数据库中导出后如何打开Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导出后如何打开excel文件
我在目录中导出了excel文件,但没有自动打开
听到的是代码

how to open excel file after export it
i have export excel file in directory but not open automaticaly
hear is code

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

推荐答案

这在c#中.
也将在VB.net中.
试试
This is in c#.
it will be also in VB.net.
try
System.Diagnostics.Process.Start("path of excel file with extension");


使用以下代码打开excel文件:

Use this code to open the excel file :

'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.


这篇关于在数据库中导出后如何打开Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆