2016-06-21 7 views
0

Ich habe den folgenden Code, um eine GridView in eine Excel-Datei zu exportieren. Theoretisch sollte dieser Code problemlos exportiert werden. Es exportiert jedoch das Leerzeichen.Exportieren nach Excel zurückzusenden leer Excel-Tabelle

public static void Export(string fileName, GridView gv, System.Web.UI.Control parentControl) 
    { 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename=" + fileName + ".xls")); 
     HttpContext.Current.Response.ContentType = "application/ms-excel"; 

     using (StringWriter sw = new StringWriter()) 
     { 
      using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
      { 
       // Create a table to contain the grid 
       Table table = new Table(); 

       // include the gridline settings 
       table.GridLines = gv.GridLines; 

       // add the header row to the table 
       if (gv.HeaderRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.HeaderRow); 
        table.Rows.Add(gv.HeaderRow); 
       } 

       // add each of the data rows to the table 
       foreach (GridViewRow row in gv.Rows) 
       { 
        GridViewExportUtil.PrepareControlForExport(row); 
        table.Rows.Add(row); 
       } 

       // add the footer row to the table 
       if (gv.FooterRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.FooterRow); 
        table.Rows.Add(gv.FooterRow); 
       } 

       //Control 'Content_ctlDisplayIssues_gvIssues' of type 'GridView' must be placed inside a form tag with runat=server. 
       System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); 
       parentControl.Controls.Add(form); 
       form.Controls.Add(gv); 
       form.RenderControl(htw); 

       // render the htmlwriter into the response 
       HttpContext.Current.Response.Write(sw.ToString()); 
       HttpContext.Current.Response.End(); 
      } 
     } 
    } 

    /// <summary> 
    /// Replace any of the contained controls with literals 
    /// </summary> 
    /// <param name="control"></param> 
    private static void PrepareControlForExport(Control control) 
    { 
     for (int i = 0; i < control.Controls.Count; i++) 
     { 
      Control current = control.Controls[i]; 
      if (current is LinkButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
      } 
      else if (current is ImageButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
      } 
      else if (current is HyperLink) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
      } 
      else if (current is DropDownList) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
      } 
      else if (current is CheckBox) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
      } 
      else if (current is Image) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as Image).AlternateText)); 
      } 

      if (current.HasControls()) 
      { 
       GridViewExportUtil.PrepareControlForExport(current); 
      } 
     } 
    } 
} 

Fehle ich etwas hier?

+0

2 Dinge, die ich bemerkt '1 Änderung Ihrer Response.ContentType = "application/vnd.ms-excel";' ' 2nd' vor Current.Response.End, legte' HttpContext.Current.Response.Flush(); 'sehen, ob das hilft – MethodMan

+0

auch die folgenden hinzufügen, bevor Sie die Header ' HttpContext.Current.Response.ClearContent(); und HttpContext.Current.Response.ClearHeaders(); 'dann fügen Sie diese Zeile ' HttpContext.Current.Response.ContentType = "application/ms-excel"; 'dann fügen Sie die Zeile zum Hinzufügen der Header hinzu. – MethodMan

+0

Lassen Sie mich das geben. –

Antwort

0

Der Großteil des Codes war korrekt. Das Problem hier ist, dass der HTML-Writer nicht richtig gerendert wird. John Wu erwähnt dies in seiner Antwort. So ist der Spaziergang hier einfach:

statt;

//Control 'Content_ctlDisplayIssues_gvIssues' of type 'GridView' must be placed inside a form tag with runat=server. 
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); 
parentControl.Controls.Add(form); 
form.Controls.Add(gv); 
form.RenderControl(htw); 

ich verwendet;

//here the table is rendered into the html writer 
table.RenderControl(htw); 

auch wichtig, ist diesen Code in der Datei Web.config für mich

<add key="PageInspector:ServerCodeMappingSupport" value="Disabled" /> 

Das funktionierte nahtlos hinzuzufügen.

0

Haben Sie schon einmal eine Excel-Datei in NotePad geöffnet? Es ist nicht HTML drin! Sie verwenden jedoch einen HtmlTextWriter, um die Daten zum Browser zu streamen. Ich denke nicht, dass das funktionieren wird.

Wenn Sie wirklich entschlossen sind, Ihre Daten in natives Excel-Format zu konvertieren, können Sie entweder Excel interop verwenden, oder Sie können nach einem optimierten Drittanbieter-Konvertierungstool suchen.

Wenn Sie den Benutzer einfach brauchen, um eine Tabelle in Excel zu öffnen, aber es ist Ihnen egal, wie die Daten zum Browser transportiert werden, empfehle ich Ihnen, die Daten im CSV-Format zu streamen, mit einer Technik wie die in this article .