当前位置:编程学习 > VB >>

VB.NET創建并下載Excel;

Private Sub PrintExcel()
        Dim time As String = DateTime.Now.ToString("yyyyMMddHHmmss")

        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet

        xlApp = CreateObject("Excel.Application")
        xlBook = xlApp.Workbooks.Add
        xlSheet = xlBook.Worksheets(1)
        xlSheet.Activate()

        xlBook.SaveAs(Server.MapPath("\TmpReport\tmp" & time & "1.xls"))

        xlBook.Close()
        xlApp.Quit()

        xlApp = Nothing
        xlBook = Nothing
        xlSheet = Nothing

        Response.Expires = 0
        Response.Clear()
        Response.Buffer = True
        Response.Charset = "utf-8"
        'Response.ContentEncoding = System.Text.Encoding.UTF8
        Response.ContentEncoding = System.Text.Encoding.Unicode
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment; filename=" & HttpUtility.UrlEncode(time & ".xls"))
        Response.WriteFile(Server.MapPath("\TmpReport\tmp" & time & "1.xls"))
        Response.Flush()
        Response.Clear()
    End Sub

WEB訪問,使用上面代碼生成空Excel、然後提示下載!
保存下來的Excel有HTML代碼、應該是生成Excel頁面本身的HTML,
有什麽辦法解決!求指教!! VB Excel
补充:VB ,  网络编程
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,