1

In asp.net, it is quite easy to convert a datatable to an excel file. How do I do the same for datatables in winforms?

For Example: in my asp.net code, here is my function to convert the datatable to excel:

Public Shared Sub DataTableToExcel(ByVal dt As DataTable, ByVal FileName As String
  HttpContext.Current.Response.Clear()
  HttpContext.Current.Response.Write(Environment.NewLine)
  For Each row As DataRow In dt.Rows
    For i As Integer = 0 To dt.Columns.Count - 1
      HttpContext.Current.Response.Write(row(i).ToString().Replace(";", String.Empty) + ";")
    Next

    HttpContext.Current.Response.Write(Environment.NewLine)
  Next

  HttpContext.Current.Response.ContentType = "application/ms-excel"
  HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls")
  HttpContext.Current.Response.[End]()
End Sub

But in winforms, you cannot use the same as it is. You need to read out the datatable and create/open the excel workbook.

I wish there is a way to directly convert datatable used in winforms to excel fast.

Thanks.

Batuta
  • 1,674
  • 16
  • 45
  • 62
  • Possible duplicate of [c# (WinForms-App) export DataSet to Excel](http://stackoverflow.com/questions/373925/c-sharp-winforms-app-export-dataset-to-excel) – Himanshu Jansari Oct 08 '16 at 11:14

0 Answers0