# region [导出数据功能]把数据接收到dataset中,转换为csv文件
public void FileStreamCSV(string csvString)
{
FileStream file = new FileStream("D:/CSVFile", FileMode.Open);
StreamReader sr = new StreamReader(file);
string line = sr.ReadLine();
if (line != null)
{
line = sr.ReadLine();
char[] charArray = new char[] { ',' };
string[] stringArray;
stringArray = line.Split(charArray);
string fileStr = Path.GetFileNameWithoutExtension("D:/CSVFile" + ".csv");
FileStream Mfile = new FileStream(fileStr, FileMode.Create);
StreamWriter sw = new StreamWriter(Mfile);
sw.WriteLine(string.Join(",", stringArray));
sw.Flush();
sw.Close();
}
}
/// <summary>
/// 将DataSet导出成CSV格式
/// </summary>
/// <param name="ds">DataSet</param>
/// <returns>CSV字符串数据</returns>
public static string ExportCSV(DataSet ds)
{
string data = "";
//data = ds.DataSetName + "\n";
foreach (DataTable tb in ds.Tables)
{
//写出列名
foreach (DataColumn column in tb.Columns)
{
data += column.ColumnName + ",";
}
data += "\n";
//写出数据
foreach (DataRow row in tb.Rows)
{
foreach (DataColumn column in tb.Columns)
{
data += row[column].ToString() + ",";
}
data += "\n";
}
data += "\n";
}
return data;
}
public DataSet GetMovieCategorys()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt = GetTableByList(AllUser);
ds.Tables.Add(dt);
return ds;
}
public DataTable GetTableByList(ArrayList list)
{
if (list.Count <= 0)
{
return new DataTable();
}
Type t = list[0].GetType();
DataTable dt = new DataTable(t.Name);
dt.Columns.Add("用户账号");
dt.Columns.Add("用户名称");
dt.Columns.Add("性别");
dt.Columns.Add("Email");
dt.Columns.Add("联系方式(一)");
dt.Columns.Add("联系方式(二)");
dt.Columns.Add("用户创建时间");
DataRow myRow;
//PropertyInfo[] pi = t.GetProperties();
for (int i = 0; i < list.Count; i++)
{
obj = (EDBC.Role.Employee)list[i];
myRow = dt.NewRow();
myRow[0] = obj.EmplID;
myRow[1] = obj.EmplName;
myRow[2] = obj.EmplSex;
myRow[3] = obj.EmplEmail;
myRow[4] = obj.EmplMobile;
myRow[5] = obj.EmplTel;
//myRow[4] = "(" + obj.SMS_SuccessCount.ToString() + "/" + obj.SMS_Count.ToString() + ")";
myRow[6] = obj.CreateDateTime;
dt.Rows.Add(myRow);
}
return dt;
}
# endregion
# region 导出按钮的点击事件
/// <summary>
/// 导出按钮的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void FileCSV_Click(object sender, EventArgs e)
{
AllUser = themgrLog.FindMyEmployee_List(sqlconditionUser);
DataTable table = GetTableByList(AllUser);
//string TableCsv = ExportCSV(GetMovieCategorys());
if (table.Rows.Count >= 1)
{
string data = ExportCSV(GetMovieCategorys());
string Filetime = DateTime.Now.ToString("yyyyMMddhhmmss");
string temp = string.Format("attachment;filename={0}", "用户列表" + Filetime + ".csv");
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.GetEncoding("gb2312");
Response.AppendHeader("Content-disposition", temp);
Response.Write(data);
Response.Flush();
Response.Close();
}
else
{
//Response.Write("<script>alert('没有数据!')</script>");
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("art.dialog({");
sb.Append("title: '提示信息:',");
sb.Append("lock:true,");
sb.Append("resize:false,");
sb.Append("width:'200px',");
sb.Append("height:'80px',");
sb.Append("content:'没有数据!'");
sb.Append("});");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "", sb.ToString());
}
}
# endregion
public void FileStreamCSV(string csvString)
{
FileStream file = new FileStream("D:/CSVFile", FileMode.Open);
StreamReader sr = new StreamReader(file);
string line = sr.ReadLine();
if (line != null)
{
line = sr.ReadLine();
char[] charArray = new char[] { ',' };
string[] stringArray;
stringArray = line.Split(charArray);
string fileStr = Path.GetFileNameWithoutExtension("D:/CSVFile" + ".csv");
FileStream Mfile = new FileStream(fileStr, FileMode.Create);
StreamWriter sw = new StreamWriter(Mfile);
sw.WriteLine(string.Join(",", stringArray));
sw.Flush();
sw.Close();
}
}
/// <summary>
/// 将DataSet导出成CSV格式
/// </summary>
/// <param name="ds">DataSet</param>
/// <returns>CSV字符串数据</returns>
public static string ExportCSV(DataSet ds)
{
string data = "";
//data = ds.DataSetName + "\n";
foreach (DataTable tb in ds.Tables)
{
//写出列名
foreach (DataColumn column in tb.Columns)
{
data += column.ColumnName + ",";
}
data += "\n";
//写出数据
foreach (DataRow row in tb.Rows)
{
foreach (DataColumn column in tb.Columns)
{
data += row[column].ToString() + ",";
}
data += "\n";
}
data += "\n";
}
return data;
}
public DataSet GetMovieCategorys()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt = GetTableByList(AllUser);
ds.Tables.Add(dt);
return ds;
}
public DataTable GetTableByList(ArrayList list)
{
if (list.Count <= 0)
{
return new DataTable();
}
Type t = list[0].GetType();
DataTable dt = new DataTable(t.Name);
dt.Columns.Add("用户账号");
dt.Columns.Add("用户名称");
dt.Columns.Add("性别");
dt.Columns.Add("Email");
dt.Columns.Add("联系方式(一)");
dt.Columns.Add("联系方式(二)");
dt.Columns.Add("用户创建时间");
DataRow myRow;
//PropertyInfo[] pi = t.GetProperties();
for (int i = 0; i < list.Count; i++)
{
obj = (EDBC.Role.Employee)list[i];
myRow = dt.NewRow();
myRow[0] = obj.EmplID;
myRow[1] = obj.EmplName;
myRow[2] = obj.EmplSex;
myRow[3] = obj.EmplEmail;
myRow[4] = obj.EmplMobile;
myRow[5] = obj.EmplTel;
//myRow[4] = "(" + obj.SMS_SuccessCount.ToString() + "/" + obj.SMS_Count.ToString() + ")";
myRow[6] = obj.CreateDateTime;
dt.Rows.Add(myRow);
}
return dt;
}
# endregion
# region 导出按钮的点击事件
/// <summary>
/// 导出按钮的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void FileCSV_Click(object sender, EventArgs e)
{
AllUser = themgrLog.FindMyEmployee_List(sqlconditionUser);
DataTable table = GetTableByList(AllUser);
//string TableCsv = ExportCSV(GetMovieCategorys());
if (table.Rows.Count >= 1)
{
string data = ExportCSV(GetMovieCategorys());
string Filetime = DateTime.Now.ToString("yyyyMMddhhmmss");
string temp = string.Format("attachment;filename={0}", "用户列表" + Filetime + ".csv");
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.GetEncoding("gb2312");
Response.AppendHeader("Content-disposition", temp);
Response.Write(data);
Response.Flush();
Response.Close();
}
else
{
//Response.Write("<script>alert('没有数据!')</script>");
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("art.dialog({");
sb.Append("title: '提示信息:',");
sb.Append("lock:true,");
sb.Append("resize:false,");
sb.Append("width:'200px',");
sb.Append("height:'80px',");
sb.Append("content:'没有数据!'");
sb.Append("});");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "", sb.ToString());
}
}
# endregion