C#从将数据导出到excel中

httpwebrequest学习

Httpwebrequest对象的创建:不是利用new关键字通过构 造函数来创建的,而是利用工厂机制(factory mechanism)通过Create()方法来创建的。

C# HttpWebRequest提交数据方式1. GET方式

描述:GET 方式通过在网络地址附加参数来完成数据的提交,比如在地址 http://www.google.com/webhp?hl=zh-CN 中,前面部分 http://www.google.com/webhp 表示数据提交的网址,后面部分 hl=zh-CN 表示附加的参数,其中 hl 表示一个键(key), zh-CN 表示这个键对应的值(value)。程序代码如下:

HttpWebRequest req=(HttpWebRequest)HttpWebRequest.Create(“http://www.google.com/webhp?hl=zh-CN”);

Request.Method=”GET”;

Using (WebResponse wr=req.GetResponse())

{

//对接收到的页面内容进行处理

}

C# HttpWebRequest提交数据方式2. POST 方式。

扫描二维码关注公众号,回复: 10898137 查看本文章

POST 方式通过在页面内容中填写参数的方法来完成数据的提交,参数的格式和 GET 方式一样,是类似于 hl=zh-CN&newwindow=1 这样的结构。程序代码如下:

 

string param = "hl=zh-CN&newwindow=1";        //参数

byte[] bs = Encoding.ASCII.GetBytes(param);    //参数转化为ascii码

HttpWebRequese req =   (HttpWebRequest) =HttpWebRequest.Create(   "http://www.google.com/intl/zh-CN/" );  //创建request

req.Method = "POST";    //确定传值的方式,此处为post方式传值

req.ContentType = "application/x-www-form-urlencoded"; 

Req.Timeout = 720000;

req.ContentLength = bs.Length; 

using (Stream reqStream = req.GetRequestStream()) //获取用于写入请求数据的Stream 实例。

   reqStream.Write(bs, 0, bs.Length); 

using (WebResponse wr = req.GetResponse()) 

   //在这里对接收到的页面内容进行处理 

 

 

图像就不放了

总结:教育平台excel文件的导出,是在生成excel文件后,先将其上传到文件服务器,实现文件服务器同步然后再通过给定的url去下载文件

 

 

//可以将本地文件输出到浏览器下载

public void OutputClient(byte[] bytes)
        {
            HttpContext.Current.Response.Buffer = true;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm")));

            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");

            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.Close();
        }

 

参考:https://blog.csdn.net/huoliya12/article/details/90645036C# 导出 Excel 的各种方法总结

发布了30 篇原创文章 · 获赞 1 · 访问量 1158

猜你喜欢

转载自blog.csdn.net/chunchunlaila/article/details/104450847