Karthi
Karthi

Reputation: 100

How to export database data into Excel file

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.

Upvotes: 1

Views: 1508

Answers (1)

user5788181
user5788181

Reputation:

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");

Upvotes: 4

Related Questions