Dim conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings.Item("conn"))
        Dim cmd As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand("抓取資料的SQL句", conn)
        conn.Open()
        Dim filename As String = Server.MapPath("GetExcel.xls")
        Dim dr As Data.SqlClient.SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        Dim sw As IO.StreamWriter = New IO.StreamWriter(filename, False, System.Text.Encoding.Default)
        Do While dr.Read()
            For i As Integer = 0 To dr.FieldCount - 1
                sw.Write(dr(i) & vbTab)
            Next
            sw.WriteLine()
        Loop
        sw.Close()
        dr.Close()

        Response.AddHeader("Content-Disposition", "attachment;filename=GetExcel.xls")
        Response.ContentType = "application/vnd.ms-excel"
        Response.WriteFile(filename)
        Response.End()


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Roger 的頭像
    Roger

    歡迎~ 程式設計 學生 作業 專題製作 程式外包 家教...

    Roger 發表在 痞客邦 留言(0) 人氣()