C# 以编程方式使用sharepoint designer 2010获取文件时出错
以编程方式使用sharepoint designer 2010获取文件时出错C# 以编程方式使用sharepoint designer 2010获取文件时出错,c#,excel,sharepoint,sharepoint-2010,C#,Excel,Sharepoint,Sharepoint 2010,以编程方式使用sharepoint designer 2010获取文件时出错 protected void updateTemplate_1() { SPSite spSite = SPContext.Current.Site; SPWeb spWeb = spSite.OpenWeb(); string FilePath = ""; try { spWeb.AllowUnsafe
protected void updateTemplate_1()
{
SPSite spSite = SPContext.Current.Site;
SPWeb spWeb = spSite.OpenWeb();
string FilePath = "";
try
{
spWeb.AllowUnsafeUpdates = true;
SPListItemCollection listItems = spWeb.Lists["Captive Report"].Items;
string sitecol= spSite.Url.ToString().Trim();
foreach(SPListItem lst11 in listItems)
{
FilePath=lst11.Url.ToString().Trim(); //Gets the site-relative URL of the item
}
SPFile file = spWeb.GetFile(FilePath);
System.IO.FileInfo templateFile = new System.IO.FileInfo(@FilePath);
using (OfficeOpenXml.ExcelPackage pck = new OfficeOpenXml.ExcelPackage(templateFile)) //OfficeOpenXml.ExcelPackage(result, source)
{
int count = pck.Workbook.Worksheets.Count;
Response.Write(count+"<br>");
byte[] fileBytes = pck.GetAsByteArray();
//byte[] fileBytes = file.OpenBinary();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//Add the header & other information
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
string filename = "fileName_" + DateTime.Now.ToString("MMM").Trim();// +".xlsx";
Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"" + filename + ".xlsx\"; " //filename of excel
);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Write it back to the client
Response.BinaryWrite(fileBytes);
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
protectedvoid updateTemplate_1()
{
SPSite SPSite=SPContext.Current.Site;
SPWeb SPWeb=spSite.OpenWeb();
字符串FilePath=“”;
尝试
{
spWeb.AllowUnsafeUpdates=true;
SPListItemCollection listItems=spWeb.List[“捕获报告”]。项;
字符串sitecol=spSite.Url.ToString().Trim();
foreach(列表项中的SPListItem lst11)
{
FilePath=lst11.Url.ToString().Trim();//获取项目的站点相对Url
}
SPFile file=spWeb.GetFile(文件路径);
System.IO.FileInfo templateFile=new System.IO.FileInfo(@FilePath);
使用(OfficeOpenXml.ExcelPackage pck=new OfficeOpenXml.ExcelPackage(templateFile))//OfficeOpenXml.ExcelPackage(结果,源)
{
int count=pck.Workbook.Worksheets.count;
响应。写入(计数+“
”);
byte[]fileBytes=pck.GetAsByteArray();
//byte[]fileBytes=file.OpenBinary();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//添加标题和其他信息
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl=“private”;
Response.Charset=System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding=System.Text.UTF8Encoding.UTF8;
AppendHeader(“Content-Length”,fileBytes.Length.ToString());
AppendHeader(“Pragma”、“cache”);
回复。附录标题(“到期”、“60”);
string filename=“filename_”+DateTime.Now.ToString(“MMM”).Trim();//+“.xlsx”;
AppendHeader(“内容处置”,
“附件;”+
“filename=\”“+filename+”.xlsx\”;“//excel的文件名
);
Response.ContentType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”;
//将其写回客户机
BinaryWrite(fileBytes);
}
}
捕获(例外情况除外)
{
Response.Write(例如ToString());
}
}
我试图获取的工作簿仍然有3个工作表
这表示工作表计数为零(0),
因此,在使用GetAsByteArray进行转换时,其错误显示为:
System.InvalidOperationException:工作簿必须至少包含
一个工作表位于OfficeOpenXml.ExcelWorkbook.Save()处
OfficeOpenXml.ExcelPackage.GetAsByteArray(布尔保存)位于
SharePoint Designer是一个桌面应用程序,您不能“以编程方式”使用它。至于错误,它与SharePoint无关。错误很明显-您试图创建一个没有任何工作表的Excel文件。如果您检查
count
变量,您会发现它是0。FilePath
是否指向有效的本地文件?你是想用文件
内容来代替吗?除此之外,代码没有多大意义。迭代所有项,但仅使用最后一个文件路径。您试图从模板创建Excel文件,但从未在其中放入任何内容。你想干什么?如果您想向客户端发送文档,为什么不直接将客户端重定向到文档的URL?还是只发送文件的内容?您好,谢谢您的时间。是的,我同意SharePoint Designer是桌面应用程序,但出于我们的一些要求,我们以编程方式实现了它。它确认excel文件不是空的,它有3个工作表。我不能将其重定向到客户端,因为一旦我得到的工作表计数超过零,我需要在excel中进行更改,然后将其保存回去。我希望这些信息能有所帮助。不是真的,因为错误与你所说的相矛盾。首先,此代码只能在SharePoint server上运行-SharePoint designer不可编程。第二,你真正加载的是什么文件?只有当FilePath是本地服务器文件的路径,或者至少是网络共享上的文件时,您的代码才会工作。如果FilePath指向存储在SharePoint中的文档,则无法使用。那么,您检查了哪个Excel文件以确认有工作表?至于重定向和检查-你现在不检查任何东西。错误很明显。你试图发送一个空文件如果我在代码中避免使用OpenOfficeXml,只需打开文件,我就可以下载它。如果文件是空的,我如何下载它?在VisualStudioWeb部件中尝试时,也会遇到类似的问题。