简体   繁体   中英

How to save downloaded Excel file data from Data table into application folder in ASP.NET

I want downloaded file into save application folder here I downloaded file as Excel sheet from data table. I didn't take path to file download it's directly download from data table and I want to save it in application folder. How can I do this?

My code:

 public void ExportToExcel(DataSet ds)
        {           
            using (XLWorkbook wb = new XLWorkbook())
            {
                foreach (DataTable dt in ds.Tables)
                {
                    //Add DataTable as Worksheet.
                    wb.Worksheets.Add(dt);
                }
                //Export the Excel file.
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");                
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);                  
                    Response.TransmitFile(Server.MapPath("~/Files/Report.xlsx"));// I added this but it shows error
                    Response.Flush();                 
                    Response.End();
                }
            }
        } 

It took a while to make sense of the order of things but removing the call to TransmitFile() should work. You're already writing out the file via the MemoryStream .

    public void ExportToExcel(DataSet ds)
    {           
        using (XLWorkbook wb = new XLWorkbook())
        {
            foreach (DataTable dt in ds.Tables)
            {
                //Add DataTable as Worksheet.
                wb.Worksheets.Add(dt);
            }
            //Export the Excel file.
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=Report.xlsx");                
            using (MemoryStream MyMemoryStream = new MemoryStream())
            {
                wb.SaveAs(MyMemoryStream);
                MyMemoryStream.WriteTo(Response.OutputStream);                  
                Response.Flush();                 
                Response.End();
            }
        }
    }

If this is a standard MVC controller action then your response is much easier and changes to;

    public FileStreamResult ExportToExcel(DataSet ds)
    {           
        using (XLWorkbook wb = new XLWorkbook())
        {
            foreach (DataTable dt in ds.Tables)
            {
                //Add DataTable as Worksheet.
                wb.Worksheets.Add(dt);
            }
            using (MemoryStream MyMemoryStream = new MemoryStream())
            {
                wb.SaveAs(MyMemoryStream);
                return FileStreamResult(MyMemoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM