jueves, 21 de mayo de 2009

Exportar un gridview a un Archivo de texto delimitado













Aqui hay un ejemplo grafico y html de como quedara la pagina.

y el codigo del boton buscar:
protected void btnbuscar_Click(object sender, EventArgs e)
{
string sql = "";
OracleDataAdapter da = new OracleDataAdapter("", cn);
DataSet ds = new DataSet();
sql = "select campo,campo from tabla";
da.SelectCommand.CommandText = sql;
da.Fill(ds, "tabla1");
GridView1.DataSource = ds.Tables["tabla1"];
GridView1.DataBind();
}

código del boton exportar;
protected void exportar_Click(object sender, EventArgs e)
{
if (txtcaracter.Text == "")
{
txtcaracter.Text = "";
}
if (txtfile.Text == "")
{
txtfile.Text = "";
}
string sql = "";
OracleDataAdapter da = new OracleDataAdapter("", cn);
DataSet ds = new DataSet();
sql = "select tramite,descripcion from sotramitestb";
da.SelectCommand.CommandText = sql;
da.Fill(ds, "tabla1");
GridView1.DataSource = ds.Tables["tabla1"];
GridView1.DataBind();
StringWriter stringWrite = new StringWriter();
for (int i = 0; i <= (ds.Tables["tabla1"].Rows.Count - 1);i++)
{
for (int j = 0; j <= (ds.Tables["tabla1"].Columns.Count - 1); j++)
{
//str.Append(ds.Tables[0].Rows[i][j].ToString());
stringWrite.Write(ds.Tables[0].Rows[i][j].ToString());
if ((ds.Tables["tabla1"].Columns.Count - 1) != j)
{
stringWrite.Write(txtcaracter.Text);
}
}
stringWrite.WriteLine("");
}
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename="+txtfile.Text);
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.text";
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Response.Write(stringWrite.ToString());
Response.End();
}


si les sirvio dejen comentario.

No hay comentarios.: