1

How to export database data into Excel file using C# Windows Forms application?

This is my code:

MySqlConnection con = new MySqlConnection(conne);
con.Open();

MySqlCommand com = new MySqlCommand ("SELECT * FROM aster",con);

MySqlDataAdapter data = new MySqlDataAdapter(com);
DataTable dat = new DataTable("aster");
data.Fill(dat);

dat.WriteXml("C:Downloads\\agent.xls");
MessageBox.Show("export data successfully");

The data exports successfully. But my problem is that again and again it saves in same file.

Please refer some link to help me.

2
  • Yeah, I that can be really better and simpler Commented Jan 14, 2016 at 6:44
  • Because you're saving it as agent.xls all the time? Commented Jan 14, 2016 at 6:54

1 Answer 1

4

Use this datetime format:

DateTime date = DateTime.Now;
int t = date.Hour;
int t1 = date.Minute;
int t2 = date.Second;
int m = date.Month;
int d = date.Day;

dat.WriteXml("C:\\Downloads\\agent" + t + " - " + t1 + " - " + t2 + " - " + d + " - " + m + ".xls");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.