1

I am unable to export my data into excel.
I have tried the suggestions on S/O, but have not had any luck.

        Dim sqlString As String = "spExportRateProfile" & Session("OfficeNumber") & "," & Session("SalesRepID")
        Dim conn As SqlConnection = New SqlConnection(Utils.GetConfigKey("ConnectionStringVimas"))
        conn.Open()
        Dim dt As DataTable = New DataTable()
        Dim da As SqlDataAdapter = New SqlDataAdapter(sqlString, conn)
        da.Fill(dt)

        Response.AddHeader("content-disposition", "attachment;filename=ReportExport.xlsx")
        Response.ContentType = "application/vnd.ms-excel"  

What do I need do after this to export my data to excel?

DNR
  • 3,706
  • 14
  • 56
  • 91
  • Try this link using ExcelLibrary: http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c – Csharp Oct 02 '11 at 21:05

2 Answers2

1

You could use a ExcelLibrary like EPPlus(GPL) which i can warmly recommend.

Then it is as easy as this to create Excel-Files from a DataTable and write it to the Response:

Dim pck = New ExcelPackage()
Dim ws = pck.Workbook.Worksheets.Add("Worksheet-Name")
ws.Cells("A1").LoadFromDataTable(dt, True, OfficeOpenXml.Table.TableStyles.Medium1)
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;  filename=ExcelFileName.xlsx")
Response.BinaryWrite(pck.GetAsByteArray())

Here is another example: http://epplus.codeplex.com/wikipage?title=WebapplicationExample

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

once you have your datatable dt here you should do this (C# - just copied from the Internet)

...
da.Fill(dt);

Response.ContentType = "Application/x-msexcel";
Response.AddHeader("content-disposition", "attachment; filename=\"test.csv\"");
Response.Write((new ExportXMLCSV()).ToCSV(dt));
Response.End();

and here the method ToCSV of the class ExportXMLCSV (C# - just copied from the Internet)

public string ToCSV(DataTable dataTable)
    {
        //create the stringbuilder that would hold our data
        StringBuilder sb = new StringBuilder();
        //check if there are columns in our datatable
        if (dataTable.Columns.Count != 0)
        {
            //loop thru each of the columns so that we could build the headers
            //for each field in our datatable
            foreach (DataColumn column in dataTable.Columns)
            {
                //append the column name followed by our separator
                sb.Append(column.ColumnName + ',');
            }
            //append a carriage return
            sb.Append("\r\n");
            //loop thru each row of our datatable
            foreach (DataRow row in dataTable.Rows)
            {
                //loop thru each column in our datatable
                foreach (DataColumn column in dataTable.Columns)
                {
                    //get the value for tht row on the specified column
                    // and append our separator
                    sb.Append(row[column].ToString() + ',');
                }
                //append a carriage return
                sb.Append("\r\n");
            }
        }
        //return our values
        return sb.ToString();
    }

everything just copied from here: http://forums.asp.net/t/1115305.aspx you should be good to go with a little exercise of conversion C# -> VB.NET ;-)

Davide Piras
  • 43,984
  • 10
  • 98
  • 147