1个回答
2013-07-18
展开全部
给你一段代码参考下吧:
//TOCSVprotectedvoidButton5_Click(objectsender,EventArgse){DataTabledt=this.GetDataTable();HttpContext.Current.Response.Clear();System.IO.StringWritersw=newSystem.IO.StringWriter();intiColCount=dt.Columns.Count;for(inti=0;i<iColCount;i++){sw.Write("\""+dt.Columns[i]+"\"");if(i<iColCount-1){sw.Write(",");}}sw.Write(sw.NewLine);foreach(DataRowdrindt.Rows){for(inti=0;i<iColCount;i++){if(!Convert.IsDBNull(dr[i]))sw.Write("\""+dr[i].ToString()+"\"");elsesw.Write("\"\"");if(i<iColCount-1){sw.Write(",");}}sw.Write(sw.NewLine);}sw.Close();HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=ss.csv");HttpContext.Current.Response.ContentType="application/vnd.ms-excel";HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");HttpContext.Current.Response.Write(sw);HttpContext.Current.Response.End();}//FROMCSVprotectedvoidButton6_Click(objectsender,EventArgse){stringfilepath=Server.MapPath("ss.csv");if(!File.Exists(filepath)){Page.RegisterClientScriptBlock("msg","<script>alert('该文件不存在!')</script>");}else{stringstrConn=@"Driver={MicrosoftTextDriver(*.txt;*.csv)};Dbq=";strConn+=";Extensions=asc,csv,tab,txt;";OdbcConnectionobjConn=newOdbcConnection(strConn);DataSetds=newDataSet();try{stringstrSql="select*from"+filepath;OdbcDataAdapterodbcCSVDataAdapter=newOdbcDataAdapter(strSql,objConn);odbcCSVDataAdapter.Fill(ds);this.GridView2.DataSource=ds.Tables[0];this.GridView2.DataBind();}catch(Exceptionex){throwex;}}}}}
//TOCSVprotectedvoidButton5_Click(objectsender,EventArgse){DataTabledt=this.GetDataTable();HttpContext.Current.Response.Clear();System.IO.StringWritersw=newSystem.IO.StringWriter();intiColCount=dt.Columns.Count;for(inti=0;i<iColCount;i++){sw.Write("\""+dt.Columns[i]+"\"");if(i<iColCount-1){sw.Write(",");}}sw.Write(sw.NewLine);foreach(DataRowdrindt.Rows){for(inti=0;i<iColCount;i++){if(!Convert.IsDBNull(dr[i]))sw.Write("\""+dr[i].ToString()+"\"");elsesw.Write("\"\"");if(i<iColCount-1){sw.Write(",");}}sw.Write(sw.NewLine);}sw.Close();HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=ss.csv");HttpContext.Current.Response.ContentType="application/vnd.ms-excel";HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");HttpContext.Current.Response.Write(sw);HttpContext.Current.Response.End();}//FROMCSVprotectedvoidButton6_Click(objectsender,EventArgse){stringfilepath=Server.MapPath("ss.csv");if(!File.Exists(filepath)){Page.RegisterClientScriptBlock("msg","<script>alert('该文件不存在!')</script>");}else{stringstrConn=@"Driver={MicrosoftTextDriver(*.txt;*.csv)};Dbq=";strConn+=";Extensions=asc,csv,tab,txt;";OdbcConnectionobjConn=newOdbcConnection(strConn);DataSetds=newDataSet();try{stringstrSql="select*from"+filepath;OdbcDataAdapterodbcCSVDataAdapter=newOdbcDataAdapter(strSql,objConn);odbcCSVDataAdapter.Fill(ds);this.GridView2.DataSource=ds.Tables[0];this.GridView2.DataBind();}catch(Exceptionex){throwex;}}}}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询