contents of datatable into excel spreadsheet in asp.net

  • 13 years ago
    need to look at a program that can put a datatable into an excel spreadsheet in asp.net 2.0 vb.net

    Customer      Shopper ID      Year      Period No      Period Name      Prestige Gross Sales      Prestige Net Sales      Prestige Discount      Nb. Prestige Transactions
    256039201      1      2005      12      Jan      64      50.15      -5.6      3

    this is a datatable using the viewer built in.

    Using Excel = Microsoft.Office.Interop.Excel;
    public class ExcelPush
    {
            Excel.ApplicationClass _excel = new Excel.ApplicationClass();

            public ExcelPush()
            {
                //
                // TODO: Add constructor logic here
                //
            }

            public Excel.Worksheet Activate()
            {
                // open new excel spreadsheet
                try
                {
                    Excel.Workbook workbook = _excel.Workbooks.Add(Type.Missing);
                    _excel.Visible = true;
                    Excel.Worksheet ws = (Excel.Worksheet)_excel.ActiveSheet;
                    ws.Activate();
                    return ws;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                return null;
            }


            public void AddItemToSpreadsheet(int row, int column, Excel.Worksheet ws, string item)
            {
                ((Excel.Range)ws.Cells[row, column]).Value2 = item;
            }
    }

       


    End Class

    Private Sub cmdProceed_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cmdProceed.Click
            Dim dt As New DataTable
            dt = _calendar.GetBusinessCalendarPeriod(txtPeriod.Text)
            Dim selectedConnectionString As String = String.Empty
            Dim selectedDBName As String = String.Empty
            Dim selectedServerName As String = String.Empty
            ' Get the correct connection string and database name
            If Not ddlDivisions.SelectedItem Is Nothing Then
                For Each div As Division In _divisions
                    If div.Name = ddlDivisions.SelectedItem.Text Then
                        selectedDBName = div.DatabaseName
                        Exit For
                    End If
                Next
            End If

            Dim rowNumber As Integer
            Dim NewDataTable As DataTable
            rowNumber = 0
            For Each rw As DataRow In dt.Rows
                NewDataTable = _customer.GetCustomerList(selectedDBName, rw)
              ****************************** Call excell routine.
            Next

        End Sub

     Dim rowNumber As Integer
            Dim NewDataTable As DataTable
            rowNumber = 0
            For Each rw As DataRow In dt.Rows
                NewDataTable = _customer.GetCustomerList(selectedDBName, rw)
              ****************************** Call excell routine.
            Next



















































































Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Beware of bugs in the above code; I have only proved it correct, not tried it.” - Donald Knuth