2017-06-16 86 views
0

下面是我的代碼下載excel中的數據,但問題是,而下載它沒有顯示該文件越來越下載此外,我給路徑下面給出的方式下載文件在下載文件夾,但我不應該使用這個,因爲它在本地主機工作,但它不會工作時託管在server.how我可以下載到下載文件夾中顯示底部的下載文件我怎樣才能保存excel文件在下載文件夾使用asp.netnet#

protected void btnExportExcel_Click(object sender, EventArgs e) 
    { 
    string pathDownload = @"~\Downloads\" Data.xls"; 
    ExportToExcel(dsExcel, pathDownload); 
    lblMessage.Text = "Downloaded Successfully"; 
    } 
    private void ExportToExcel(DataSet table, string filePath) 
    { 

    int tablecount = table.Tables.Count; 
     StreamWriter sw = new StreamWriter(filePath, false); 
     sw.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); 
     sw.Write("<font style='font-size:10.0pt; font-family:Calibri;'>"); 
sw.Write("<BR><BR><BR>"); 
      sw.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:'#1E90FF'> <TR>"); 
sw.Write("</Table>"); 
      //sw.Write("<BR><BR><BR><BR>"); 
      //sw.Write("\n"); 
      //sw.Write(string.Format("Line1{0}Line2{0}", Environment.NewLine)); 


      sw.Write("</font>"); 

     } 
     sw.Close(); 
    } 
    this is the path that i am getting ~\Downloads\DATA.xls 

    and i am getting this exception Could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\~\Downloads\DATA.xls'. StreamWriter sw = new StreamWriter(filePath, false); 
+0

簡短的回答:不能。您無法控制客戶端的文件位置。而且你不是創建Excel文件,而是創建一個xls擴展名的html頁面。使用像EPPplus這樣的專業庫來創建Excel文件。 – VDWWD

+0

XLS文件是一種二進制格式,而不是文本文件(可能是僞裝成XLS的HTML格式?)。您需要使用第三方庫將數據插入到Excel文件中,然後將其提供給用戶(甚至不能決定將文件存儲在客戶端代碼中的位置)。 –

+0

[重命名的HTML文件現在將在Excel中打開](http://www.infoworld.com/article/3106774/microsoft-windows/good-news-for-microsoft-office-renamed-html-files-now-open-在-excel.html)。如果OP可以在本地打開它,它也可以作爲下載工作。 –

回答

0

當前您的Streamwriter寫入本地文件。你需要把它寫入瀏覽器。因此,而不是

StreamWriter sw = new StreamWriter(filePath, false); 

使用

StreamWriter sw = new StreamWriter(HttpContext.Current.Response.OutputStream); 

此外,一定要set the right MIME type,並設置content-disposition to trigger a download

+0

你是否還設置了MIME類型和內容處置標題? –

+0

您是否關注鏈接? –

0

私人無效ExportGridToExcel(){

 Maingrid.AllowPaging = false;// To print all the pages without pagination in grid 

     string filename = string.Empty; 
     filename = "Report -" + DateTime.Now.ToString("yyyyMMddHHmmssfffff"); 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.ClearHeaders(); 
     Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls"); 
     Response.Charset = ""; 
     Response.ContentType = "application/vnd.ms-excel"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw);   
     Maingrid.GridLines = GridLines.Both; 
     Maingrid.HeaderStyle.Font.Bold = true; 


     int x = Maingrid.Rows.Count; 
     for (int i = 0; i < Maingrid.Rows.Count; i++) 
     { 
      GridViewRow row = Maingrid.Rows[i]; 
      //Apply text style to each Row 
      row.Attributes.Add("class", "textmode"); 
      row.BackColor = System.Drawing.Color.White; 
     } 
     Maingrid.RenderControl(hw); 


     string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
     //style to format numbers to string 
     Response.Output.Write("<h1>Merchant Registration report</h1>"); 
     Response.Write(style); 
     Response.Output.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 
    } 

    public override void VerifyRenderingInServerForm(Control control) 
    { 

    } 
+0

嗨sivapriya謝謝你的迴應它不符合我的要求,請你看看我已發佈的第二個代碼 – abc

+0

我已經通過你的代碼。沒有必要提及下載路徑。使用我提供的代碼。這應該工作。 – Sivapriya

+0

我沒有主網格在我的代碼我有多個網格,我試圖下載單個Excel表中的所有多個網格我保持你的代碼,但它給我錯誤的主網格附近 – abc

相關問題