- using System;
- using System.Data;
- using System.Web;
- using System.Net;
- using System.Text;
- using System.IO;
- using System.Collections.Generic;
-
- namespace IWOMTracker.Common
- {
- public class PageText
- {
- /// <summary>
- /// 下载页面
- /// </summary>
- /// <param name="strUrl"></param>
- /// <returns></returns>
- public static string GetPageData(string strUrl)
- {
- string text = "";
- if (strUrl == "")
- {
- return "";
- }
- WebResponse response = null;
- Stream responseStream = null;
- StreamReader reader = null;
- try
- {
- response = WebRequest.Create(strUrl).GetResponse(); //创建一个合适的对象/ 2获得村服务器上得到的数据
- responseStream = response.GetResponseStream(); //得到数据流
- reader = new StreamReader(responseStream, Encoding.UTF8);
- string txt = reader.ReadToEnd();
- string encod = txt.Substring(txt.IndexOf("<meta"));
- encod = encod.Substring(encod.IndexOf("charset=") + 8, 20);
- response = WebRequest.Create(strUrl).GetResponse();
- responseStream = response.GetResponseStream();
- reader = new StreamReader(responseStream, Encoding.UTF8);
- if (encod.ToLower().IndexOf("utf-8") >= 0)
- {
- reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
- }
- else if (encod.ToLower().IndexOf("gb2312") >= 0)
- {
- reader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
- }
- else if (encod.ToLower().IndexOf("utf8") >= 0)
- {
- reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
- }
- else if (encod.ToLower().IndexOf("gbk") >= 0)
- {
- reader = new StreamReader(responseStream, Encoding.GetEncoding("gbk"));
- }
- else
- {
- reader = new StreamReader(responseStream, Encoding.Default);
- }
- try
- {
- return reader.ReadToEnd();
- }
- catch
- {
- return "";
- }
- }
- catch (Exception exn)
- {
- if (exn.Message.ToString().IndexOf("远程服务器返回错误: (503) 服务器不可用") > -1)
- text = "beifeng";
- else
- text = "";
- }
- finally
- {
- if (reader != null)
- {
- reader.Close();
- }
- if (responseStream != null)
- {
- responseStream.Close();
- }
- if (response != null)
- {
- response.Close();
- }
- }
- return text;
- }
-
- public static string GetWebContent(string Url)
- {
- string strResult = "";
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- //声明一个HttpWebRequest请求
- request.Method = "GET";
- request.SendChunked = false;
-
- request.Timeout = 10000;
- //设置连接超时时间
- request.Headers.Set("Pragma", "no-cache");
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream streamReceive = response.GetResponseStream();
- Encoding encoding = Encoding.GetEncoding("GB2312");
- StreamReader streamReader = new StreamReader(streamReceive, encoding);
- strResult = streamReader.ReadToEnd();
- response.Close();
- streamReader.Close();
- }
- catch (Exception ex)
- {
- throw new Exception("抓取数据出错" + ex.Message);
- }
- return strResult;
- }
-
- /// <summary>
- /// 导出Excel(不带表头)
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="FileName"></param>
- /// <param name="response"></param>
- public static void CreateExcel(DataTable dt, string FileName, HttpResponse response)
- {
- HttpResponse resp;
- resp = response;
- FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(FileName));
- resp.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
- resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
- string ls_item = "";
-
- //定义表对象与行对象,同时用DataSet对其值进行初始化
-
- DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
- int i = 0;
- int cl = dt.Columns.Count;
-
- //向HTTP输出流中写入取得的数据信息
-
- //逐行处理数据
- foreach (DataRow row in myRow)
- {
- //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
- for (i = 0; i < cl; i++)
- {
- if (i == (cl - 1))//最后一列,加n
- {
- ls_item += row[i].ToString().Replace("\n", " ").Replace("\"", "\"\"") + "\n";
- }
- else
- {
- ls_item += row[i].ToString().Replace("\n", " ").Replace("\"", "\"\"") + "\t";
- }
-
- }
- resp.Write(ls_item.Replace("\r", " "));
- ls_item = "";
-
- }
- resp.End();
- }
-
- /// <summary>
- /// 导出Execl(带表头)
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="FileName"></param>
- /// <param name="response"></param>
- public static void CreateExcel2(DataTable dt, string FileName, HttpResponse response)
- {
- HttpResponse resp;
- resp = response;
- FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(FileName));
- resp.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
- resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
- string colHeaders = "", ls_item = "";
-
- //定义表对象与行对象,同时用DataSet对其值进行初始化
-
- DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
- int i = 0;
- int cl = dt.Columns.Count;
-
- //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
- for (i = 0; i < cl; i++)
- {
- if (i == (cl - 1))//最后一列,加n
- {
- colHeaders += dt.Columns[i].Caption.ToString().Replace("\n", " ").Replace("\"", "\"\"") + "\n";
- }
- else
- {
- colHeaders += dt.Columns[i].Caption.ToString().Replace("\n", " ").Replace("\"", "\"\"") + "\t";
- }
-
- }
- resp.Write(colHeaders.Replace("\r", " "));
- //向HTTP输出流中写入取得的数据信息
-
- //逐行处理数据
- foreach (DataRow row in myRow)
- {
- //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
- for (i = 0; i < cl; i++)
- {
- if (i == (cl - 1))//最后一列,加n
- {
- ls_item += row[i].ToString().Replace("\n", " ").Replace("\"", "\"\"") + "\n";
- }
- else
- {
- ls_item += row[i].ToString().Replace("\n", " ").Replace("\"", "\"\"") + "\t";
- }
-
- }
- resp.Write(ls_item.Replace("\r", " "));
- ls_item = "";
-
- }
- resp.End();
- }
-
- /// <summary>
- /// 导出CSV
- /// </summary>
- /// <param name="tb">DataTable</param>
- /// <param name="response">Page.Response</param>
- /// <param name="fileName">导出文件名</param>
- /// <param name="isHeader">是否导出tb表头</param>
- public static void ExportCSV(DataTable tb, HttpResponse response, string fileName, bool isHeader)
- {
- string data = "";
-
- //写出列名
- if (isHeader)
- {
- foreach (DataColumn column in tb.Columns)
- {
- data += column.ColumnName.Replace(",", ",") + ",";
- }
- data += "\n";
- }
-
- //写出数据
- foreach (DataRow row in tb.Rows)
- {
- foreach (DataColumn column in tb.Columns)
- {
- data += row[column].ToString().Replace(",", ",") + ",";
- }
- data += "\n";
- }
- data += "\n";
-
- string temp = string.Format("attachment;filename={0}", fileName + ".csv");
- response.ClearHeaders();
- response.AppendHeader("Content-disposition", temp);
- response.Charset = "gb2312";
- response.ContentType = "application/ms-excel";
- response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
- response.Write(data);
- response.End();
- }
-
- /// <summary>
- /// 实现对IList到DataSet的转换
- /// </summary>
- /// <param name="list">待转换的IList</param>
- /// <returns>转换后的DataSet</returns>
- public static DataSet IListToDataSet<T>(IList<T> list, Type type)
- {
- if (list == null || list.Count <= 0)
- {
- return null;
- }
-
- DataSet ds = new DataSet();
- DataTable dt = new DataTable(type.Name);
- DataColumn column;
- DataRow row;
-
- System.Reflection.PropertyInfo[] myPropertyInfo = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
-
- foreach (T t in list)
- {
- if (t == null)
- {
- continue;
- }
-
- row = dt.NewRow();
-
- for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
- {
- System.Reflection.PropertyInfo pi = myPropertyInfo[i];
-
- string name = pi.Name;
-
- if (dt.Columns[name] == null)
- {
- column = new DataColumn(name, pi.PropertyType);
- dt.Columns.Add(column);
- }
-
- row[name] = ((object[])(object)t)[i] == null ? DBNull.Value : ((object[])(object)t)[i];
- }
-
- dt.Rows.Add(row);
- }
-
- ds.Tables.Add(dt);
-
- return ds;
- }
- }
- }
|