I am getting below error when exporting file
Access to the path 'D:\Excel\OrderDetails.xlsx' is denied.
I do not want to specify folder, just want to directly download on browser when exporting excel file
below is my code
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = GetDataTableFromExcel();
List<DataTable> dts = dt.AsEnumerable()
.GroupBy(row => row.Field<string>("Ref_ID"))
.Select(g => g.CopyToDataTable()).ToList();
string path = "D:\\Excel\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
using (XLWorkbook wb = new XLWorkbook())
{
for (int i = 0; i < dts.Count; i++)
{
if (!string.IsNullOrEmpty(dts[i].Rows[0][0].ToString()))
{
wb.Worksheets.Add(dts[i], dts[i].Rows[0][0].ToString());
}
}
wb.SaveAs(path + "OrderDetails.xlsx");
}
DownloadFile(path + "OrderDetails.xlsx");
}
public void DownloadFile(string path)
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(path));
Response.WriteFile(path);
Response.End();
}