2016-06-21 8 views
0

GridViewをExcelファイルにエクスポートするには、次のコードを使用します。理論的には、このコードは面倒なくエクスポートする必要があります。ただし、ブランクをエクスポートしています。excelにエクスポートexcelスプレッドシートを返す

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); 
      } 
     } 
    } 
} 

ここに何か不足していますか?

+0

私が気付く2つのこと '1あなたのResponse.ContentType =" application/vnd.ms-excel ";' '2ndをCurrent.Response.Endの前に置き、' HttpContext.Current.Response.Flush();を置き換えます。 'あなたがヘッダを追加する前にそれが – MethodMan

+0

に次のものを追加するのに役立つかどうかを確認してください。 ' HttpContext.Current.Response.ClearContent();そして、 'HttpContext.Current.Response.ContentType =" application/ms-excel ";を追加し、ヘッダーを追加する行を追加します。 – MethodMan

+0

私はそれを行ってみましょう。 –

答えて

0

コードの大部分は正しいものでした。ここでの問題は、HTMLライターが正しく表示されないことです。 John Wuは彼の答えでこれを言及した。ここでのウォークアウトは単純です:

の代わりに;

//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); 

私は使用しました。

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

も重要なのは、これは私のためにシームレスに働いたWeb.configファイル

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

にこのコードを追加することです。

0

メモ帳にExcelファイルを開いたことがありますか?そこにはHTMLがありません!しかし、HtmlTextWriterを使ってデータをブラウザにストリームしています。私はそれがうまくいくとは思わない。

実際にデータをExcelのネイティブ形式に変換することを決定した場合は、Excel interopを使用するか、より効率的なサードパーティの変換ツールを使用できます。

スプレッドシートをExcelで開く必要があるだけで、データがブラウザにどのように転送されても気にしない場合は、this articleのような手法を使用してCSV形式でデータをストリーミングすることをおすすめします。

関連する問題