2014-03-13 52 views
1

調用此方法時,整個html設計頁面隨着數據移植到excel。 我只想導出數據,不應該使用html設計。我無法找到解決方案,請有人幫助解決。將DataTable導出到ASP.NET中的

public void ExportToExcel(DataTable dtExportData) 
     { 
      string strReportName = string.Empty; 
      //StringWriter oStringWriter = null; 
      //Html32TextWriter oHtmlTextWriter = null; 
      //TableOneDayPassReport.Visible = true; 
      ExcelGrid.EnableViewState = false; 
      ExcelGrid.DataSource = dtExportData; 
      ExcelGrid.DataBind(); 
      Response.Clear(); 
      Response.ClearHeaders(); 
      Response.Cache.SetCacheability(HttpCacheability.Private); 
      Response.AddHeader("content-disposition", "attachment;filename=OwnTransportDetails.xls"); 
      Response.Charset = ""; 
      Response.ContentType = "application/vnd.ms-excel"; 
      System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
      System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
      //TableOneDayPassReport.RenderControl(htmlWrite); 
      Response.Write(stringWrite.ToString()); 
      //Response.End(); 


      // strReportName = dtExportData.TableName; 
      // Report_Month.InnerHtml = "<b>" + strReportName + " Report</b><br />"; 
      // ReportHead.Attributes.Add("style", "DISPLAY: block; VISIBILITY: visible"); 
      // TableOneDayPassReport.Visible = true; 
      // //oStringWriter = new StringWriter(); 
      // //oHtmlTextWriter = new Html32TextWriter(oStringWriter); 
      // ExcelGrid.EnableViewState = false; 
      // ExcelGrid.DataSource = dtExportData; 
      // ExcelGrid.DataBind(); 
      // Response.Clear(); 
      // Response.Buffer = true; 
      // Response.AppendHeader("Content-Disposition", "attachment; filename=" + strReportName + ".xls"); 
      // Response.ContentType = "application/vnd.ms-excel"; 
      // TableOneDayPassReport.RenderControl(oHtmlTextWriter); 
      // Response.Write(oStringWriter.ToString()); 
      // HttpContext.Current.ApplicationInstance.CompleteRequest(); 
      //// Response.End(); 
     } 
+0

檢查此代碼@Suresh Savage –

回答

0

您告訴瀏覽器將數據作爲Microsoft Excel處理,但返回HTML數據。 Microsoft Excel足夠聰明,可以顯示HTML數據並顯示數據。

如果您只想導出數據,您應該使用庫(如EPPlus或Microsoft自己的SDK),並在服務器上創建一個Excel工作表。然後,返回此工作表。

0

檢查這個代碼通過這個代碼,你可以下載.csv文件以Excel格式

在這裏你需要創建文件夾下載,並從該文件夾,你需要給服務器的路徑。 href="../../download/Orders.csv"

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
using System.IO; 
using System.Text; 
public partial class Admin_export_order : System.Web.UI.Page 
{ 
    DBAccess dbAcc = new DBAccess(); 
    DataTable dt = new DataTable(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["admin_id"] == null) 
      Response.Redirect("~/jbs/admin/login"); 
     if (!IsPostBack) 
     { 

      view_edit_supplier_BAL objHelp = new view_edit_supplier_BAL(); 
      DataTable dtHelp = objHelp.SearchSupplier("where page = 'export_order'", 18); 
      if (dtHelp != null && dtHelp.Rows.Count > 0) 
       lblhelp_msg.Text = dtHelp.Rows[0][0].ToString().Replace("''", "'"); 
     } 
     StreamWriter sw; 
     DataSet ds = new DataSet(); 
     export_bal objGet = new export_bal(); 
     ds = objGet.getDetails(5); 

     try 
     { 


      for (int i = 0; i < ds.Tables.Count; i++) 
      { 
       sw = new StreamWriter(HttpContext.Current.Server.MapPath("~/download/Orders.csv"), false, Encoding.Unicode); 

       int iColCount = ds.Tables[i].Columns.Count; 
       for (int h = 0; h < iColCount; h++) 
       { 
        sw.Write(ds.Tables[i].Columns[h]); 
        if (h < iColCount - 1) 
        { 
         sw.Write("\t"); 
        } 
       } 
       sw.Write(sw.NewLine); 

       foreach (DataRow dr in ds.Tables[i].Rows) 
       { 
        for (int h = 0; h < iColCount; h++) 
        { 
         if (!Convert.IsDBNull(dr[h])) 
         { 
          if (dr[h].ToString().Contains("\r\n")) 
          { 
           string newdata = dr[h].ToString(); 
           string newcolumn = newdata.Replace("\r\n", ""); 
           sw.Write(newcolumn); 
          } 
          else 
          { 

           sw.Write(dr[h].ToString()); 

          } 
         } 
         if (h < iColCount - 1) 
         { 
          sw.Write("\t"); 
         } 
        } 
        sw.Write(sw.NewLine); 

       } 
       sw.Close(); 
      } 


     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
     finally 
     { 

     } 
    } 


} 





<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
</head> 
<table style="height: auto; width: 99.3%;" cellspacing="0" cellpadding="0" border="0" 
     align="left" bgcolor="white"> 
     <tr> 
      <td valign="top" bgcolor="#ffffff" align="left" width="8"> 
       <img height="8" width="8" alt="left bottom" src="../admin/images/top-left-corner.gif" /> 
      </td> 
      <td bgcolor="#ffffff" width="99%"> 
      </td> 
      <td bgcolor="#ffffff" align="right" width="8"> 
       <img height="8" width="8" align="right" alt="bottom right" src="../admin/images/right-top-corner.gif" /> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="3" valign="top"> 
       <table bgcolor="#ffffff" width="100%"> 
        <tr> 
         <td width="36px" style="padding-left: 18px;"> 
          <img src="images/category-img.gif" width="36" height="36" /> 
         </td> 
         <td class="font_text_bold"> 
          Export Orders 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="3" valign="top"> 
       <table bgcolor="#ffffff" width="100%" style="height: 660px;"> 
        <tr> 
         <td valign="top"> 
          <table width="100%"> 
           <tr> 
            <td colspan="3" valign="top"> 
             <table bgcolor="#ffffff" width="100%" style="padding-left: 7px;"> 
              <tr> 
               <td> 
                <table width="99%" cellspacing="0" cellpadding="0" border="0" align="left" style="height: auto;"> 
                 <tr class="box_hd_bg_for_every"> 
                  <td align="left" valign="top" width="7"> 
                   <img src="images/left-add-pro.gif" alt="left" height="29" /> 
                  </td> 
                  <td class="font_text_bold" width="100%" align="center" style="padding-right: 200px; 
                   width: 1500px"> 
                   Export Setup 
                  </td> 
                  <td align="right"> 
                   <img src="images/right-add-pro.gif" alt="right" height="29" /> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td class="Gray_blowtr" colspan="3"> 
                   <table width="100%" height="100%" border="0" cellpadding="1" cellspacing="0"> 
                    <tr> 
                     <td colspan="2" height="10"> 
                     </td> 
                    </tr> 
                    <tr> 
                     <td colspan="2" align="left" class="font_text" style="padding-left: 5px;"> 
                      Export file will be readable in Microsoft Excel or equivalent applications for ease 
                      review of data. Please click on below download button to Export file for Orders. 
                     </td> 
                    </tr> 
                    <tr> 
                     <td style="height: 20px;"> 
                     </td> 
                    </tr> 
                    <tr> 
                     <td colspan="2" align="center" style="height: 30px;"> 
                      <a href="../../download/Orders.csv" class="setpostion">&nbsp;<img style="border: 0" 
                       src="images/download.gif" alt="Download" title="Export" /> 
                       &nbsp;</a> 
                     </td> 
                    </tr> 
                    <tr> 
                     <td style="height: 20px;"> 
                     </td> 
                    </tr> 
                   </table> 
                  </td> 
                 </tr> 
                </table> 
               </td> 
              </tr> 
             </table> 
            </td> 
           </tr> 
           <tr> 
            <td colspan="3" valign="top"> 
             <table bgcolor="#ffffff" width="100%" style="padding-left: 7px;"> 
              <tr> 
               <td> 
                <table width="99%" cellspacing="0" cellpadding="0" border="0" align="left" style="height: auto;"> 
                 <tr class="box_hd_bg_for_every"> 
                  <td align="left" valign="top" width="7"> 
                   <img src="images/left-add-pro.gif" alt="left" height="29" /> 
                  </td> 
                  <td class="font_text_bold" width="100%" style="padding-right: 200px"> 
                   How to use this page 
                  </td> 
                  <td align="right"> 
                   <img src="images/right-add-pro.gif" alt="right" height="29" /> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td class="Gray_blowtr" colspan="3" style="padding-left: 10px; padding-right: 10px; 
                   padding-top: 7px; padding-bottom: 7px;"> 
                   <asp:Label ID="lblhelp_msg" runat="server" Text=""></asp:Label> 
                  </td> 
                 </tr> 
                 <tr style="height: 20px;"> 
                  <td> 
                  </td> 
                 </tr> 
                </table> 
               </td> 
              </tr> 
             </table> 
            </td> 
           </tr> 
          </table> 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
     <tr> 
      <td valign="bottom" bgcolor="#ffffff" align="left" width="8"> 
       <img height="8" width="8" alt="left bottom" src="images/bottom-left-corner.gif" /> 
      </td> 
      <td bgcolor="#ffffff" width="99%"> 
      </td> 
      <td bgcolor="#ffffff" align="right" width="8"> 
       <img height="8" width="8" align="right" alt="bottom right" src="images/bottom-right-corner.gif" /> 
      </td> 
     </tr> 
    </table> 
</html> 
0

我找到了解決方案,請移動Response.End();行到onclick事件並調用ExportToExcel(DataTable dtExportData)方法。

And Response.End();不應該在try ... catch語句中,它應該在外面嘗試... catch語句

+0

爲什麼不應該在try ... catch語句中? – jterm