c#如何通过实现下载链接所生成的excel文件

爬爬爬往死爬 2017-12-01 10:26:03
如题.
下载:http://pub.alimama.com/report/getTbkPaymentDetails.json?spm=0.0.0.0.Oq98lr&queryType=1&payStatus=3&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime=2014-12-05&endTime=2014-12-11

比如这个是阿里妈妈的数据报表。是需要执行url后才会获得下载文件。
直接运行ie是可以获得下载文件。并弹出保存对话框。
那么如何通过c#实现下载这样链接所生成的文件

百度的方法试了,但是下载下来的是页面信息,并不是生成的xls文件

代码如下:
/// <summary>
/// 下载需要得xls
/// </summary>

private string DownloadFile(string starttime, string endtime, int p)
{
string fileName = "";
try
{
object[] args = new object[] { starttime, endtime, p, Http.Token };
string url = string.Format("http://pub.alimama.com/report/getTbkPaymentDetails.json?queryType=1&payStatus=&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime={0}&endTime={1}&toPage={2}&perPageSize=100&_tb_token_={3}&_input_charset=utf-8", args);
fileName = CreateFileName(url);
if (!Directory.Exists(Application.StartupPath + "/"))
{
Directory.CreateDirectory(Application.StartupPath + "/");
}
WebClient client = new WebClient();
client.DownloadFile(url, Application.StartupPath + "/" + fileName);
}
catch (Exception e)
{
Console.WriteLine(e);
return "";
}
if (!String.IsNullOrEmpty(fileName))
{
Console.WriteLine("文件下载成功,文件名称:" + fileName);
}
else
{
Console.WriteLine("文件下载失败");
}
Console.ReadLine();
return fileName;
}

/// <summary>
/// 创建文件名称
/// </summary>
public static string CreateFileName(string url)
{
string fileName = "";
string startTime = url.Substring(url.IndexOf("startTime=") + 10, 10);
string endTime = url.Substring(url.IndexOf("endTime=") + 8, 10);
fileName = "TaokeDetail" + startTime + "-" + endTime + ".xls";
Console.WriteLine(fileName);
return fileName;
}
...全文
704 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZY_zhu 2018-06-29
  • 打赏
  • 举报
回复
防盗链关键代码:
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(httpUrl);
webrequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webrequest.Headers.Add("Accept-Language", "zh-cn");
webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MS-RTC LM 8; Alexa Toolbar)";
webrequest.Headers.Add("UA-CPU", "x86");
webrequest.KeepAlive = true;
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
事理 2017-12-01
  • 打赏
  • 举报
回复
WebClient client=new WebClient(); client.DownLoad(url,"本地路径");
wushang923 2017-12-01
  • 打赏
  • 举报
回复
WebClient client = new WebClient(); client.DownloadFile("http://xxxxxxxxxx", "D:\\123.xlsx"); var req = (HttpWebRequest)WebRequest.Create("http://xxxxxxxxxx"); req.Method = "GET"; req.ContentType = "application/vnd.ms-excel";//EXCEL req.Timeout = 30000; var rsp = (HttpWebResponse)req.GetResponse(); var requestStream = rsp.GetResponseStream(); if (requestStream != null) { using (var fileStream = new FileStream("D:\\123.xlsx", FileMode.Create, FileAccess.Write)) { requestStream.CopyTo(fileStream); } }
爬爬爬往死爬 2017-12-01
  • 打赏
  • 举报
回复
引用 1 楼 slyzly 的回复:
WebClient client=new WebClient(); client.DownLoad(url,"本地路径");
WebClient client=new WebClient(); client.DownLoad(url,"本地路径"); 哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
爬爬爬往死爬 2017-12-01
  • 打赏
  • 举报
回复
WebClient client=new WebClient(); client.DownLoad(url,"本地路径"); 哥们,没有DownLoad这个方法呀,DownLoadFile这个方法下载出来的xls文件内容是错误的。
Free Spire.XLS for .NET 是e-iceblue公司开发的一款支持对所有Excel格式类型文件进行操作的.NET 控件。它适用于任何类型的应用程序比如ASP.NET Web应用程序或者Windows桌面应用程序。Spire.XLS for .NET允许开发人员创建和管理Excel,而不需要安装Microsoft Excel或者Microsoft Office。相比Microsoft,Spire.XLS for .NET对于开发人员来说是一个更好的选择 - 自动化,安全,稳定,可扩展性延伸,速度快,价格便宜和功能强大。 通过使用Free Spire.XLS for .NET,开发人员可以快速方便的查看,生成,读写以及自定义编辑Excel文件。Spire.XLS for .NET支持Excel 97-2003, Excel 2007, Excel 2010 and Excel 2013。它还支持C#, VB.NET, ASP.NET和 ASP.NET MVC编程语言 作为一款专业,强大,方便易用的控件,Spire.XLS for .NET有一下主要功能: 新建Excel文件,编辑已有的Excel文件,多种保存Excel方式,设置Excel文件属性,文件打印设置,合并,拆分或者拷贝单元格,支持对Excel文件加锁或者加密保护。 Spire.XLS for .NET支持把数据从数据库导出到Excel工作表,或者把数据从Excel数据导入到数据库。 Spire.XLS for .NET 提供强大的功能为开发者设置单元格格式。这次格式化数字,文字缩进,定位调整,设置单元格边线,单元格填充以及保护等。 开发人员可以轻松的利用Spire.XLS for .NET向Excel文件里面写入内容,包括超过530多种的公式,70多种表格,支持插入图片,不同类型的文字,链接,注释甚至VBA等。 Spire.XLS for .NET 还允许开发人员将Excel文件转换成PDF, HTML, CSV, Text, XML以及图片格式。 注:下载包含有Spire.xls.dll文件以及部分Demo, 你可以调用DLL并参照Demo提供的代码处理Excel文档。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧