Is it possible to execute SQL from Query Analyzer and have the results go an Excel Spreadsheet?

  • I need to create a Excel spreadsheet from a SQL statement. The spreadsheet should be an open spread with the contents from the SQL statement.

    Excel is 2000

    MDE is 2003

  • Run your query from Excel.

    Use Data -> Import External Data -> New Database Query

    _____________
    Code for TallyGenerator

  • Thank you for you reply. My mistake was not stating that the spreadsheet is started from vb.net 2003. Sorry bout that.

  • timeline (4/7/2009)


    Thank you for you reply. My mistake was not stating that the spreadsheet is started from vb.net 2003. Sorry bout that.

    if you are doing it from VB.NET then it is very easy. Populate the dataset and export the dataset table to Excel sheet.

    check out the below link

    http://www.dotnetjohn.com/articles.aspx?articleid=36

  • For vb.net I've used something like this in the past. It has worked well. Hope it helps. Grady Christie

    '//

    Public Sub DataTableToExcel(ByVal dt As DataTable)

    Dim filename As String = "CFLCollections_" & _

    String.Format("{0:MM-dd-yyy-hh-mm}", Now) & _

    ".xls"

    Dim dge As New DataGrid

    dge.EnableViewState = False

    dge.GridLines = GridLines.Both

    dge.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(208, 208, 208)

    dge.HeaderStyle.Font.Bold = True

    dge.DataSource = dt

    dge.DataBind()

    HttpContext.Current.Response.Clear()

    HttpContext.Current.Response.Buffer = True

    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & filename)

    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"

    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8

    HttpContext.Current.Response.Charset = ""

    Dim sw As New StringWriter

    Dim hw As New HtmlTextWriter(sw)

    dge.RenderControl(hw)

    dge.Dispose()

    HttpContext.Current.Response.Write(sw)

    HttpContext.Current.Response.End()

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply