3

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.

6
  • What do you mean when you say that the file "comes back as read-only"? Could you describe the scenario more precisely? Commented Aug 5, 2013 at 13:59
  • Question updated to be more explicit. Commented Aug 5, 2013 at 14:13
  • 1
    Are you sure it's not MS Office doing that? That's the default for any files downloaded off the net. It makes them readonly and gives a security warning. Commented Aug 5, 2013 at 14:13
  • I don't actually have MS Office, but I did consider that it was OpenOffice doing it. Is there a way I can prevent that or do I just have to assume the user knows how to work around this? Commented Aug 5, 2013 at 14:17
  • 2
    Actually I think it's the browser that makes the file read-only when you choose to open it directly. If you save it somewhere explicitly, it doesn't happen. So in any case, the issue is not in your code. Commented Aug 5, 2013 at 15:00

2 Answers 2

6

I found this example, I see 3 main differences

  1. Response.ContentType is set to application/vnd.openxmlformats
  2. Response.WriteFile is used instead or Response.BinaryWrite
  3. 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(); 
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.