以编程方式使用sharepoint designer 2010获取文件时出错
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());
}
}
我想要获取的工作簿仍有3个工作表 这使得工作表计数为零(0), 因此在使用GetAsByteArray进行转换时,其显示错误为:
System.InvalidOperationException:工作簿必须至少包含 OfficeOpenXml.ExcelWorkbook.Save()中的一个工作表
的OfficeOpenXml.ExcelPackage.GetAsByteArray(布尔保存)