用C#从sql数据库导出csv格式的数据
using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using dooll.Data; using System.Drawing; using System.Web.UI.WebControls; using System.IO; using System.Text; namespace dooll.cs { public partial class _import : dooll.Web.UI.MasterPage { #region 全局变量 string name; string qqNo; string Tel; string email; string house; string remark; string RetrunNum;//执行sql存储过程返回的值 string title; string Url = ""; public string fileCsvName = ""; int ImportId = 0;//获取t_business_thread_import导入的ID int ImportDataID = 0;//获取录入t_business_thread_import_data表中的Id string f_statusText = string.Empty; public int F_siteID = 0;//从cookie上获取站点id DateTime ordertime; MsSql ms = new MsSql(); #endregion protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["siteid"] != null) { F_siteID = Convert.ToInt32(Request.Cookies["siteid"].Value); } if (IsPostBack) { if (CSVHelper.CountSum != 0) { this.lblText.Style["color"] = "#777;font-size:12px"; lblText.Text = "录入完成,总共有<b>" + CSVHelper.CountSum + "</b>条,录入成功<b>" + CSVHelper.ThreadSum + "</b>条"; } } this.LabelInfo.ACL = 3; ChkACL(); } #region 上传csv文件,并获取内容是数据库 //上传 protected void btnUrl_Click(object sender, EventArgs e) { #region 获取并保存上传的文件 //获取并保存上传的文件 try { if (fuUpload.PostedFile != null && fuUpload.FileName != "") { CSVHelper.CountSum = 0; CSVHelper.ImportSum = 0; CSVHelper.ThreadSum = 0; CSVHelper.Pathurl = string.Empty; CSVHelper.ImportId = 0; Random rad = new Random(); string smsg = fuUpload.FileName.ToString(); string[] split = smsg.Split('.'); string namehouzhui = "." + split[1];//获取后缀名 //判断后缀名称-------------start string fileExtension = System.IO.Path.GetExtension(fuUpload.FileName).ToLower(); string allowedExtensions = ".csv"; if (fileExtension != allowedExtensions) { Error.Text = "格式不正确,必须是csv格式!"; lblText.Text = "请重新选择......"; Error.ForeColor = Color.Red; return; } else { Error.Text = ""; lblText.Text = "请稍等..."; } //判断后缀名称-------------end int value = rad.Next(1000, 10000); Url = DateTime.Now.ToString("yyyyMMdd"); string fileUrl = DateTime.Now.ToString("HHmm"); string filename = fileUrl + value + namehouzhui; string pathurl = Server.MapPath(@"\cache\import\" + Url + ""); CreateDirectory(pathurl); CSVHelper.fileUrl = pathurl + "\\"; fileCsvName = filename; fuUpload.PostedFile.SaveAs(pathurl + "\\" + filename + ""); CSVHelper.Pathurl = pathurl + "\\" + filename + ""; } else { lblText.Text = "请选择需上传的文件!!!"; lblText.ForeColor = Color.Red; givImport.Visible = false; return; } } catch (Exception ex) { Error.Text = ex.ToString(); Error.ForeColor = Color.Red; return; } #endregion //当下拉框选择的是QQ楼盘报名 if (DDLList.SelectedValue == "1") { Import(); } // 删除缓存数据 DeleteDirectory(CSVHelper.Pathurl); this.lblText.Style["color"] = "#777;font-size:12px"; if (CSVHelper.CountSum != 0) { lblText.Text = "录入完成,总共有<b>" + CSVHelper.CountSum + "</b>条,录入成功<b>" + CSVHelper.ThreadSum + "</b>条"; } this.btnlook.Visible = true; } #endregion #region 查看导入的数据 /// <summary> /// 查看导入的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnlook_Click(object sender, EventArgs e) { this.btnlook.Visible = true; givImport.Visible = true; this.btnlook.Visible = false; DataSet ds = new DataSet(); SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@f_import_id",SqlDbType.Int) }; sp[0].Value = CSVHelper.ImportId.ToString(); ms.ExecProc("sp_admin_business_thread_import_data_selectByImportId", sp, ref ds, "TableName"); string s = ms.ErrMessage; givImport.DataSource = ds; givImport.DataBind(); try { foreach (GridViewRow rs in givImport.Rows) { int statusId = Convert.ToInt32(rs.Cells[14].Text.ToString()); if (statusId > 1 || statusId < 0) { rs.ForeColor = Color.Red; } } } catch (Exception ex) { Error.Text = ex.ToString(); Error.ForeColor = Color.Red; return; } } #endregion #region 分页事件 /// <summary> /// 分页事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void givImport_PageIndexChanging(object sender, GridViewPageEventArgs e) { givImport.Visible = true; givImport.PageIndex = e.NewPageIndex; givImport.AllowPaging = true; givImport.PageSize = 50; DataSet ds = new DataSet(); SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@f_import_id",SqlDbType.Int) }; sp[0].Value = CSVHelper.ImportId.ToString(); ms.ExecProc("sp_admin_business_thread_import_data_selectByImportId", sp, ref ds, "TableName"); givImport.DataSource = ds; givImport.DataBind(); try { foreach (GridViewRow rs in givImport.Rows) { int statusId = Convert.ToInt32(rs.Cells[14].Text.ToString()); if (statusId > 1 || statusId < 0) { rs.ForeColor = Color.Red; } } } catch (Exception ex) { Error.Text = ex.ToString(); Error.ForeColor = Color.Red; return; } } #endregion #region 根据目录删除文件 //根据目录删除文件 public void DeleteDirectory(string path) { File.Delete(path); } #endregion #region 根据目录创建文件 private static void CreateDirectory(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } #endregion #region 导入数据到t_business_thread_import_data表中 public void Import() { #region 往dbo.t_business_thread_import表插入纪录 //往dbo.t_business_thread_import表插入纪录-------star SqlParameter[] alist = new SqlParameter[]{ new SqlParameter("@f_title",SqlDbType.NVarChar,50), new SqlParameter("@f_master_id",SqlDbType.Int), new SqlParameter("@f_master_name",SqlDbType.NVarChar,50), new SqlParameter("@f_count",SqlDbType.Int), new SqlParameter("@f_okcount",SqlDbType.Int), new SqlParameter("@output",SqlDbType.Int), new SqlParameter("@f_siteid",SqlDbType.Int) }; title = fuUpload.FileName.ToString(); string[] spt = title.Split('.'); title = spt[0]; alist[0].Value = title; if (Admin.ID == "" || Admin.ID == null) { alist[1].Value = 0; } else { alist[1].Value = Convert.ToInt32(Admin.ID); } alist[2].Value = Admin.Description; alist[3].Value = "0"; alist[4].Value = "0"; alist[5].Direction = ParameterDirection.InputOutput; alist[6].Value = F_siteID; int i = ms.ExecProc("sp_admin_business_thread_import_insert", alist);//执行存储过程后返回值 string error = ms.ErrMessage; if (i <= 0)//0失败,1成功 { Error.Text = ms.ErrMessage; Error.ForeColor = Color.Red; return; } if (i == 2) { Error.Text = "导入失败!"; Error.ForeColor = Color.Red; return; } string s = ms.ErrMessage; ImportId = Convert.ToInt32(alist[5].Value); //执行完存储过程获取返回的ImportId值 CSVHelper.ImportId = ImportId; if (ImportId == null || ImportId == 0) { Error.Text = "导入失败!"; Error.ForeColor = Color.Red; return; } //往dbo.t_business_thread_import表插入纪录-------end //在构造函数中传入文件路径和文件名 // CSVHelper CsvFile = new CSVHelper(Server.MapPath("") + "\\", fuUpload.FileName); CSVHelper CsvFile = new CSVHelper(CSVHelper.fileUrl, fileCsvName); DataTable dt = CsvFile.ReadCsvFileToTable();//一种是采用文件流形式读取 CSVHelper.CountSum = dt.Rows.Count;//总共有多少条 #endregion #region 获取数据 foreach (DataRow dr in dt.Rows) { try { if (dr[0] != null) { name = dr[0].ToString(); } else { name = string.Empty; } if (dr[1] != null) { qqNo = (dr[1]).ToString(); } else { qqNo = string.Empty; } if (dr[2] != null) { Tel = (dr[2]).ToString(); } else { Tel = string.Empty; } if (dr[3] != null) { email = dr[3].ToString(); } else { email = string.Empty; } if (dr[4] != null) { house = dr[4].ToString(); } else { house = string.Empty; } if (dr[5] != null) { ordertime = Convert.ToDateTime(dr[5]); } else { ordertime = DateTime.Now; } if (dr[6] != null) { remark = dr[6].ToString(); } else { remark = string.Empty; } } catch (Exception ex) { Error.Text = "格式不正确,无法导入!"; Error.ForeColor = Color.Red; return; } #region 导入数据 try { SqlParameter[] arr = new SqlParameter[] { new SqlParameter("@f_client_name",SqlDbType.NVarChar,50), new SqlParameter("@f_client_qq",SqlDbType.VarChar,50), new SqlParameter("@f_client_tel",SqlDbType.VarChar,50), new SqlParameter("@f_client_email",SqlDbType.VarChar,250), new SqlParameter("@f_newhouse",SqlDbType.NVarChar,50), new SqlParameter("@f_ordertime",SqlDbType.DateTime), new SqlParameter("@f_remark",SqlDbType.NVarChar,250), new SqlParameter("@f_siteid",SqlDbType.Int), new SqlParameter("@f_import_id",SqlDbType.Int), new SqlParameter("@output",SqlDbType.Int) }; arr[0].Value = name; arr[1].Value = qqNo; arr[2].Value = Tel; arr[3].Value = email; arr[4].Value = house; arr[5].Value = ordertime; arr[6].Value = remark; arr[7].Value = F_siteID; arr[8].Value = ImportId.ToString(); arr[9].Direction = ParameterDirection.InputOutput; RetrunNum = ms.ExecProc("sp_admin_business_thread_import_data_Insert", arr).ToString();//执行存储过程 string errors = ms.ErrMessage; //注释:在执行sql存储过程后返回的值 switch (RetrunNum) { case "2": f_statusText = "导入失败!"; break; case "3": f_statusText = "线索表存在记录!"; ++CSVHelper.ImportSum; break; case "4": f_statusText = "写入线索出错!"; ++CSVHelper.ImportSum; break; case "5": f_statusText = "没有找到该楼盘"; ++CSVHelper.ImportSum; break; case "1": f_statusText = "导入成功!"; ++CSVHelper.ImportSum; ++CSVHelper.ThreadSum; break; case "0": f_statusText = "其他错误!"; break; } ImportDataID = Convert.ToInt32(arr[9].Value); } catch (Exception ex) { Error.Text = ex.ToString(); Error.ForeColor = Color.Red; return; } #endregion #region 往dbo.t_business_thread_import_data表更新状态 //往dbo.t_business_thread_import_data表更新状态------start try { SqlParameter[] ArrData = new SqlParameter[] { new SqlParameter("@ImportDataID",SqlDbType.Int), new SqlParameter("@f_status",SqlDbType.Int), new SqlParameter("@f_statusText",SqlDbType.VarChar,200), }; ArrData[0].Value = ImportDataID; ArrData[1].Value = RetrunNum; ArrData[2].Value = f_statusText; int sy = ms.ExecProc("sp_admin_business_thread_import_data_update", ArrData); string ss = ms.ErrMessage; if (sy == 2) { Error.Text = "修改导入表出错!"; Error.ForeColor = Color.Red; return; } if (sy <= 0) { Error.Text = ms.ErrMessage; ; Error.ForeColor = Color.Red; return; } } catch (Exception ex) { Error.Text = ex.ToString(); Error.ForeColor = Color.Red; return; } //往dbo.t_business_thread_import_data表更新状态------end #endregion } #endregion #region 往dbo.t_business_thread_import表插入采集成功的纪录 //往dbo.t_business_thread_import表插入采集成功的纪录------start try { SqlParameter[] Iarr = new SqlParameter[] { new SqlParameter("@ImportID",SqlDbType.Int), new SqlParameter("@ImportNum",SqlDbType.Int), new SqlParameter("@ThreadSum",SqlDbType.Int), }; Iarr[0].Value = ImportId; Iarr[1].Value = CSVHelper.ImportSum; Iarr[2].Value = CSVHelper.ThreadSum;//采集成功多少条 int t = ms.ExecProc("sp_admin_business_thread_import_update", Iarr); if (t == 2) { Error.Text = "修改线索表出错!"; Error.ForeColor = Color.Red; return; } if (t <= 0) { Error.Text = ms.ErrMessage; ; Error.ForeColor = Color.Red; return; } } catch (Exception ex) { Error.Text = ex.ToString(); Error.ForeColor = Color.Red; return; } //往dbo.t_business_thread_import表插入采集成功的纪录------end #endregion } #endregion } #region CSVHelper public class CSVHelper { private string path; private string fileName; public static int ImportSum = 0;//纪录往t_business_thread_import插入成功多少条 public static int ThreadSum = 0;//纪录往t_business_thread插入成功多少条 public static int CountSum = 0;//从csv获取的总条数 public static string Pathurl = string.Empty; public static string fileUrl = string.Empty; public static int ImportId = 0; /// <summary> /// z构造函数 /// </summary> public CSVHelper(string filePath, string fileName) { this.path = filePath; this.fileName = fileName; } #region 通过文件流的方式来读取CSV文件 /// <summary> /// 通过文件流的方式来读取CSV文件,默认第一列为标题列,列之间用逗号分隔 /// </summary> /// <param name="files"></param> /// <returns></returns> public DataTable ReadCsvFileToTable() { return ReadCsvFileToTable(true, ','); } /// <summary> /// 通过文件流的方式来读取CSV文件,默认列之间用逗号分隔 /// </summary> /// <param name="files">文件名称</param> /// <param name="HeadYes">第一行是否为列标题</param> /// <returns></returns> public DataTable ReadCsvFileToTable(bool HeadYes) { return ReadCsvFileToTable(HeadYes, ','); } /// <summary> /// 通过文件流的方式来读取CSV文件 /// </summary> /// <param name="files">文件名称</param> /// <param name="HeadYes">第一行是否为列标题</param> /// <param name="span">分隔符</param> /// <returns></returns> public DataTable ReadCsvFileToTable(bool HeadYes, char span) { //文件路径和文件名 string files = path + fileName; DataTable dt = new DataTable(); StreamReader fileReader = new StreamReader(files, Encoding.Default); try { //是否为第一行(如果HeadYes为TRUE,则第一行为标题行) int lsi = 0; //列之间的分隔符 char cv = span; while (fileReader.EndOfStream == false) { string line = fileReader.ReadLine(); string[] y = line.Split(cv); //第一行为标题行 if (HeadYes == true) { //第一行 if (lsi == 0) { for (int i = 0; i < y.Length; i++) { dt.Columns.Add(y[i].Trim().ToString()); } lsi++; } //从第二列开始为数据列 else { DataRow dr = dt.NewRow(); for (int i = 0; i < y.Length; i++) { dr[i] = y[i].Trim(); } dt.Rows.Add(dr); } } //第一行不为标题行 else { if (lsi == 0) { for (int i = 0; i < y.Length; i++) { dt.Columns.Add("Col" + i.ToString()); } lsi++; } DataRow dr = dt.NewRow(); for (int i = 0; i < y.Length; i++) { dr[i] = y[i].Trim(); } dt.Rows.Add(dr); } } } catch (Exception ex) { } finally { fileReader.Close(); fileReader.Dispose(); } return dt; } #endregion } #endregion }
从csv格式读取数据导入到sql表中
using System; using System.Data; using System.Data.SqlClient; using System.Web.UI; using dooll.Data; namespace dooll { public partial class export : dooll.Web.UI.MasterPage { public string surl = ""; protected void Page_Load(object sender, EventArgs e) { this.LabelInfo.ACL = 3; ChkACL(); string ExportID = Request.QueryString["ExportID"]; try { if (ExportID != "") { IsExport(ExportID); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('下载失败!');", true); return; } } catch (Exception ex) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('下载失败!错误提示:" + ex.ToString() + "');", true); return; } } #region 根据编号从数据库获取数据集 public DataSet Export(string f_import_id) { MsSql ms = new MsSql(); DataSet ds = new DataSet(); SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@f_import_id", SqlDbType.Int) }; sp[0].Value = f_import_id; ms.ExecProc("sp_admin_business_thread_import_data_export_select", sp, ref ds, "TableName"); return ds; } #endregion #region 导出 public void IsExport(string import_id) { try { //设置导出的文件名 Random rd = new Random(); int value = rd.Next(1000, 10000); string fileUrl = DateTime.Now.ToString("HHmm"); DataSet dds = Export(import_id); if (dds != null && dds.Tables[0].Rows.Count != 0) { OutPutCSV(dds, fileUrl); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('下载失败!不存在该导入编号!');", true); return; } } catch (Exception ex) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('下载失败!错误提示:" + ex.ToString() + "');", true); return; } } #endregion public void OutPutCSV(DataSet ds, string fileName) { try { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".csv");//txt檔名 Response.ContentType = "application/unknown"; Response.Charset = ""; System.Text.StringBuilder sb = new System.Text.StringBuilder(); //欄位名 for (int i = 0; i < ds.Tables[0].Columns.Count; i++) { sb.Append(ds.Tables[0].Columns[i].ColumnName); sb.Append(","); } sb.Append(Environment.NewLine); //資料 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { sb.Append(ds.Tables[0].Rows[i][j].ToString()); sb.Append(","); } sb.Append(Environment.NewLine); } Response.Write(sb.ToString()); Response.End(); } catch (Exception ex) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('下载失败!错误提示:" + ex.ToString() + "');", true); return; } } } }
存储过程:
USE [800Fc#Com#System] GO /****** Object: StoredProcedure [dbo].[sp_admin_business_thread_import_data_insert] Script Date: 02/26/2013 13:51:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[sp_admin_business_thread_import_data_insert] @f_client_name nvarchar(50), @f_client_tel varchar(50), @f_client_qq varchar(50), @f_client_email varchar(250), @f_newhouse nvarchar(50), @f_ordertime datetime, @f_remark nvarchar(250), @f_siteid int, @f_import_id int, @output int=0 output as declare @houseid int = 0 declare @serviceid int =0 declare @returnNo int=0 declare @thread int=0 select @houseid = id from dbo.t_newhouse where f_name=@f_newhouse and f_siteid=@f_siteid and f_status<>99 if @@ROWCOUNT=0 begin set @returnNo=5 end --插入到t_business_thread_import_data表 begin transaction --事务开始 insert into t_business_thread_import_data(f_client_name,f_client_qq,f_client_tel,f_client_email,f_newhouse,f_ordertime,f_remark,f_addtime,f_newhouse_id,f_siteid,f_import_id) values(@f_client_name,@f_client_qq,@f_client_tel,@f_client_email,@f_newhouse,@f_ordertime,@f_remark,GETDATE(),@houseid,@f_siteid,@f_import_id); set @serviceid = @@IDENTITY set @output=@serviceid if(@returnNo!=5) begin if(@@ERROR>0) begin set @returnNo=2--导入失败 end else begin --插入到Insert_t_business_thread表 if exists(select 1 from dbo.t_business_thread where f_addtime=@f_ordertime and f_client_tel=@f_client_tel and f_newhouse=@f_newhouse and f_siteid=@f_siteid) begin set @returnNo=3--线索表存在记录 end else begin insert into dbo.t_business_thread(f_client_name,f_client_qq,f_client_tel,f_client_email,f_newhouse,f_addtime,f_newhouse_id,f_source,f_source_id,f_service_id,f_siteid) values(@f_client_name,@f_client_qq,@f_client_tel,@f_client_email,@f_newhouse,@f_ordertime,@houseid,'楼盘库',30,@serviceid,@f_siteid); set @thread=@@IDENTITY if(@@ERROR>0) begin rollback transaction --回滚事务 set @returnNo=4--写入线索出错 end else begin set @returnNo=1--导入成功 --插入到dbo.t_business_log表 insert into dbo.t_business_log(f_siteid,f_type,f_content,f_client_name,f_client_tel,f_client_sex,f_status,f_addtime,f_master_id,f_master_name,f_oldid,f_newid,f_client_telinfo) values(@f_siteid,18,'楼盘库导入',@f_client_name,@f_client_tel,0,@returnNo,GETDATE(),0,'系统生成',@thread,1,dbo.fn_PhoneNum2Area(@f_client_tel)) end end end end commit transaction --提交事务 return @returnNo
USE [800Fc#Com#System] GO /****** Object: StoredProcedure [dbo].[sp_admin_business_thread_import_data_update] Script Date: 02/26/2013 13:51:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[sp_admin_business_thread_import_data_update] @ImportDataID int, @f_status int, @f_statusText Varchar(200) as begin transaction update dbo.t_business_thread_import_data set f_status=@f_status, f_statustext=@f_statusText where id=@ImportDataID if(@@ERROR>0) begin rollback transaction return 2 end else begin commit transaction return 1 end
USE [800Fc#Com#System] GO /****** Object: StoredProcedure [dbo].[sp_admin_business_thread_import_insert] Script Date: 02/26/2013 13:51:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[sp_admin_business_thread_import_insert] @f_title nvarchar(50), @f_master_id int, @f_master_name nvarchar(50), @f_count int, @f_okcount int, @f_siteid int, @output int =0 output as begin transaction --事务开始 insert into dbo.t_business_thread_import(f_siteid,f_title,f_addtime,f_master_id,f_master_name,f_count,f_okcount) values(@f_siteid,@f_title,GETDATE(),@f_master_id,@f_master_name,@f_count,@f_okcount) set @output= @@IDENTITY if(@@ERROR>0) begin return 2 rollback transaction --回滚事务 end else begin commit transaction --提交事务 return 1 end
USE [800Fc#Com#System] GO /****** Object: StoredProcedure [dbo].[sp_admin_business_thread_import_update] Script Date: 02/26/2013 13:52:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[sp_admin_business_thread_import_update] @ImportID int, @ImportNum int, @ThreadSum int as begin transaction update dbo.t_business_thread_import set f_count=@ImportNum ,f_okcount=@ThreadSum where id=@ImportID if(@@ERROR>0) begin return 2 rollback transaction end else begin commit transaction return 1 end