问题
I'm creating/sending a file with EPPlus like this:
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet");
... //create file here
Response.Clear();
Response.ContentType = "application/xlsx";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xlsx");
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
}
Everything works fine, except the file always comes back as read-only, and I can't figure out why. Within the "package" object, there is a file object with a IsReadOnly field, but the file object is null. I anticipate that I'm not creating the xcel file correctly, but this is the only way I could figure out how to create the file well. Initially I was using a memory stream, but doing that, I ran into issues when the excel file was bigger than 50 rows.
Edit/update: So I initiate the code block by clicking a button "Download as Excel File". The code runs, creating the file, and the user is prompted with a "You have chose to open: thisismyexcelfile.xlsx which is a: XLSX file (size here) from: mywebsite. What should firefox do with this file?" After selecting "Open with OpenOffice Calc" the spreedsheet opens and displays appropriately but is read-only.
Edit/update: I checked the file properties with OpenOffice. Under Properties/Security there is a "Open file Read Only" checkbox, but it is already unchecked and disabled.
回答1:
I found this example, I see 3 main differences
- Response.ContentType is set to application/vnd.openxmlformats
- Response.WriteFile is used instead or Response.BinaryWrite
In this example the file is being saved to the server, sent as response and then deleted
void ExportToExcel(Event evt) { var fileInfo = new FileInfo(Path.GetTempPath() + "\\" + DateTime.Now.Ticks + ".xlsx"); using (var xls = new ExcelPackage(fileInfo)) { var sheet = xls.Workbook.Worksheets.Add(evt.Title); sheet.Cell(1, 1).Value = "First name"; sheet.Cell(1, 2).Value = "Last name"; sheet.Cell(1, 3).Value = "E-mail"; sheet.Cell(1, 4).Value = "Phone"; sheet.Cell(1, 5).Value = "Registered"; sheet.Cell(1, 6).Value = "Live Meeting"; var i = 1; foreach(var attendee in evt.Attendees) { i++; var profile = attendee.Profile; sheet.Cell(i, 1).Value = profile.FirstName; sheet.Cell(i, 2).Value = profile.LastName; sheet.Cell(i, 3).Value = profile.Email; sheet.Cell(i, 4).Value = profile.Phone; sheet.Cell(i, 5).Value = att.Created.ToString(); sheet.Cell(i, 6).Value = att.LiveMeeting.ToString(); } xls.Save(); } Response.Clear(); Response.ContentType = "application/vnd.openxmlformats"; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name); Response.WriteFile(fileInfo.FullName); Response.Flush(); if (fileInfo.Exists) fileInfo.Delete(); }
回答2:
From the comments, I was able to verify that saving the file to a location on the disk and opening it directly did not open the file as read-only, implying the problem was not with the code, but the browsers handling of files downloaded from the internet.
来源:https://stackoverflow.com/questions/18059979/creating-an-excel-file-with-epplus-but-it-is-always-read-only