2017-06-16 33 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

相关问题