protected void btnExport_Click(object sender, EventArgs e)
{
}
public static void WriteAttachment(string FileName, string FileType, string content)
{
try
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
Response.End();
}
catch (Exception ex)
{
}
}
{
DataTable dt = new DataTable();
dt.Columns.Add("Sr No");
dt.Columns.Add("Qty");
dt.Columns.Add("Rate");
foreach (gridviewrow
item in gridview.Rows)
{
DataRow
dr = dt.NewRow();
dr["Sr
No"] = item["colOrderSrNo"].Text;
dr["Qty"]
= item["colOrderQty"].Text;
dr["Rate"]
= item["colUnitRate"].Text;
;
dt.Rows.Add(dr);
}
if (dt == null)
{
throw
new Exception("No record found for export");
}
string Path = "D:\\FileName"
+ DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString()
+ ".xls";
FileInfo FI = new FileInfo(Path);
if (!FI.Exists)
{
}
else
{
FI.Delete();
}
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt;
DataGrd.DataBind();
DataGrd.RenderControl(htmlWrite);
string
directory = Path.Substring(0, Path.LastIndexOf("\\"));
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
System.IO.StreamWriter vw = new System.IO.StreamWriter(Path,
true);
stringWriter.ToString().Normalize();
vw.Write(stringWriter.ToString());
vw.Flush();
vw.Close();
WriteAttachment(FI.Name, "application/vnd.ms-excel",
stringWriter.ToString());
public static void WriteAttachment(string FileName, string FileType, string content)
{
try
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
Response.End();
}
catch (Exception ex)
{
}
}