In my application i'm exporting gridview data to excel and storing it to a particular folder now what i want is to make this excel file as read only so that no one should edit it.
i have written code like this:
protected void Button5_Click(object sender, EventArgs e) {
this.GridView1.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
hw.WriteLine("<b开发者_开发问答><font size='5'> Report</font></b>");
this.GridView1.RenderControl(hw);
string HtmlInfo = tw.ToString();
string DocFileName = "Report" + ".xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0, FilePathName.LastIndexOf("\\"));
FilePathName = @"C:\Excel" + "\\" + DocFileName;
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter = new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("UTF-8"));
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
}
any1 help me on this...
You want to make the file readonly after it's been written?
var fileInfo = new FileInfo(FilePathName );
fileInfo.IsReadOnly = true;
EDIT
This is for a website? I don't know of a way you can prevent someone from downloading then modifying your file. You could use an API to do it instead. We use Aspose.Cells here to make Sheets readonly before sending them to the client.
I think you want to have something like
FileStream Fs = new FileStream(FilePathName, FileMode.Create, Fileaccess.Read);
See this link for more info http://msdn.microsoft.com/en-us/library/4z36sx0f.aspx#Y494
You cant do both work together first u have to create file stream and than define it's access mode
Have a look on this link
http://msdn.microsoft.com/en-us/library/y973b725.aspx
精彩评论