将导出的Excel工作表设为只读

问题描述

| 在我的应用程序中,我正在将gridview数据导出到excel并将其存储到一个特定的文件夹中,现在我想要的是将此excel文件设置为只读,以便没有人可以对其进行编辑。 我写了这样的代码: 受保护的void Button5_Click(对象发送者,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在这方面帮助我...     

解决方法

        您要在写入文件后将其设置为只读文件吗?
var fileInfo = new FileInfo(FilePathName );
fileInfo.IsReadOnly = true;
编辑 这是一个网站?我不知道您可以防止他人下载然后修改文件的方法。您可以使用API​​代替。我们在这里使用Aspose.Cells将Sheets设为只读,然后再将其发送给客户端。     ,        我想你想拥有像
 FileStream Fs = new FileStream(FilePathName,FileMode.Create,Fileaccess.Read);
查看此链接以获取更多信息 http://msdn.microsoft.com/zh-CN/library/4z36sx0f.aspx#Y494     ,        您不能先一起工作,您必须先创建文件流,然后再定义其访问模式 看看这个链接 http://msdn.microsoft.com/en-us/library/y973b725.aspx