简体   繁体   中英

How do I export file excel is read-only

I'm exporting a file excel by StringBuilder. How can I set read-only for file excel.

this's my code :

byte[] fileContents = Encoding.UTF8.GetBytes(sb.ToString());

return File(fileContents, "application/vnd.ms-excel", "FileName" + DateTime.Now + ".xls");

You can't. You have to protect the workbook instead of making it read-only. File attributes are not preserved over HTTP, so there is no way to force a client to set the read-only attribute on the file (and if you could, a client could easily remove it).

There are options to make the workbook protected on the server, but Microsoft discourages the use of Office in ASP.NET , so you have to look for other options, like EPPlus .

Can't you open the Excel via C#? If you can; open the file and run vba with

SetAttr("TESTFILE", vbReadOnly)

and close the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM