#region 构造URL POST请求
2 public static int timeout = 5000;//时间点
3 /// <summary>
4 /// 获取请求的反馈信息
5 /// </summary>
6 /// <param name="url"></param>
7 /// <param name="bData">参数字节数组</param>
8 /// <returns></returns>
9 private static String doPostRequest(string url, byte[] bData)
10 {
11 HttpWebRequest hwRequest;
12 HttpWebResponse hwResponse;
13
14 string strResult = string.Empty;
15 try
16 {
17 ServicePointManager.Expect100Continue = false;//远程服务器返回错误: (417) Expectation failed 异常源自HTTP1.1协议的一个规范: 100(Continue)
18 hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
19 hwRequest.Timeout = timeout;
20 hwRequest.Method = "POST";
21 hwRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
22 hwRequest.ContentLength = bData.Length;
23 Stream smWrite = hwRequest.GetRequestStream();
24 smWrite.Write(bData, 0, bData.Length);
25 smWrite.Close();
26 }
27 catch
28 {
29 return strResult;
30 }
31
32 //get response
33 try
34 {
35 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
36 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
37 strResult = srReader.ReadToEnd();
38 srReader.Close();
39 hwResponse.Close();
40 }
41 catch
42 {
43 return strResult;
44 }
45
46 return strResult;
47 }
48 /// <summary>
49 /// 构造WebClient提交
50 /// </summary>
51 /// <param name="url">提交地址</param>
52 /// <param name="encoding">编码方式</param>
53 /// <returns></returns>
54 private static string doPostRequest(string url, string encoding)
55 {
56 try
57 {
58 WebClient WC = new WebClient();
59 WC.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
60 int p = url.IndexOf("?");
61 string sData = url.Substring(p + 1);
62 url = url.Substring(0, p);
63 byte[] Data = Encoding.GetEncoding(encoding).GetBytes(sData);
64 byte[] Res = WC.UploadData(url, "POST", Data);
65 string result = Encoding.GetEncoding(encoding).GetString(Res);
66 return result;
67 }
68 catch
69 {
70 return "";
71 }
72 }
73 #endregion
1 #region 数据导出为EXCEL
2 public static void CreateExcel(DataTable dt, string fileName)
3 {
4 StringBuilder strb = new StringBuilder();
5 strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
6 strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
7 strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
8 strb.Append(" <head> <meta http-equiv=‘Content-Type‘ content=‘text/html; charset=utf-8‘>");
9 strb.Append(" <style>");
10 strb.Append(".xl26");
11 strb.Append(" {mso-style-parent:style0;");
12 strb.Append(" font-family:\"Times New Roman\", serif;");
13 strb.Append(" mso-font-charset:0;");
14 strb.Append(" mso-number-format:\"@\";}");
15 strb.Append(" </style>");
16 strb.Append(" <xml>");
17 strb.Append(" <x:ExcelWorkbook>");
18 strb.Append(" <x:ExcelWorksheets>");
19 strb.Append(" <x:ExcelWorksheet>");
20 strb.Append(" <x:Name>" + fileName + "</x:Name>");
21 strb.Append(" <x:WorksheetOptions>");
22 strb.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
23 strb.Append(" <x:Selected/>");
24 strb.Append(" <x:Panes>");
25 strb.Append(" <x:Pane>");
26 strb.Append(" <x:Number>3</x:Number>");
27 strb.Append(" <x:ActiveCol>1</x:ActiveCol>");
28 strb.Append(" </x:Pane>");
29 strb.Append(" </x:Panes>");
30 strb.Append(" <x:ProtectContents>False</x:ProtectContents>");
31 strb.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
32 strb.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
33 strb.Append(" </x:WorksheetOptions>");
34 strb.Append(" </x:ExcelWorksheet>");
35 strb.Append(" <x:WindowHeight>6750</x:WindowHeight>");
36 strb.Append(" <x:WindowWidth>10620</x:WindowWidth>");
37 strb.Append(" <x:WindowTopX>480</x:WindowTopX>");
38 strb.Append(" <x:WindowTopY>75</x:WindowTopY>");
39 strb.Append(" <x:ProtectStructure>False</x:ProtectStructure>");
40 strb.Append(" <x:ProtectWindows>False</x:ProtectWindows>");
41 strb.Append(" </x:ExcelWorkbook>");
42 strb.Append(" </xml>");
43 strb.Append("");
44 strb.Append(" </head> <body> <table align=\"center\" style=‘border-collapse:collapse;table-layout:fixed‘>");
45 if (dt.Rows.Count > 0)
46 {
47 strb.Append("<tr>");
48 //写列标题
49 int columncount = dt.Columns.Count;
50 for (int columi = 0; columi < columncount; columi++)
51 {
52 strb.Append(" <td style=‘text-align:center;‘><b>" + ColumnName(dt.Columns[columi].ToString()) + "</b></td>");
53 }
54 strb.Append(" </tr>");
55 //写数据
56 for (int i = 0; i < dt.Rows.Count; i++)
57 {
58 strb.Append(" <tr>");
59
60 for (int j = 0; j < dt.Columns.Count; j++)
61 {
62 strb.Append(" <td class=‘xl26‘>" + dt.Rows[i][j].ToString() + "</td>");
63 }
64 strb.Append(" </tr>");
65 }
66 }
67 strb.Append("</table> </body> </html>");
68 HttpContext.Current.Response.Clear();
69 HttpContext.Current.Response.Buffer = true;
70 HttpContext.Current.Response.Charset = "utf-8";
71 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
72 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//
73 HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
74 //HttpContext.Current.p.EnableViewState = false;
75 HttpContext.Current.Response.Write(strb);
76 HttpContext.Current.Response.End();
77 }
78 #endregion