下面是我的代码下载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);
简短的回答:不能。您无法控制客户端的文件位置。而且你不是创建Excel文件,而是创建一个xls扩展名的html页面。使用像EPPplus这样的专业库来创建Excel文件。 – VDWWD
XLS文件是一种二进制格式,而不是文本文件(可能是伪装成XLS的HTML格式?)。您需要使用第三方库将数据插入到Excel文件中,然后将其提供给用户(甚至不能决定将文件存储在客户端代码中的位置)。 –
[重命名的HTML文件现在将在Excel中打开](http://www.infoworld.com/article/3106774/microsoft-windows/good-news-for-microsoft-office-renamed-html-files-now-open-在-excel.html)。如果OP可以在本地打开它,它也可以作为下载工作。 –