问题
On my extjs webclient I hit a button and it downloads an excel template and populates data. When I click on the CHROME popup to open excel file, it gives me the excel error message below. Does anyone know how I can fix this? The actual file looks fine to me.
Here is my webapi code
public HttpResponseMessage CreatePosSheet(string cName, string calcMethod, string username, int cid, HttpRequestMessage Request)
{
string templatePath = HttpContext.Current.Server.MapPath(@"~\tempLocation\");
string templateFileName = System.Configuration.ConfigurationManager.AppSettings["TemplateFileName"];
string templateName = string.Format("{0}{1}", templatePath, templateFileName);
FileInfo xlTemplate = new FileInfo(templateName);
HttpResponseMessage response;
response = Request.CreateResponse(HttpStatusCode.OK);
MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/octet-stream");
//MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content = new ByteArrayContent(ExcelSheet(xlTemplate, cName, calcMethod, username, cid));
response.Content.Headers.ContentType = mediaType;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "Orders.xlsx";
return response;
}
here is the ExcelSheet code
public byte[] ExcelSheet(FileInfo xlTemplate, string cName, string calcMethod, string username, int cid)
{
object[] tempObj = Orders(username, cid);
List<TradeDetailDTO> tList = new List<TradeDetailDTO>();
List<TempClass> acctList = new List<TempClass>();
List<TempClass2> sectorList = new List<TempClass2>();
tList = tempObj[0] as List<TradeDetailDTO>;
acctList = tempObj[1] as List<TempClass>;
sectorList = tempObj[2] as List<TempClass2>;
#region set column variables
int colAcctNum = 1;
int colAcctDesc = 2;
int colAcctTrdLvl = 5;
int colCname = 6;
int rowCname = 15;
#endregion
using (var package = new ExcelPackage(xlTemplate))
{
var worksheet = package.Workbook.Worksheets["Data"];
var wkshtFormula = package.Workbook.Worksheets["Formulas"];
wkshtFormula.Cells[rowCname, colCname].Value = cName;
package.Save();
return package.GetAsByteArray();
}
}
回答1:
You have to remove the call to .Save()
since it will close the pck as a side effect. See this for more info:
Shaman.EPPlus + ASP.NET Core MVC - Part already exist exception
来源:https://stackoverflow.com/questions/40141077/webapi-using-epplus-excel-file-download-error-on-open-unreadable-content-r