I have looked everywhere for the answer to my problem but I cant seem to figure it out. I tried the method described at Create Excel (.XLS and .XLSX) file from C# using the excellibrary and the excel file was created but the data was completely destroyed and was not recoverable. What is the best way export data from mysql to excel in an asp.net application?
DataSet ds = new DataSet("New_Dataset");
DataTable dt = new DataTable("New_DataTable");
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
MySqlConnection con = new MySqlConnection(GetConnectionString());
con.Open();
string sql = "SELECT * FROM reportdata WHERE TakenDate =?Date";
MySqlCommand cmd = new MySqlCommand(sql, con);
MySqlDataAdapter adapt = new MySqlDataAdapter();
MySqlParameter[] param = new MySqlParameter[1];
param[0] =new MySqlParameter("?Date", MySqlDbType.VarChar, 10);
param[0].Value = txtStartDate.Text;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
adapt.SelectCommand = cmd;
adapt.Fill(dt);
con.Close();
ds.Tables.Add(dt);
string strDay = DateTime.Now.DayOfWeek.ToString();
string day = DateTime.Now.Day.ToString();
string month = DateTime.Now.Month.ToString();
string year = DateTime.Now.Year.ToString();
ExcelLibrary.DataSetHelper.CreateWorkbook(@"G:\Reports\Report " + strDay + " " + month + "-" + day + "-" + year + ".xls", ds);