2014-09-24 51 views
0

我一直在与.NET桌面项目epplus(C#),并使用模板的工作是这样的:Epplus使用模板上的Web项目

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

但现在与.NET Web项目I'working(C#)我不知道该怎么做来指代存在像网络资源,其中像这样的资源的URI模板:

http://myownweb:29200/Content/excelTemplates/Formato.xlsx 
+1

您是否尝试过这个?: 'var package = new ExcelPackage(“http:// myownweb:29200/Content/excelTemplates/Formato.xlsx”)' – Donal 2014-09-24 14:48:21

+0

是的,它显示一个错误:URI格式a不支持。 – Kenar716 2014-09-24 14:55:26

回答

0

最后我通过Excel模板为使用此代码流。

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(); 
    } 

要读取Excel文件有个字节我用这个 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; 
     } 

并获得该网站的名称,我使用 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; 
      } 
     }