0

I have been working with epplus on .NET desktop projects (C#) and using templates like this:

var package = new ExcelPackage(new FileInfo("C:\\Templates\\FormatoReporteSamsung.xlsx"))

But now I'working with a .NET Web Project (C#) and i don't know what make to refer to the template that exist like a web resource where the URI of that resource like this:

http://myownweb:29200/Content/excelTemplates/Formato.xlsx 
Kenar716
  • 11
  • 4

1 Answers1

0

At the end I pass the excel template as a stream using this code.

using (var package = new ExcelPackage(new MemoryStream(GetBytesTemplate(FullyQualifiedApplicationPath + "Content/excelTemplates/Format.xlsx"))))
    {
        //Write data to excel

        //Read file like byte array to return a response
        Response.Clear();
        Response.ContentType = "application/xlsx";
        Response.AddHeader("content-disposition", "attachment; filename=" + "myFileName" + ".xlsx");
        Response.BinaryWrite(package.GetAsByteArray());
        Response.End();
    }

To read the excel file has bytes I use this Error "This stream does not support seek operations" in C#

private byte[] GetBytesTemplate(string url)
        {
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            WebResponse myResp = myReq.GetResponse();

            byte[] b = null;
            using (Stream stream = myResp.GetResponseStream())
            using (MemoryStream ms = new MemoryStream())
            {
                int count = 0;
                do
                {
                    byte[] buf = new byte[1024];
                    count = stream.Read(buf, 0, 1024);
                    ms.Write(buf, 0, count);
                } while (stream.CanRead && count > 0);
                b = ms.ToArray();
            }

            return b;
        }

And to get the name of the website i use http://devio.wordpress.com/2009/10/19/get-absolut-url-of-asp-net-application/

public string FullyQualifiedApplicationPath
        {
            get
            {
                //Return variable declaration
                string appPath = null;

                //Getting the current context of HTTP request
                HttpContext context = HttpContext.Current;

                //Checking the current context content
                if (context != null)
                {
                    //Formatting the fully qualified website url/name
                    appPath = string.Format("{0}://{1}{2}{3}",
                      context.Request.Url.Scheme,
                      context.Request.Url.Host,
                      context.Request.Url.Port == 80
                        ? string.Empty : ":" + context.Request.Url.Port,
                      context.Request.ApplicationPath);
                }
                if (!appPath.EndsWith("/"))
                    appPath += "/";
                return appPath;
            }
        }
Community
  • 1
  • 1
Kenar716
  • 11
  • 4