Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
}
}
}
}
}
}
}
Posted
Comments
VandrenSKL 7-Mar-16 3:56am    
For the code i show is only able to export all not selected user. Please advice?
Richard Deeming 7-Mar-16 5:37am    
You should read the following Microsoft knowledgebase article:

Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

There are various ways to create Excel spreadsheets on the server without using Office interop. For example:
* EPPlus[^];
* ClosedXML[^];
* The OpenXML SDK[^];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900