ADD THIS NAMESPACE's
using System.Text;
using System.IO;
In a Page we will taken a Button,and Give the Button Name or ID btnExport
This Button Click Event We Write This Code
C# Code
protected void btnExport_Click(object sender, EventArgs e)
{
try
{
lblReport means here all the data are retrive and store in this LBLreport
if (lblReport.Text.ToString().Trim() != "")
{
StringBuilder s2 = new StringBuilder();
s2.Append("<table>");
s2.Append("<tr>");
s2.Append("<td align='center' colspan='4'>");
s2.Append("<b>Student Details Report</b>");
s2.Append("</td>");
s2.Append("</tr>");
s2.Append("</table>");
this.ExportToExcel(s2.ToString().Trim()+lblReport.Text.ToString());
}
else
{
Response.Write("<script language='javascript'>alert('No data exist to export');</script>");
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
// In your Solution Explorer you add a Folder which Name is Exported_Files //
private void ExportToExcel(string dataToExport)
{
try
{
string filepath = Server.MapPath("Exported_Files/" + DateTime.Now.ToString("dd-MM-yyyy hh-mm-ss") + ".xls");
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(dataToExport.ToString().Trim());
sw.Close();
fs.Close();
Response.ClearContent();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + filepath);
Response.WriteFile(filepath);
Response.End();
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
using System.Text;
using System.IO;
In a Page we will taken a Button,and Give the Button Name or ID btnExport
This Button Click Event We Write This Code
C# Code
protected void btnExport_Click(object sender, EventArgs e)
{
try
{
lblReport means here all the data are retrive and store in this LBLreport
if (lblReport.Text.ToString().Trim() != "")
{
StringBuilder s2 = new StringBuilder();
s2.Append("<table>");
s2.Append("<tr>");
s2.Append("<td align='center' colspan='4'>");
s2.Append("<b>Student Details Report</b>");
s2.Append("</td>");
s2.Append("</tr>");
s2.Append("</table>");
this.ExportToExcel(s2.ToString().Trim()+lblReport.Text.ToString());
}
else
{
Response.Write("<script language='javascript'>alert('No data exist to export');</script>");
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
// In your Solution Explorer you add a Folder which Name is Exported_Files //
private void ExportToExcel(string dataToExport)
{
try
{
string filepath = Server.MapPath("Exported_Files/" + DateTime.Now.ToString("dd-MM-yyyy hh-mm-ss") + ".xls");
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(dataToExport.ToString().Trim());
sw.Close();
fs.Close();
Response.ClearContent();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=" + filepath);
Response.WriteFile(filepath);
Response.End();
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
No comments:
Post a Comment