none
error : missing css file when exporting to excel

    Question

  • I have no idea why but I just cannot export to excel anymore. And I was able to do that. this is weird. And I have this kind of error: missing file:...path, and it's a css file. What should I do?

    Thanks a lot

    • Edited by ziara Thursday, August 05, 2010 9:56 AM incomplete
    Wednesday, August 04, 2010 1:27 PM

Answers

  • problem solved. this is my code: Public Class GridViewExportUtil

        Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
            HttpContext.Current.Response.ContentType = "application/ms-excel"
            Dim sw As StringWriter = New StringWriter
            Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
            '  Create a form to contain the grid
            Dim table As Table = New Table
            table.GridLines = gv.GridLines
            '  add the header row to the table
            If (Not (gv.HeaderRow) Is Nothing) Then
                GridViewExportUtil.PrepareControlForExport(gv.HeaderRow)
                table.Rows.Add(gv.HeaderRow)
            End If
            '  add each of the data rows to the table
            For Each row As GridViewRow In gv.Rows
                GridViewExportUtil.PrepareControlForExport(row)
                table.Rows.Add(row)
            Next
            '  add the footer row to the table
            If (Not (gv.FooterRow) Is Nothing) Then
                GridViewExportUtil.PrepareControlForExport(gv.FooterRow)
                table.Rows.Add(gv.FooterRow)
            End If
            '  render the table into the htmlwriter
            table.RenderControl(htw)
            '  render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString)
            HttpContext.Current.Response.End()
        End Sub

        ' Replace any of the contained controls with literals
        Private Shared Sub PrepareControlForExport(ByVal control As Control)
            Dim i As Integer = 0
            Do While (i < control.Controls.Count)
                Dim current As Control = control.Controls(i)
                If (TypeOf current Is LinkButton) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
                ElseIf (TypeOf current Is ImageButton) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
                ElseIf (TypeOf current Is HyperLink) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
                ElseIf (TypeOf current Is DropDownList) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
                ElseIf (TypeOf current Is CheckBox) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
                    'TODO: Warning!!!, inline IF is not supported ?
                End If
                If current.HasControls Then
                    GridViewExportUtil.PrepareControlForExport(current)
                End If
                i = (i + 1)
            Loop
        End Sub
    End Class
    Protected Sub BtnExportGrid_Click(sender As Object, args As EventArgs)
     If Me.RadioButtonList1.SelectedIndex = 1 Then
      '  the user wants all rows exported, turn off paging
      '  and rebing the grid before sending it to the export
      '  utility
      Me.GridView1.AllowPaging = False
      Me.GridView1.DataBind()
     ElseIf Me.RadioButtonList1.SelectedIndex = 2 Then
      '  the user wants just the first 100,
      '  adjust the PageSize and rebind
      Me.GridView1.PageSize = 100
      Me.GridView1.DataBind()
     End If

     '  pass the grid that for exporting ...
     GridViewExportUtil.Export("YourFileName.xls", Me.GridView1)
    End Sub

    • Marked as answer by ziara Tuesday, August 10, 2010 6:16 AM
    Tuesday, August 10, 2010 6:15 AM

All replies

  • Hi,

    Can you tell us what you are going to export to excel?


    Microsoft Online Community Support
    Friday, August 06, 2010 6:02 AM
  • oh yes... I forgot about that.:) I am exporting a gridview, and the data inside is from a sql table.

    I even copyed the _layouts folder from the web server on my c: but still nothing works.i have the same error.

    Thanks

    Friday, August 06, 2010 7:44 AM
  • Hi,

    I don't know why you get the error.

    But you can try the following code and let me know whether it works. Thanks.

     public void Export(string fileName, GridView gv)
      {
        Response.Clear();    
    
        Response.ContentEncoding = System.Text.Encoding.UTF8;
    
        Response.AppendHeader("Content-Disposition", "attachment;filename=" +
           HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.ContentType = "application/ms-excel"; //"application/ms-word"; 
    
        using (StringWriter sw = new StringWriter())
        {
          using (HtmlTextWriter htw = new HtmlTextWriter(sw))
          {
            gv.RenderControl(htw);      
    
            // render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
          }
        }
      }
    

    Microsoft Online Community Support
    Friday, August 06, 2010 10:57 AM
  • hello, and thanks. I managed to fix that problem somehow... \i have no idea what happened.but n ow I have a different problem: it saves an empty file. i have nothing in my excel file. Here's my code:

    Sub doExcel(Source as Object, E as EventArgs)
    If GridView1.Rows.Count.ToString + 1 < 65536 Then
    GridView1.AllowPaging="False"
    GridView1.DataBind
    Dim tw As New StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    Dim frm As HtmlForm = new HtmlForm()
    Response.AddHeader("content-disposition", "attachment;filename=" & txtFile.text & ".xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    EnableViewState = False
    Controls.Add(frm)
    frm.Controls.Add(GridView1)
    frm.RenderControl(hw)
    Response.Write(tw.ToString())
    Response.End()
    GridView1.AllowPaging="True"
    GridView1.Databind
    Else
    LblError.Text="Too many rows - Export to Excel not possible"
    End If
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)  
    End Sub
    what is the problem now?

    Monday, August 09, 2010 5:11 AM
  • Hi,

    What's the 'frm'? It's not necessary to add the gridview into the 'frm'. Maybe it's the reason that you get empty file.

    Then you forget to specify data source for the GridView1. Because you bind it again.

    And the other thing is that it's invalid that you append some code to the 'Response.End()'.  You can try the following solution.

    Sub doExcel(Source as Object, E as EventArgs)
    If GridView1.Rows.Count.ToString + 1 < 65536 Then
    GridView1.AllowPaging="False"
    
    GridView1.DataSource=YourDataSource
    
    GridView1.DataBind
    Dim tw As New StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    Dim frm As HtmlForm = new HtmlForm()
    Response.AddHeader("content-disposition", "attachment;filename=" & txtFile.text & ".xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    
    
    GridView1.RenderControl(hw)
    Response.Write(tw.ToString())
    Response.End()
    
    Else
    LblError.Text="Too many rows - Export to Excel not possible"
    End If
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) 
    End Sub 
    

     


    Microsoft Online Community Support
    • Edited by KeFang Chen Monday, August 09, 2010 8:01 AM forget to specify data source
    Monday, August 09, 2010 7:55 AM
  • thanks a lot

    well it doesn't work. after clicking the button it gives me the error page with unexpected error message.

     

    Even without the frm it gives me the empty file:(

     

    • Edited by ziara Monday, August 09, 2010 8:26 AM incomplete
    Monday, August 09, 2010 8:25 AM
  • Hi,

    Can you tell me what the error is and did you specify the data source for the GridView1 again?


    Microsoft Online Community Support
    Monday, August 09, 2010 8:27 AM
  • yes I did specify the datasource  and this is the error
     
    An unexpected error has occurred.
    Monday, August 09, 2010 8:34 AM
  • oh.. and if i use this line : GridView1.RenderControl(hw) i get the missing css file again
    Monday, August 09, 2010 8:41 AM
  • Hi,

    If you still have that unknown error message, try to add the following code to your page to disable the property EnableEventValidation.

    <%@ Page Language="C#" EnableEventValidation="false" %>
    

    If the error exists again, please modify your web.config to show the error message.

    <system.web>
     <customErrors mode="Off"></customErrors>
    </system.web>

    Then if you want to keep the style which is used to render the GridView, you can add it to the 'frm' and try it again.

    I  am not sure the 'frm' can help you resove the issue.

    Finally ,if the most importance is to export the data of the GridView to excel and you don't concern the style, please let me know.

    Thanks. 


    Microsoft Online Community Support
    • Edited by KeFang Chen Monday, August 09, 2010 9:19 AM
    Monday, August 09, 2010 8:51 AM
  • The enableeventvalidation attribute on the page directive is not allowed in this page.  this is the error that i get if i use EnableEventValidation="false"

    I will try and and change the web.config.

    but i have a problem. whn specigying the datasource is wriet SQLDataSource1 or the datasource that is pecifyied in the connection string(with this last one it says that it is not declared)

    thanks a lot

    Monday, August 09, 2010 9:05 AM
  • but i have a problem. whn specigying the datasource is wriet SQLDataSource1 or the datasource that is pecifyied in the connection string(with this last one it says that it is not declared)

    Hi,

    I didn't understand clearly.

    If you mean you specify the  SqlDataSource as the  data source of the GridView, it's not necessary to specify the data source again.

    Or else you have to do that.

     


    Microsoft Online Community Support
    • Edited by KeFang Chen Monday, August 09, 2010 10:03 AM
    Monday, August 09, 2010 9:28 AM
  • oh ok... sorry. I say just now these mistakes... but you managed to understand. Thanks.
    Monday, August 09, 2010 10:01 AM
  • here is my last version of vb code. this is what works up until I open the file and it's empty. if i uncomment what has been comented then I get error pages- an unexpected error has occured. And i do not have the : EnableEventValidation="false" because i het : The enableeventvalidation attribute on the page directive is not allowed in this page.  this is the error that i get if i use EnableEventValidation="false"

    Sub doExcel(Source as Object, E as EventArgs)
    If GridView1.Rows.Count.ToString + 1 < 65536 Then
    GridView1.AllowPaging="False"
    'GridView1.DataSource=SqlDataSource1
    GridView1.DataBind
    Dim tw As New StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    Response.AddHeader("content-disposition", "attachment;filename=" & txtFile.text & ".xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Response.Write(tw.ToString())
    Response.End()
    GridView1.AllowPaging="True"
    'GridView1.DataSource=SqlDataSource1
    GridView1.Databind
    'Else
    'LblError.Text="Too many rows - Export to Excel not possible"
    End If
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    End Sub

    Monday, August 09, 2010 10:34 AM
  • Hi,

    You didn't render the GridView1 to the HtmlTextWriter.

    GridView1.RenderControl(hw)
    

    Please refer to the following solution.

    Sub doExcel(Source as Object, E as EventArgs)
    If GridView1.Rows.Count.ToString + 1 < 65536 Then
    GridView1.AllowPaging=False
    'GridView1.DataSource=SqlDataSource1
    GridView1.DataBind()
    Dim tw As New StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    Response.AddHeader("content-disposition", "attachment;filename=" & txtFile.text & ".xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel" 
    GridView1.RenderControl(hw)
    Response.Write(tw.ToString())
    Response.End()
    End If
    End Sub
    

    Microsoft Online Community Support
    Tuesday, August 10, 2010 1:19 AM
  • Hello,

    But if I render the Gridview! I get the missing css file.:(

    Tuesday, August 10, 2010 5:28 AM
  • problem solved. this is my code: Public Class GridViewExportUtil

        Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
            HttpContext.Current.Response.ContentType = "application/ms-excel"
            Dim sw As StringWriter = New StringWriter
            Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
            '  Create a form to contain the grid
            Dim table As Table = New Table
            table.GridLines = gv.GridLines
            '  add the header row to the table
            If (Not (gv.HeaderRow) Is Nothing) Then
                GridViewExportUtil.PrepareControlForExport(gv.HeaderRow)
                table.Rows.Add(gv.HeaderRow)
            End If
            '  add each of the data rows to the table
            For Each row As GridViewRow In gv.Rows
                GridViewExportUtil.PrepareControlForExport(row)
                table.Rows.Add(row)
            Next
            '  add the footer row to the table
            If (Not (gv.FooterRow) Is Nothing) Then
                GridViewExportUtil.PrepareControlForExport(gv.FooterRow)
                table.Rows.Add(gv.FooterRow)
            End If
            '  render the table into the htmlwriter
            table.RenderControl(htw)
            '  render the htmlwriter into the response
            HttpContext.Current.Response.Write(sw.ToString)
            HttpContext.Current.Response.End()
        End Sub

        ' Replace any of the contained controls with literals
        Private Shared Sub PrepareControlForExport(ByVal control As Control)
            Dim i As Integer = 0
            Do While (i < control.Controls.Count)
                Dim current As Control = control.Controls(i)
                If (TypeOf current Is LinkButton) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
                ElseIf (TypeOf current Is ImageButton) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
                ElseIf (TypeOf current Is HyperLink) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
                ElseIf (TypeOf current Is DropDownList) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
                ElseIf (TypeOf current Is CheckBox) Then
                    control.Controls.Remove(current)
                    control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
                    'TODO: Warning!!!, inline IF is not supported ?
                End If
                If current.HasControls Then
                    GridViewExportUtil.PrepareControlForExport(current)
                End If
                i = (i + 1)
            Loop
        End Sub
    End Class
    Protected Sub BtnExportGrid_Click(sender As Object, args As EventArgs)
     If Me.RadioButtonList1.SelectedIndex = 1 Then
      '  the user wants all rows exported, turn off paging
      '  and rebing the grid before sending it to the export
      '  utility
      Me.GridView1.AllowPaging = False
      Me.GridView1.DataBind()
     ElseIf Me.RadioButtonList1.SelectedIndex = 2 Then
      '  the user wants just the first 100,
      '  adjust the PageSize and rebind
      Me.GridView1.PageSize = 100
      Me.GridView1.DataBind()
     End If

     '  pass the grid that for exporting ...
     GridViewExportUtil.Export("YourFileName.xls", Me.GridView1)
    End Sub

    • Marked as answer by ziara Tuesday, August 10, 2010 6:16 AM
    Tuesday, August 10, 2010 6:15 AM
  • thanks for all your help and support I really appreciate your patience
    Tuesday, August 10, 2010 6:16 AM