C# Epplus在Web项目中使用模板
我一直在.NET桌面项目(C#)上使用epplus,并使用如下模板:C# Epplus在Web项目中使用模板,c#,asp.net,templates,web,epplus,C#,Asp.net,Templates,Web,Epplus,我一直在.NET桌面项目(C#)上使用epplus,并使用如下模板: var package = new ExcelPackage(new FileInfo("C:\\Templates\\FormatoReporteSamsung.xlsx")) 但现在我正在处理一个.NET Web项目(C#),我不知道如何引用像Web资源一样存在的模板,其中该资源的URI如下所示: http://myownweb:29200/Content/excelTemplates/Formato.xlsx 最后
var package = new ExcelPackage(new FileInfo("C:\\Templates\\FormatoReporteSamsung.xlsx"))
但现在我正在处理一个.NET Web项目(C#),我不知道如何引用像Web资源一样存在的模板,其中该资源的URI如下所示:
http://myownweb:29200/Content/excelTemplates/Formato.xlsx
最后,我使用以下代码将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文件,我使用以下命令
以及获取我使用的网站的名称
您尝试过这个吗?:
var package=new ExcelPackage(“http://myownweb:29200/Content/excelTemplates/Formato.xlsx“”
是,它显示一个错误:不支持URI格式。
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;
}
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;
}
}