Monday, March 25, 2019

mysql - How to export datatable to excel in c# windows application

I found some code through googling that allows me to export a data table to excel file. i successfully export the file from database table and save it in my document
My coding is:


  using System;
using System.Data.OleDb;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data;
namespace ImportFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String connection = "SERVER=******;"DATABASE=dbd;"UID=root;"PASSWORD=pws;"Convert Zero Datetime = True";
private void BExport_Click(object sender, EventArgs e)
{
DateTime dat = DateTime.Now;
int time = dat.Hour;
int time1 = dat.Minute;
int time2 = dat.Second;
int month = dat.Month;
int day = dat.Day;
MySqlConnection connection1 = new MySqlConnection(connection);
connection1.Open();
MySqlCommand command = new MySqlCommand ("SELECT * FROM TABLE_Name",connection1);
MySqlDataAdapter dataadpter = new MySqlDataAdapter(command);
DataTable datatable = new DataTable("TABLE_NAME");
dataadpter.Fill(datatable);
datatable.WriteXml("C:\\Users\\Downloads\\agent.xlsx" + time + " - " + time1 + " - " + time2 + " - " + day + " - " + month + "");
MessageBox.Show("export data");
}
}
}

my problem is that when i download file(click import button)its not download an excel file its just an normal file.


how i download it as excel file . if any one know this.... help me

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...