Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Problem when exporting formview to Excel

$
0
0

I'm using Visual Web Developer 2010 Express with SQL Server 2008 Express, VB.

I'm having a problem when exporting formview to Excel.  The export occurs without a problem except for two formview labels.  The first label; Package_Size contains data such as 0402 or 0603 or 0805.  The export drops the leading 0 and shows as 402 or 603 or 805.  The second label; UserField02 (containing ECO numbers) contains data such as 4380,4381,4382 (3 ECO numbers).  When this exports it appears as 438,043,814,382.  It appears that even though both fields are string they are for some reason being converted to integer in the export process.  The same formview prints to pdf and exports correctly to Word.  I've tried everything I can think of (and even just wild guesses) but have not found a solution.  I tried changing Labels to textboxes and that solved the format issue but I do not like the way Labels appear in Excel.

VB code:

    Protected Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click     'Export to Excel
        Dim style As String = "<style> .text { mso-number-format:\@; } </style> "

        If FormView1.CurrentMode = FormViewMode.ReadOnly Then
            Dim lblpkg = DirectCast(FormView1.FindControl("Package_SizeLabel"), Label)
            lblpkg.Attributes.Add("class", "text")
        End If

        Dim lbl1 As Label = FormView1.FindControl("PART_NUMBERLabel")
        Response.ClearContent()
        Response.Buffer = True
        Response.AddHeader("content-disposition", String.Format("attachment; filename={0}.xls", lbl1.Text))
        Response.ContentType = "application/vnd.ms-excel"
        Dim stringWriter As New StringWriter()
        Dim stringWriter1 As New StringWriter()
        Dim htmlTextWriter As New HtmlTextWriter(stringWriter)
        Dim htmlTextWriter1 As New HtmlTextWriter(stringWriter1)
        FormView1.DataBind()
        FormView1.HeaderRow.Style.Add("background-color", "#FFFFFF")    'white; middle blue #3399FF
        For index As Integer = 0 To FormView1.HeaderRow.Cells.Count - 1
            FormView1.HeaderRow.Cells(index).Style.Add("background-color", "#969696")   'orange, was #d17250
        Next
        Dim index2 As Integer = 1
        FormView1.RenderControl(htmlTextWriter)
        Response.Write(style)
        Response.Write(stringWriter.ToString())
        If FormView2.DataItemCount <> 0 Then
            FormView2.DataBind()
            FormView2.HeaderRow.Style.Add("background-color", "#FFFFFF")    'white; middle blue #3399FF
            For index As Integer = 0 To FormView2.HeaderRow.Cells.Count - 1
                FormView2.HeaderRow.Cells(index).Style.Add("background-color", "#969696")   'orange, was #d17250
            Next
            index2 = 1
            FormView2.RenderControl(htmlTextWriter1)
            Response.Write(style)
            Response.Write(stringWriter1.ToString())
        End If
        Response.[End]()

    End Sub

asp for Package_Size label:

<td class="style41" style="border: 1px solid #000000"><asp:Label ID="Package_SizeLabel" runat="server" 
                        Text='<%# Bind("Package_Size") %>' Width="250px" />

asp for UserField02 label (used for ECO numbers):

<td class="style41" style="border: 1px solid #000000"><asp:Label ID="UserField02Label" runat="server" 
                        Text='<%# Bind("UserField02") %>' Width="250px" />

Has anybody run across this problem before and know how to solve it?

Thanks!


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>