Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
given code is working fine but it generate report less than one record from starting.

code here
VB
Private Sub ExportReport(ByVal dr As SqlDataReader, ByVal type As Integer, ByVal Header As String)
        Dim iMileStone As Integer = 0
        Try

            sessionBusinessType = Session("BusinessID")
            iMileStone = 1010
            Dim Report As String = sessionBusinessType
            Dim dg As New DataGrid()
            iMileStone = 1011

            Dim dtSchema As System.Data.DataTable = dr.GetSchemaTable()


            iMileStone = 10111
            Dim dt As System.Data.DataTable = New DataTable()
            iMileStone = 10112
            ' You can also use an ArrayList instead of List<>
            Dim listCols As List(Of DataColumn) = New List(Of DataColumn)()
            iMileStone = 10113
            Dim Count As Integer = 0
            Dim remove As Integer = 0
            Dim ColumnValue As Integer = 0
            iMileStone = 10114
            If Not dtSchema Is Nothing Then
                iMileStone = 1012
                For Each drow As DataRow In dtSchema.Rows
                    Dim columnName As String = System.Convert.ToString(drow("ColumnName"))
                    Select Case columnName

                        Case "ETADisport"
                            columnName = "ETA"

                        Case "Vessel"
                            columnName = "Vessel"
                        Case "Dwt"
                            columnName = "Dwt"
                        Case "Built"
                            columnName = "Built"
                        Case "HullType"
                            columnName = "Hull"
                        Case "Disport"
                            columnName = "Disport"
                        Case "Owner"
                            columnName = "Owner"
                    End Select
                    iMileStone = 1013
                    Dim column As DataColumn = New DataColumn(columnName)
                    listCols.Add(column)
                    dt.Columns.Add(column)
                    Count = Count + 1
                    iMileStone = 1014
                Next drow
            End If
            iMileStone = 1015
            ' Read rows from DataReader and populate the DataTable


            Dim i As Integer

            While dr.Read()
                Dim dataRow As DataRow = dt.NewRow
                For i = 0 To listCols.Count - 1
                    dataRow((CType(listCols(i), DataColumn))) = dr(i)
                Next i
                dt.Rows.Add(dataRow)
            End While

        

            iMileStone = 1016
            If type = 1 Then
                'for .xls 


                Dim dsExport As New DataSet()
                Dim tw As New System.IO.StringWriter()
                Dim hw As New System.Web.UI.HtmlTextWriter(tw)
                Dim dgGrid As New DataGrid()
                dgGrid.DataSource = dt
                iMileStone = 1017
                'Report Header
                hw.WriteLine("           ")
                hw.WriteLine("           ")
                hw.WriteLine("           ")
                hw.WriteLine("           ")

                hw.WriteLine("<font size="4"> " & Header & " </font>")
                hw.WriteBreak()
                hw.WriteBreak()
                iMileStone = 1018
                '   Get the HTML for the control.
                dgGrid.HeaderStyle.Font.Bold = True
                dgGrid.HeaderStyle.BackColor = Drawing.Color.DarkSeaGreen
                dgGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
                dgGrid.DataBind()
                dgGrid.RenderControl(hw)
                iMileStone = 1019
                Dim strPath As String = Server.MapPath("~") & "\Report\"
                Dim dir As New System.IO.DirectoryInfo(strPath)
                Dim ArrayFile As Array = dir.GetFiles()


                For Each File1 In ArrayFile
                    If File1.CreationTime < Now.AddMinutes(-5) Then
                        System.IO.File.Delete(File1.fullname)
                    End If

                Next

                Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
                Dim Path As String = DateTime.UtcNow.AddHours(5).AddMinutes(30).ToString("dd-MMM-yyyy-HH-mm-ss") & ".xls"
                System.IO.File.WriteAllText(strPath & Path, style & tw.ToString())


''==========for html to excel==============
VB
Response.ContentType = "application/vnd.ms-excel"
              iMileStone = 1012
              Me.EnableViewState = False
              Response.Clear()
              Response.Write(tw.ToString())
              iMileStone = 1021
              HttpContext.Current.ApplicationInstance.CompleteRequest()
              iMileStone = 1022
              Response.[End]()
              iMileStone = 1023
          Else
              iMileStone = 1024  ' For Word
          End If
          dr.Close()
      Catch ex As Exception
          Logs.WriteErrorLog("TankerPositionPreview.aspx.vb | ExportReport " + ex.Message + iMileStone.ToString)
      End Try

  End Sub

Please help me
Posted
Updated 30-Jan-12 21:45pm
v2
Comments
uspatel 31-Jan-12 3:46am    
pre tag added.

see also belowing link..

One click ASP.NET export button control for Excel/CSV file[^]

VB
<pnwc:exportbutton id="btnExcel"  runat="server" xmlns:pnwc="#unknown">
  Separator="TAB" Text="Export to Excel" 
  FileNameToExport="Books.xls" BackColor="#E0E0E0"></pnwc:exportbutton>
….
….

'Code Behind [Run time]
    Private Sub Page_Load(ByVal sender As System.Object,_
             ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds as dataset=filldataset()
        dgBooks.DataSource = ds.Tables("Books")
        dgBooks.DataBind()
            
        'Set Export button properties 
        btnExcel.Dataview = ds.Tables("Books").DefaultView
        btnExcel.FileNameToExport = "Books.xls"
        btnExcel.ExportType = _
          PNayak.Web.UI.WebControls.ExportButton.ExportTypeEnum.Excel
        btnExcel.Separator = _
          PNayak.Web.UI.WebControls.ExportButton.SeparatorTypeEnum.TAB

    End Sub
 
Share this answer
 
v2
C#
private void ExportDataTable(DataTable dt)
        {
            string attachment = "attachment; filename=a.xls";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            string sTab = "";
            foreach (DataColumn dc in dt.Columns)
            {
                HttpContext.Current.Response.Write(sTab + dc.ColumnName);
                sTab = "\t";
            }
            HttpContext.Current.Response.Write("\n");

            int i;
            foreach (DataRow dr in dt.Rows)
            {
                sTab = "";
                for (i = 0; i < dt.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write(sTab + dr[i].ToString());
                    sTab = "\t";
                }
                HttpContext.Current.Response.Write("\n");
            }
            HttpContext.Current.Response.End();
        }
 
Share this answer
 
You can refer to


[^]
 
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