Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of queries then every queries I want to export to different worksheets. I use StreamWriter for fast transfer to CSV file. (see sample code below)

VB
Dim _listOfQrys As New List(Of String)

  _listOfQrys.Add("SELECT * FROM Table1")
  _listOfQrys.Add("SELECT * FROM Table2")
  _listOfQrys.Add("SELECT * FROM Table3")


  For Each _query As String In _listOfQrys

      Dim _dataAdap As New SqlDataAdapter(_query, Constr)
      Dim _dataTbl As New DataTable

      _dataTbl.Clear()
      _dataAdap.Fill(_dataTbl)

      If _dataTbl.Rows.Count > 0 Then

          Dim headers = (From header As DataGridViewColumn In _dataTbl.Columns.Cast(Of DataGridViewColumn)() _
             Select header.HeaderText).ToArray

          Dim rows = From row As DataGridViewRow In _dataTbl.Rows.Cast(Of DataGridViewRow)() _
                     Where Not row.IsNewRow _
                     Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))

          Using sw As New IO.StreamWriter("sample.csv")
              sw.WriteLine(String.Join(",", headers))
              For Each r In rows
                  sw.WriteLine(String.Join(",", r))
              Next
          End Using

      End If

  Next



My question is how to export those _ListofQrys to different worksheets?

Any alternatives or other solutions is much appreciated. thanks!
Posted

1 solution

The *.csv format does not support worksheets!

Maybe you can try this:
A free "Export to Excel" C# class, using OpenXML[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900