I able to export excel file by a button which to view all, but how to export excel file by selected user login?
Example:
username login = Joyce.
She can export out her data according to her name in AM column.
Remark: AM = Account Manager.
What I have tried:
protected void Button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Type, NatureBusiness, Title, Firstname, Lastname, Company, Mobile_Phone, Office_Phone, ForeignNoF, ForeignNoT, Email, Job_Title, City, Country, AM, ISR, Remarks, Staff, Date FROM Customer Where AM = CURRENT_USER"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "Account Record");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Account Database.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
}
}
}
}