Thursday, March 6, 2008

Coding For Import Data from datagrid to Excel,Word,Notepad

protected void Button1_Click(object sender, EventArgs e)
{
//export method called
ExportDataSetToExcel_Method(Cache["sample"] as DataSet);
}
//Export Method
private void ExportDataSetToExcel_Method(DataSet ds)
{
Response.Clear();
Response.ContentType = "application/vijay.excel";
Response.Charset = "";
Response.AddHeader("content-disposition", "attachment; FileName=Book1.xls");
DataSet copyDataSet = ds.Copy();
GridView dg = new GridView();
dg.DataSource = copyDataSet;
dg.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}


//protected void Button1_Click(object sender, EventArgs e)
//{
// ExportDataSetToExcel_Method(Cache["Categories"] as DataSet, new string[] { "CategoryID" });
//new string[] is used for remove the primary column from database because pk is not imported
//}

//private void ExportDataSetToExcel_Method(DataSet ds, string[] columns)
//{
// Response.Clear();
// Response.ContentType = "application/vnd.excel";
// Response.Charset = "";


// Response.AddHeader("content-disposition", "attachment; FileName=MyFile.xls");


// DataSet copyDataSet = ds.Copy();


// if (columns != null && columns.Length > 0)
// {
// RemoveColumns(copyDataSet, columns);
// }


// GridView dg = new GridView();
// dg.DataSource = copyDataSet;
// dg.DataBind();


// StringWriter sw = new StringWriter();
// HtmlTextWriter htw = new HtmlTextWriter(sw);


// dg.RenderControl(htw);


// Response.Write(sw.ToString());


// Response.End();
//}


//private void RemoveColumns(DataSet ds, string[] columns)
//{
// foreach (string columnName in columns)
// ds.Tables[0].Columns.Remove(columnName);
//}

protected void Button2_Click(object sender, EventArgs e)
{
ExportDataSetToWord_Method(Cache["sample"] as DataSet);
}
private void ExportDataSetToWord_Method(DataSet ds)
{
Response.Clear();
Response.ContentType = "application/vijay.Documnet";
Response.Charset = "";
Response.AddHeader("content-disposition", "attachment; FileName=Doc1.Doc");
DataSet copyDataSet = ds.Copy();
GridView dg = new GridView();
dg.DataSource = copyDataSet;
dg.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
private void ExportDataSetToNotePad_Method(DataSet ds)
{


Response.Clear();
Response.ContentType = "application/vijay.Documnet";
Response.Charset = "";
Response.AddHeader("content-disposition", "attachment; FileName=one.txt");
DataSet copyDataSet = ds.Copy();
GridView dg = new GridView();
dg.DataSource = copyDataSet;
dg.DataBind();

StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
protected void Button3_Click(object sender, EventArgs e)
{
ExportDataSetToNotePad_Method(Cache["sample"] as DataSet);
}
}

No comments: