当前位置:编程学习 > C#/ASP.NET >>

Asp.net中导入导出CSV文件的问题,代码如何写?

在ASP.net中导入,导出CSV文件的问题写入数据库
答案:给你一段代码参考下吧:
//TO CSV

protected void Button5_Click(object sender, EventArgs e)

{

DataTable dt = this.GetDataTable();

HttpContext.Current.Response.Clear();

System.IO.StringWriter sw = new System.IO.StringWriter();

int iColCount = dt.Columns.Count;

for (int i = 0; i < iColCount; i++)

{

sw.Write("\\"" + dt.Columns[i] + "\\"");

if (i < iColCount - 1)

{

sw.Write(",");

}

}

sw.Write(sw.NewLine);

foreach (DataRow dr in dt.Rows)

{

for (int i = 0; i < iColCount; i++)

{

if (!Convert.IsDBNull(dr[i]))

sw.Write("\\"" + dr[i].ToString() + "\\"");

else

sw.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();

}

//FROM CSV

protected void Button6_Click(object sender, EventArgs e)

{

string filepath = Server.MapPath("ss.csv");

if (!File.Exists(filepath))

{

Page.RegisterClientScriptBlock("msg", "<script>alert('该文件不存在!')</script>");

}

else

{

string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=";

strConn += ";Extensions=asc,csv,tab,txt;";

OdbcConnection objConn = new OdbcConnection(strConn);

DataSet ds = new DataSet();

try

{

string strSql = "select * from " + filepath;

OdbcDataAdapter odbcCSVDataAdapter = new OdbcDataAdapter(strSql, objConn);

odbcCSVDataAdapter.Fill(ds);

this.GridView2.DataSource = ds.Tables[0];

this.GridView2.DataBind();

}

catch (Exception ex)

{

throw ex;

}

}

}

}

}
给你一段代码参考下吧:
//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;}}}}}

上一个:ASP.NET中怎样才能使自己的代码运行的效率更高?
下一个:怎样在asp.net中连接ACCESS数据库 要能直接连接的代码

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,