i using EPPlus package to export 2007 excel file from datatable, but i want to save excel file that i have created on the server, i using this method

private string SaveExcelFile(DataTable dt)
        {
            string reportName = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss-fff") + ".xlsx";

           Response.Clear();
           Response.Charset = "";
           Response.ContentEncoding = System.Text.Encoding.UTF8;
           Response.Cache.SetCacheability(HttpCacheability.NoCache);
           Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
           Response.AddHeader("content-disposition", "attachment;filename=\"" + reportName + "\"");

           using(ExcelPackage pck = new ExcelPackage())
           {
             ExcelWorksheet wsDt = pck.Workbook.Worksheets.Add("Sheet1");
             wsDt.Cells["A1"].LoadFromDataTable(dt, true, TableStyles.None);
             wsDt.Cells[wsDt.Dimension.Address].AutoFitColumns();

             //Response.BinaryWrite(pck.GetAsByteArray());
             string filePhysicalPath = Server.MapPath("~/Uploads/PeriodsReports/" + reportName);
             System.IO.File.WriteAllText(filePhysicalPath, pck.GetAsByteArray().ToString());
           }

           Response.Flush();
           Response.End();

            return reportName;
        }

but there are an error while saving related to this line : System.IO.File.WriteAllText(filePhysicalPath, pck.GetAsByteArray().ToString());

Related posts

Recent Viewed