Hi @Dharmendr
First of all thanks a lot for your quick reply.
I have used your code as follows:
After successful excution no excel file generated.
pl help!
I also want to auto generate new file name of excel during save.
private void button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
try
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string strLocation = dlg.SelectedPath;
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "Users");
wb.SaveAs(strLocation + "testReport.xlsx");
}
MessageBox.Show("Database Exported successfully!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}