Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5995945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:01:59+00:00 2026-05-23T00:01:59+00:00

I want to export 2 gridviews to 2 worksheets in single excel file. and

  • 0

I want to export 2 gridviews to 2 worksheets in single excel file.
and also don’t want to use any other dll or utility or Interop assemblies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:01:59+00:00Added an answer on May 23, 2026 at 12:01 am

    I’ve just done that a few days a ago.

    Imports System.Data.SqlClient
    
    Public Class Export
    
    Inherits System.Web.UI.Page
    
    Dim myGridViews(2) As Object 'number of gridviews -1 
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myGrid As New GridView
        Dim myN As Integer = 0
        Dim myID As String
        Dim strq As String
        Dim cmd As New SqlCommand
        Dim dt As DataTable
    
        'load the first gridview
        myGrid = OneWorkBook
        myID = myGrid.ID
        strq = "SELECT * FROM " + myID
        cmd = New SqlCommand(strq)
        dt = Getdata(cmd)
        myGrid.DataSource = dt
        myGrid.DataBind()
        myGridViews(myN) = myGrid 'add this gridview to myGridViews, myN =0
        myN += 1
    
        'load the second gridview
        myGrid = TwoWorkBook
        myID = myGrid.ID
        strq = "SELECT * FROM " + myID
        cmd = New SqlCommand(strq)
        dt = Getdata(cmd)
        myGrid.DataSource = dt
        myGrid.DataBind()
        myGridViews(myN) = myGrid 'add this gridview to myGridViews, myN =1 
        myN += 1
    
        'load the third gridview
        myGrid = ThreeWorkBook
        myID = myGrid.ID
        strq = "SELECT * FROM " + myID
        cmd = New SqlCommand(strq)
        dt = Getdata(cmd)
        myGrid.DataSource = dt
        myGrid.DataBind()
        myGridViews(myN) = myGrid 'add this gridview to myGridViews, myN =2 
        myN += 1
    
    End Sub
    
    Protected Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
        CreateWorkBook(myGridViews, "ExportToExcel", 80)
    End Sub
    Public Shared Sub CreateWorkBook(ByVal cList As Object, ByVal wbName As String, ByVal CellWidth As Integer)
        Dim attachment As String = "attachment; filename=""" & wbName & ".xml"""
    
        HttpContext.Current.Response.ClearContent()
    
        HttpContext.Current.Response.AddHeader("content-disposition", attachment)
        HttpContext.Current.Response.ContentType = "application/ms-excel"
    
        Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
        sw.WriteLine("<?xml version=""1.0""?>")
    
        sw.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
        sw.WriteLine("<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""")
    
        sw.WriteLine("xmlns:o=""urn:schemas-microsoft-com:office:office""")
        sw.WriteLine("xmlns:x=""urn:schemas-microsoft-com:office:excel""")
    
        sw.WriteLine("xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""")
        sw.WriteLine("xmlns:html=""http://www.w3.org/TR/REC-html40"">")
    
        sw.WriteLine("<DocumentProperties xmlns=""urn:schemas-microsoft-com:office:office"">")
        sw.WriteLine("<LastAuthor>Try Not Catch</LastAuthor>")
    
        sw.WriteLine("<Created>2010-05-15T19:14:19Z</Created>")
        sw.WriteLine("<Version>11.9999</Version>")
    
        sw.WriteLine("</DocumentProperties>")
        sw.WriteLine("<ExcelWorkbook xmlns=""urn:schemas-microsoft-com:office:excel"">")
    
        sw.WriteLine("<WindowHeight>9210</WindowHeight>")
        sw.WriteLine("<WindowWidth>19035</WindowWidth>")
    
        sw.WriteLine("<WindowTopX>0</WindowTopX>")
        sw.WriteLine("<WindowTopY>90</WindowTopY>")
    
        sw.WriteLine("<ProtectStructure>False</ProtectStructure>")
        sw.WriteLine("<ProtectWindows>False</ProtectWindows>")
    
        sw.WriteLine("</ExcelWorkbook>")
        sw.WriteLine("<Styles>")
    
        sw.WriteLine("<Style ss:ID=""Default"" ss:Name=""Normal"">")
        sw.WriteLine("<Alignment ss:Vertical=""Bottom""/>")
    
        sw.WriteLine("<Borders/>")
        sw.WriteLine("<Font/>")
    
        sw.WriteLine("<Interior/>")
        sw.WriteLine("<NumberFormat/>")
    
        sw.WriteLine("<Protection/>")
        sw.WriteLine("</Style>")
    
        sw.WriteLine("<Style ss:ID=""s22"">")
        sw.WriteLine("<Alignment ss:Horizontal=""Center"" ss:Vertical=""Center"" ss:WrapText=""1""/>")
    
        sw.WriteLine("<Borders>")
        sw.WriteLine("<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("</Borders>")
    
        sw.WriteLine("<Font ss:Bold=""1""/>")
        sw.WriteLine("</Style>")
    
        sw.WriteLine("<Style ss:ID=""s23"">")
        sw.WriteLine("<Alignment ss:Vertical=""Bottom"" ss:WrapText=""1""/>")
    
        sw.WriteLine("<Borders>")
        sw.WriteLine("<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
    
        sw.WriteLine("ss:Color=""#000000""/>")
        sw.WriteLine("</Borders>")
    
        sw.WriteLine("</Style>")
        sw.WriteLine("<Style ss:ID=""s24"">")
    
        sw.WriteLine("<Alignment ss:Vertical=""Bottom"" ss:WrapText=""1""/>")
        sw.WriteLine("<Borders>")
    
        sw.WriteLine("<Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
        sw.WriteLine("ss:Color=""#000000""/>")
    
        sw.WriteLine("<Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
        sw.WriteLine("ss:Color=""#000000""/>")
    
        sw.WriteLine("<Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
        sw.WriteLine("ss:Color=""#000000""/>")
    
        sw.WriteLine("<Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1""")
        sw.WriteLine("ss:Color=""#000000""/>")
    
        sw.WriteLine("</Borders>")
        sw.WriteLine("<Font ss:Color=""#FFFFFF""/>")
    
        sw.WriteLine("<Interior ss:Color=""#FF6A6A"" ss:Pattern=""Solid""/>") 'set header colour here
        sw.WriteLine("</Style>")
    
        sw.WriteLine("</Styles>")
        For Each gView As GridView In cList
            CreateWorkSheet(gView.ID.ToString, sw, gView, CellWidth)
        Next
        sw.WriteLine("</Workbook>")
        HttpContext.Current.Response.Write(sw.ToString())
        HttpContext.Current.Response.End()
    
    End Sub
    Private Shared Sub CreateWorkSheet(ByVal wsName As String, ByVal sw As System.IO.StringWriter, ByVal gv As GridView, ByVal cellwidth As Integer)
        If IsNothing(gv.HeaderRow) = False Then
            sw.WriteLine("<Worksheet ss:Name=""" & wsName & """>")
            Dim cCount As Integer = gv.HeaderRow.Cells.Count
            Dim rCount As Long = gv.Rows.Count + 1
            sw.WriteLine("<Table ss:ExpandedColumnCount=""" & cCount & """ ss:ExpandedRowCount=""" & rCount & """ x:FullColumns=""1""")
            sw.WriteLine("x:FullRows=""1"">")
            For i As Integer = (cCount - cCount) To (cCount - 1)
                sw.WriteLine("<Column ss:AutoFitWidth=""1"" ss:Width=""" & cellwidth & """/>")
            Next
    
            GridRowIterate(gv, sw)
            sw.WriteLine("</Table>")
            sw.WriteLine("<WorksheetOptions xmlns=""urn:schemas-microsoft-com:office:excel"">")
    
            sw.WriteLine("<Selected/>")
            sw.WriteLine("<DoNotDisplayGridlines/>")
    
            sw.WriteLine("<ProtectObjects>False</ProtectObjects>")
            sw.WriteLine("<ProtectScenarios>False</ProtectScenarios>")
    
            sw.WriteLine("</WorksheetOptions>")
            sw.WriteLine("</Worksheet>")
        End If
    End Sub
    Private Shared Sub GridRowIterate(ByVal gv As GridView, ByVal sw As System.IO.StringWriter)
        sw.WriteLine("<Row>")
    
        For Each tc As TableCell In gv.HeaderRow.Cells
            Dim tcText As String = tc.Text
    
            Dim tcWidth As String = gv.Width.Value
            Dim dType As String = "String"
    
            If IsNumeric(tcText) = True Then
    
                dType = "Number"
    
            End If
            sw.WriteLine("<Cell ss:StyleID=""s24""><Data ss:Type=""String"">" & tcText & "</Data></Cell>")
    
        Next
        sw.WriteLine("</Row>")
    
        For Each gr As GridViewRow In gv.Rows
            sw.WriteLine("<Row>")
    
            For Each gc As TableCell In gr.Cells
                Dim gcText As String = gc.Text
                Dim dType As String = "String"
    
                If IsNumeric(gcText) = True Then
    
                    dType = "Number"
                    gcText = CDbl(gcText)
    
                End If
                sw.WriteLine("<Cell ss:StyleID=""s23""><Data ss:Type=""" & dType & """>" & gcText & "</Data></Cell>")
    
            Next
            sw.WriteLine("</Row>")
        Next
    
    End Sub
    Private Function Getdata(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable()
        Dim strcon As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As New SqlConnection(strcon)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt
        Catch ex As Exception
            Throw (ex)
        Finally
            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try
    
    End Function
    

    End Class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to export data from Dataset or Data Table to Excel file in
I want to export the data from a DaraGrid into an Excel file. Having
I want to save excel file which export data of grid view. I have
I want to export the audio-media of a MOV-File with the QuickTime-API and save
I want to export jar file with existing jar file in the project by
I am want to export database table to a file. I am using the
I have a huge IQueryable that I want to export directly to Excel for
I've got a load of DataTables that I want to export into several Excel
I want to export the database to CSV file and read in MatLab. I
I want to export some data from my jruby on rails webapp to excel,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.